<求助> hibernate+spring+servlet无法传值到JSP界面
本帖最后由 qq245282209 于 2013-07-21 15:26:05 编辑 这是我的UserListServlet.java
package com.zemo.hibernatespring.action;
import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import com.zemo.hibernatespring.bean.User;
import com.zemo.hibernatespring.biz.impl.UserBizImpl;
@Controller
public class UserListServlet extends HttpServlet {
@Resource
private UserBizImpl userBiz;
private static final long serialVersionUID = -8609429659212933530L;
@Override
public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("-----------service--------------");
List<User> users = userBiz.getAllUser();
request.setAttribute("users", users);
System.out.println("---------------" + users.size() + "-----------");
getServletConfig().getServletContext()
.getRequestDispatcher("/userList.jsp")
.forward(request, response);
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="com.zemo.hibernatespring.bean.User"%>
<%@ page import="java.util.*" %>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc//DTD JSP Tag Library 1//EN"
"http://javasuncom/j2ee/dtds/web-jsptaglibrary_1_dtd">
<html>
<head>
<title>用户列表</title>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" align="center" width="800">
<tr>
<th>ID</th>
<th>用户名</th>
<th>密码</th>
</tr>
<%
List<User> users = (List<User>)request.getAttribute("users");
if (users == null || users.size() == 0) {
%>
<tr>
<td colspan="3">没有符合条件的数据232</td>
</tr>
<%
}else {
for (Iterator<User> iter=users.iterator(); iter.hasNext();) {
User user = (User)iter.next();
%>
<tr>
<td><%=user.getUserId() %></td>
<td><%=user.getUserName() %></td>
<td><%=user.getPassword() %></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>UserListServlet</servlet-name>
<servlet-class>com.zemo.hibernatespring.action.UserListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserListServlet</servlet-name>
<url-pattern>/userList.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/userList.jsp</welcome-file>
</welcome-file-list>
</web-app>
.forward(request, response); 原来的转发代码最好改为 request.getRequestDispatcher("你的列表页").forward(request,response);
5 你对servlet还不熟悉 继续加油.
[解决办法]