【问题标题】:PostConstruct Not called jsfPostConstruct 不叫 jsf
【发布时间】:2015-01-16 03:45:58
【问题描述】:

我有一个只包含命令链接的 jsf 页面。我的支持 bean 有一个后构造方法。当我单击命令链接时,我的操作方法被调用但不调用后构造方法。这是代码..

Xhtml 页面:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">

    <h:body>
        <ui:composition template="CommonLayout.xhtml" >
            <ui:define name="title">
                Final Page
            </ui:define>
            <ui:define name="content">

                <h:graphicImage value="resources\images\arigatougozaimashita.gif" />

                <h2 style="text-align: center; padding-bottom: 25px; padding-top: 100px">Thanks !!!</h2>

                <h:form style="text-align: center;padding-bottom: 100px; padding-top: 25px">
                    Click <h:commandLink value="here" action="#{finalPage.action}" /> to return to First Login page.
                </h:form>

            </ui:define>
        </ui:composition>
    </h:body>
</html>

支持 bean:

package com.mycompany.newyearchallenge2015;

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named(value = "finalPage")
@ViewScoped
public class FinalPage implements Serializable {

    /**
     * Constructor
     */

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

    public FinalPage() {
    }

    /**
     * @Parameters : No parameters
     * @return : String which tells what's the next page to visit
     * @Description: Called on clicking the next button.
     */
    public String action() {

        return "LoginScreen1?faces-redirect=true";

    }

}

有人知道为什么吗???

【问题讨论】:

  • 除非存在一些隐蔽的设计问题,并且如果您需要在每个 HTTP 请求上调用 init() 方法(由 @PostConstruct 注释),您需要使用 @RequestScoped 指定您的 bean一个请求范围的bean。由@PostConstruct 修饰的方法在 bean 投入使用之前被调用 - 只有在 bean 被创建(因此调用构造函数)并且依赖注入发生(如果有)之后才调用一次。

标签: jsf


【解决方案1】:

我猜这是因为 bean 是 ViewScoped 并且在收到包含 onClick 消息的请求之前已经创建了 bean。

如果它是请求范围的,我希望它的行为更像你所期望的。

【讨论】:

    【解决方案2】:

    @ViewScoped 仅在您重新显示同一视图时才会存在。 当您重定向到另一个视图时,@ViewScoped bean 将在渲染响应阶段结束时被销毁。 我猜你只在你指定的那个视图上使用finalPage bean,并且在重定向到LoginScreen1 视图之后你正在使用不同的bean。

    【讨论】:

      猜你喜欢
      • 2011-09-30
      • 1970-01-01
      • 2011-07-14
      • 2012-12-18
      • 2015-02-26
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      相关资源
      最近更新 更多