2015-08-27 24 views
0

我已經創建了一個web動態項目,使用hibernate4spring4。 這裏有我的文件:init @PostConstruct不起作用


的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()方法

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這意味着ID不工作的消息「你好」控制檯。 也許這是因爲我沒有在web.xm聲明servlet元素,但我沒有它如何工作.. NB任何想法:我測試服務在JUnit,工作得很好

+0

爲什麼要它,你沒有使用託管的Spring實例,並讓JSP自己創建一個。這不會起作用(也不瞭解'@ PostConstruct'。 –

+0

ii新的使用jsp ..什麼是解決方案? – mednabli

+0

不要用戶'jsp:useBean'。使用Spring Controller爲你的模型做準備 –

回答

0

我已經解決了註解這個問題與@Service(「的UserBean」)UserBean類並添加:

<bean id="usersBean" class="com.posts.beans.UsersBean" init-method="init" /> 

在應用context.xml文件.. 當我運行我的應用程序,我看到冬眠SELECT生成的代碼和消息hello

你好

休眠:選擇users0_.id如id1_2_,users0_.login如login2_2_,users0_.nom如nom3_2_,users0_.password如password4_2_,users0_.prenom如prenom5_2_從public.users users0_

「,在這意味着init()方法已被執行。

0

你可以把它在三種不同的方式工作:

  1. 添加到您的應用程序上下文。XML豆,負責制定@PostConstruct註釋:

    <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

  2. 新增已擁有CommonAnnotationBeanPostProcessor會內namespase:

<context:annotation-config />

<context:component-scan base-package="package.with.beans" /> <!-- scans components and adds <context:annotation-config /> -->

  • 在你的類的標籤豆定義init方法爲變量 「初始化方法」