我想在用戶第一次請求頁面時顯示錯誤消息。該錯誤是在請求後構建方法設定範圍內管理的bean如下所示:如何從請求作用域bean的PostConstruct方法顯示FacesMessage?
@RequestScoped
public class MyBean {
private String name;
@PostConstruct
public void init() {
// some validations here
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "You have no credit!");
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, message);
context.renderResponse();
}
public String getName() {
return name;
}
}
,然後在我的JSF頁面:
<!-- I'm expecting the error you have no credit will be displayed here -->
<h:messages />
<h:form>
<h:inputText value="#{myBean.name}" />
</h:form>
當處於發展階段運行中,JSF投訴這是一個未處理的消息:
"Project Stage[Development]: Unhandled Messages - You have no credit!"
你能幫我嗎?