需要在jsp中打印一个从spring控制器传递的列表

时间:2013-05-09 06:00:30

标签: spring spring-mvc

我在一个春天的例子中遇到错误,我正在关注。我能够从用户那里获得输入并将数据传输到Spring控制器,在那里使用hibernate模板在数据库中进行更新。但是,当我尝试通过将数据传递到.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 {

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

我的hibernate模板实现类如下

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文件如下

<%@ 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 config xml,我的一个spring xml如下

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

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

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

我问了一个类似的问题,他们说我没有将列表添加到模型中,但是我的控制器中的下面一行是这样的:

 modelMap.addAttribute("userList", userDAO.listUser());

4 个答案:

答案 0 :(得分:0)

尝试在CController控制器上设置list属性,如下所示:

request.setAttribute("userList",userDAO.listUser());

答案 1 :(得分:0)

您的问题有两种解决方案:

  1. 也许您没有将视图(jsp)存储在项目中的正确位置。所以,将你的jsp保存在WEB-INF / jsp文件夹下。
  2. 从Controller返回JSP的实际路径。
  3.  return new ModelAndView("redirect:/list.htm");
    

答案 2 :(得分:0)

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



    model.put("userList", userDAO.listUser());
    model.put("user", new User1());
    return "list";  // this is name of jsp page like list.jsp
}

试试这个会奏效。请检查。

答案 3 :(得分:0)

在控制器

   @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(HttpServletRequest request,
          HttpServletResponse response) throws Exception {
    ModelAndView mav = new ModelAndView("list");
    mav.addObject("lstUser", userDAO.listUser());
    return mav;
  }

在Jsp中

   <c:forEach items="${lstUser}" 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>

不测试..