将数据从spring控制器类传递给jsp时出错

时间:2013-05-06 12:26:55

标签: spring spring-mvc

我正在为CRUD尝试一些弹簧样本。现在我能够成功地将数据保存在数据库中但是当我尝试通过jsp显示它时我无法这样做...我的弹簧控制器类如下< / p>

CController.java

@Controller
public class CController{

  private UserDAO1 userDAO;

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

  @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);

        return new ModelAndView("redirect:list.htm");
    }

    @RequestMapping(params = "/frm/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 = "/frm/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 = "/frm/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");
    }

    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);
    }
 }

我的jsp文件如下

的List.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  <form:form method="POST">
   <table>
    <tr>
      <td width="50">Id</td>
      <td width="150">First Name</td>
      <td width="150">Last Name</td>
      <td width="100">Money</td>
      <td width="50">Currency</td>
    </tr>
   <c:forEach items="${list}" var="person">
    <tr>
     <td><c:out value="${person.id}" /></td>
     <td><c:out value="${person.name}" /></td>
     <td><c:out value="${person.password}" /></td>
     <td><c:out value="${person.gender}" /></td>
     <td><c:out value="${person.country}" /></td>
    </tr>
   </c:forEach>
  </table>
 </form:form>
</body>
</html>

我的控制器配置文件如下

CController-servlet.xml中

<?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: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>

我有三个xml文件,一个用于管理员级别,另一个用于dao,另一个是应用程序上下文。上面是管理员级别xml

当我尝试在jsp中显示数据时,会发生以下错误

HTTP Status 404 - 

--------------------------------------------------------------------------------

type Status report

message 

description The requested resource () is not available.

我正在获取网址

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

当我应该得到的实际网址是

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

frm4是我的另一个jsp表单,我将数据传输到控制器类。

有人可以帮助PLZ

2 个答案:

答案 0 :(得分:0)

您可以更改此行
    return new ModelAndView("redirect:list.htm");
到     return new ModelAndView("redirect:/list.htm");
这将解决问题。

答案 1 :(得分:0)

我通过手动添加

来修复它
 <mvc:default-servlet-handler />

到我的spring配置文件。