Spring MVC 需要打印从spring控制器传递的jsp中的列表

t0ybt7op  于 2022-11-14  发布在  Spring
关注(0)|答案(4)|浏览(173)

我在一个Spring示例中遇到了一个错误。我可以从用户那里获得输入,并将数据传输到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">

我正在使用两个springconfigxml,其中一个springxml如下所示:
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.

并且URL是:

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

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

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

6tdlim6h1#

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

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

bz4sfanl2#

@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
}

试试看这个能不能用。2请检查一下。

wpx232ag

wpx232ag3#

您的问题有两种解决方案:
1.也许你没有把你的视图(jsp)保存在你项目中的正确位置。所以,把你的jsp保存在WEB-INF/jsp文件夹下。
1.从控制器返回JSP的实际路径。

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

but5z9lq4#

在控制器中

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

不是测试..

相关问题