【发布时间】: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