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

时间:2013-05-08 12:44:03

标签: spring spring-mvc

我坚持这个春天的例子。我通过jsp页面从用户那里获取数据,并且从控制器中获取保存方法,并通过hibernateTemplate.save()方法将数据存储在数据库中。我能够实现直到这个但当我尝试通过将数据传递给jsp来显示数据时我得到了http 404错误。我的控制器类如下

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类如下

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

    }

 }

和我试图显示列表的jsp类如下

的List.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org 
   /TR/html4/loose.dtd">
<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="${userList}" 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>

而不是使用

  <c:forEach items="${userList}" var="person">

我也试过

  <c:forEach items="${user}" var="person">

  <c:forEach items="${list}" var="person">

我的spring 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: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>

这只是xml配置文件的一部分,因为我使用了两个xml文件。

我可以将数据保存在数据库中,但是当我尝试显示数据时,我收到以下错误

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

需要帮助PLZ

1 个答案:

答案 0 :(得分:0)

你有太多的反斜杠......

你写道:

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

但必须是:

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

你在viewResolver

中有这个