ui:include值是在preRenderView之前评估的 [英] ui:include value is evaluated before preRenderView
问题描述
page1.xhtml
page1.xhtml
<h:body>
<h:link outcome="page2.xhtml>
<f:param name="id" value="1"/>
</hlink>
</h:body>
page2.xhtml
page2.xhtml
<h:body>
<f:metadata>
<f:event type="preRenderView" listener="#{myBean.init}"/>
</f:metadata>
<ui:include src="#{myBean.myString}"/>
</h:body>
MyBean.java
MyBean.java
public void init(ComponentsystemEvent e){
Map<String,String> params =
FacesContext.getExternalContext().getRequestParameterMap();
String myId = params.get("id");
int id = Integer.parseInteger(myId);
if(id==1)
setMyString = "myPage.xhtml";
}
当我从page1.xhtml导航到page2.xhtml时,我正在发送一个id作为参数,根据该ID,我将显示一个页面
While i am navigating from page1.xhtml to page2.xhtml i am sending an id as a parameter where according to this id i will display a page
问题是无法找到页面
我在控制台中打印的结果是,我发现它在进入preRenderView初始化之前正在评估getMyString(),为什么会这样发生
i am printing in console what's happening what i found is that it is evaluating getMyString() before going to preRenderView init so why is this happening like this
我还尝试了构造后,它在ManagedBean的资源注入中返回了错误
i also tried post construct it returned error in resource injection of managedBean
推荐答案
这是经典的视图构建时间与视图渲染时间的问题:<ui:include>
是一个标记处理程序,在视图构建时间进行评估,而<f:event type="preRenderView">
自然会在即将呈现的视图时被调用.如您所料,后一个事件发生在前一个事件之后,而您预期则相反.不过,当前一个标签要求评估其属性时,它肯定是null
或尚不存在.
That's a classic view build time vs view render time problem: <ui:include>
is a tag handler that's evaluated at view build time, while <f:event type="preRenderView">
naturally is called just when the view is about to be rendered. As you guess, the latter event happens after the former, while you expect it to be otherwise. Still, when the former tag requests to evaluate its attribute, it's definitely null
, or isn't there yet.
阅读经典的 JSF2 Facelets中的JSTL ...有意义吗? 以获得一个更好地了解了JSF生命周期的这两个阶段之间的关系.
Read the classic JSTL in JSF2 Facelets... makes sense? to get a better grasp at what's the relationship between these two phases of JSF lifecycle.
这篇关于ui:include值是在preRenderView之前评估的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!