在RequestScoped中没有调用PostConstruct方法

时间:2015-05-17 16:52:59

标签: java eclipse tomcat jsf-2

我希望每次刷新@PostConstruct bean中的页面时都会调用@RequestScoped方法。但由于某种原因,它并没有。默认构造函数也不是。

我尝试使用filter来禁用cache,并且我看到它的doFilter方法被调用,但它没有效果。

这是代码:

@ManagedBean
@RequestScoped
public class SearchResults implements Serializable {

    public SearchResults() {
        System.out.println("searchResults default constructor called");
    }

    @PostConstruct
    public void init() {
        System.out.println("SearchResults.init CALLED");
    }
}

过滤器的相关方法:

public void doFilter(ServletRequest request, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    System.out.println("DisableCacheFilter.doFilter CALLED");

    HttpServletResponse response = (HttpServletResponse) res;
    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
    response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
    response.setDateHeader("Expires", 0); // Proxies.

    chain.doFilter(request, response);
}

我错过了什么?

编辑:这就是我所遗漏的:我将annotation中的.javaSessionScoped更改为RequestScoped,但将ManagedBean作为会话范围在faces-config。一旦我改变它,它开始按预期工作:

<managed-bean>
    <managed-bean-name>searchResults</managed-bean-name>
    <managed-bean-class>com.app.pages.SearchResults</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

0 个答案:

没有答案