【发布时间】:2015-08-27 01:51:02
【问题描述】:
我使用 hibernate4 和 spring4 创建了一个网络动态项目。 这是我的文件:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>posts</display-name>
<welcome-file-list>
<welcome-file>users.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
</web-app>
这是我调用 init() 方法的 bean
package com.posts.beans;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.posts.models.Users;
import com.posts.services.UsersService;
@SuppressWarnings("serial")
@Component("Usersbean")
@Scope("session")
public class UsersBean implements Serializable{
@Autowired
private transient UsersService us;
private List<Users> lu;
public UsersBean() {
}
@PostConstruct
public void init(){
lu=us.getAllUsers();
System.out.println("hello");//doesn't shw up=init() doesnt work
}
public String getMyname(){
return "mohamed";
}
public List<Users> getLu() {
return lu;
}
public void setLu(List<Users> lu) {
this.lu = lu;
}
}
这是我的jsp文件;我试图只显示一个字段:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<!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=UTF-8">
<title>test list users</title>
</head>
<body>
<jsp:useBean id="users" class="com.posts.beans.UsersBean" />
<table border="1">
<tr><th>id</th><th>nom</th><th>prenom</th><th>login</th><th>password</th></tr>
</table>
<c:forEach var="user" items="${users.lu}">
<p><c:out value="${user.nom}" /></p>
</c:forEach>
</body>
</html>
所以控制台不会在方法 init 中显示消息“hello”,这意味着 id 不起作用。 也许是因为我没有在 web.xm 中声明 servlet 元素,但我不知道它是如何工作的.. 注意:我使用 JUnit 测试了服务,工作正常
【问题讨论】:
-
为什么要这样做,您没有使用托管的 Spring 实例,而是让 tje JSP 自己创建一个。调用不起作用(也不知道
@PostConstruct。 -
ii m new at using jsp .. 解决方案是什么??
-
请勿使用
jsp:useBean。使用 Spring Controller 为您准备模型。 -
感谢您的快速回复,但是修改 web.xml 是否还有其他操作,因为??
-
没有。您必须使用spring托管实例,您可以使用
RequestContextUtils获取ApplicationContext并检索bean。但这基本上是您应该避免的视图层中的编程,因此是带有模型的控制器。
标签: spring hibernate jsp spring-mvc web.xml