Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法将数据从spring控制器传输到jsp_Spring_Spring Mvc - Fatal编程技术网

无法将数据从spring控制器传输到jsp

无法将数据从spring控制器传输到jsp,spring,spring-mvc,Spring,Spring Mvc,我一直在使用这个spring示例。我通过jsp页面从用户处获取数据,并调用控制器的save方法,然后通过hibernateTemplate.save()将数据存储在数据库中方法。在此之前我可以实现,但是当我试图通过将数据传递给jsp来显示数据时,我得到了HTTP404错误 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmln

我一直在使用这个spring示例。我通过jsp页面从用户处获取数据,并调用控制器的save方法,然后通过hibernateTemplate.save()将数据存储在数据库中方法。在此之前我可以实现,但是当我试图通过将数据传递给jsp来显示数据时,我得到了HTTP404错误

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"   
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
   xmlns:util="http://www.springframework.org/schema/util"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans   
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema
          /jdbc/spring-jdbc-3.0.xsd
   http://www.springframework.org/schema/tx   http://www.springframework.org/schema
        /tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/util  http://www.springframework.org/schema
        /util/spring-util-3.0.xsd
   http://www.springframework.org/schema/mvc   http://www.springframework.org/schema 
         /mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/context  http://www.springframework.org/schema
       /context/spring-context-3.0.xsd">

 <mvc:default-servlet-handler />
  <mvc:annotation-driven />
 <context:annotation-config/>

<bean  
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>


<bean id="urlMapping"  
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>


        </props>
    </property>
</bean>



<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />


<bean name="cController.do" class="project4.CController" >
  <property name="userDAO" ref="myUserDAO"/>

</bean>


<bean name="indexController"  
    class="org.springframework.web.servlet.mvc.ParameterizableViewController"
        p:viewName="index" />

</beans>
CController.java

import project4.UserDAO1;
import project4.User1;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.validation.BindingResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;


@Controller


public class CController{

    private UserDAO1 userDAO;
    @Autowired
    @Qualifier("myUserDAO")
    private UserDAOImpl1 myUserDAO;

    public void setUserDAO(UserDAO1 userDAO) {
        this.userDAO = userDAO;
    }

    @RequestMapping(value = "/frm4/add", method = RequestMethod.POST)
   public ModelAndView add( @ModelAttribute("add") User1 user,HttpServletRequest 
         request,HttpServletResponse response) throws Exception {
         System.out.println("hai");

    userDAO.saveUser(user);
    System.out.println("hai");
    return new ModelAndView("redirect:/list.htm");
    }

    @RequestMapping(params = "delete", method = RequestMethod.POST)
    @Transactional
    public ModelAndView delete(@ModelAttribute("delete") User1 user,HttpServletRequest 
        request,HttpServletResponse response) throws Exception {
        userDAO.deleteUser(user);
        return new ModelAndView("redirect:list.htm");
    }


    @RequestMapping(params = "find", method = RequestMethod.POST)
    @Transactional
    public ModelAndView find(@ModelAttribute("find") User1 user,HttpServletRequest 
         request,HttpServletResponse response) throws Exception {  
                     userDAO.findUser(user);
                      return new ModelAndView("redirect:list.htm");
    }


    @RequestMapping(params = "update", method = RequestMethod.POST)
    @Transactional
    public ModelAndView update(@ModelAttribute("update") User1 user,HttpServletRequest 
        request,HttpServletResponse response) throws Exception {  
                     userDAO.updateUser(user);
                      return new ModelAndView("redirect:list.htm");
    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
     public ModelAndView list(HttpServletRequest request,
        HttpServletResponse response) throws Exception {


        ModelMap modelMap = new ModelMap();
        modelMap.addAttribute("userList", userDAO.listUser());
        modelMap.addAttribute("user", new User1());
        return new ModelAndView("list", modelMap);
     }
  }
我的hibernateTemplate类如下

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"   
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
   xmlns:util="http://www.springframework.org/schema/util"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans   
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema
          /jdbc/spring-jdbc-3.0.xsd
   http://www.springframework.org/schema/tx   http://www.springframework.org/schema
        /tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/util  http://www.springframework.org/schema
        /util/spring-util-3.0.xsd
   http://www.springframework.org/schema/mvc   http://www.springframework.org/schema 
         /mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/context  http://www.springframework.org/schema
       /context/spring-context-3.0.xsd">

 <mvc:default-servlet-handler />
  <mvc:annotation-driven />
 <context:annotation-config/>

<bean  
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>


<bean id="urlMapping"  
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>


        </props>
    </property>
</bean>



<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />


<bean name="cController.do" class="project4.CController" >
  <property name="userDAO" ref="myUserDAO"/>

</bean>


<bean name="indexController"  
    class="org.springframework.web.servlet.mvc.ParameterizableViewController"
        p:viewName="index" />

</beans>
UserDAOImpl.java

package project4;
import project4.User1;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class UserDAOImpl1 implements UserDAO1 {

    private HibernateTemplate hibernateTemplate;

  public void setSessionFactory(SessionFactory sessionFactory) {
    this.hibernateTemplate = new HibernateTemplate(sessionFactory);
  }

   @Override
   @Transactional(propagation=Propagation.REQUIRED, readOnly=false)
   public void saveUser(User1 user) {
      try {
       System.out.println (user.getId ());
      hibernateTemplate.save(user);
      }catch (RuntimeException re){
            throw re;
      }
  }

  @Override
  @SuppressWarnings("unchecked")
  public List<User1> listUser() {
    List<User1> result = hibernateTemplate.find("from User1");
    System.out.println("hai");
    System.out.println(result);
    return result;
  }

    @Override
  public void deleteUser(User1 user) {
    hibernateTemplate.delete(user);
  }

    @Override
    public List<User1> findUser(User1 user) {
        List<User1> result =hibernateTemplate.find("from User1 where USER_ID=:" 
                   +user.getId());
        return result;

    }

    @Override
    public void updateUser(User1 user) {
        hibernateTemplate.update(user);

    }

 }
我得到的网址是

http://localhost:8080/Spring/list.htm

需要帮助plz

你的反斜杠太多了

你写道:

@RequestMapping(value = "/list", method = RequestMethod.GET)
但它必须是:

@RequestMapping(value = "list", method = RequestMethod.GET)

您的viewResolver中可能有此错误信息

您可以打印准确的错误信息吗?(转到您的服务器日志并复制抛出的异常)它会这样说“eclipse.buildId=M20090211-1700 java.version=1.6.0_03 java.vendor=Sun Microsystems Inc.引导加载程序常量:OS=win32,ARCH=x86,WS=win32,NL=en_US命令行参数:-os win32-ws win32-arch x86错误Mon May 06 09:58:11 IST 2013将忽略从插件“org.eclipse.wst.jsdt.web.ui.internal.hyperlink.script.event.JSPJavaHyperlinkDetector”到“org.eclipse.ui.workbench.texteditor.HyperlinkDetector”的扩展点,因为它包含无效的属性。“您希望jsp中有一个列表,但并没有将其添加到spring控制器操作方法中的模型中。”。
type Status report

message /Spring/WEB-INF/jsp/list.jsp

description The requested resource (/Spring/WEB-INF/jsp/list.jsp) is not available.
http://localhost:8080/Spring/list.htm
@RequestMapping(value = "/list", method = RequestMethod.GET)
@RequestMapping(value = "list", method = RequestMethod.GET)