我堅持這一個春天的例子。我通過jsp頁面從用戶獲取數據,並從控制器保存的方法被喚醒,數據通過hibernateTemplate.save存儲在數據庫中)method.I我能夠實現直到但是當我嘗試通過將數據以顯示數據把jsp我得到HTTP 404 error.My控制器類是如下couldnt傳輸數據從彈簧控制器到jsp
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">
和我的春天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
你期待在JSP列表。但是你並沒有在彈簧控制器操作方法中將它添加到模型中。 – 2013-05-08 14:06:45