PF 3.5.10,Mojarra 2.1.21,Omnifaces 1.5
如何在加载.xhtml JSF页面之前调用特殊的init() - 某些(CDI)SessionScoped bean的方法?现在我调用init(),如果用户从站点菜单中选择页面(带p:menutitem).但是如果用户使用浏览器地址行直接输入url该怎么办?
编辑my.xhtml::
<ui:define template="/mytemp.xhtml">
<f:event type="preRenderView" listener="#{mybean.init()}" />
<h:form>
<p:commandButton update="@form" ... />
</h:form>
</ui:define>
Run Code Online (Sandbox Code Playgroud)
如果我这样做,init()将在每次更新时调用(即每次回发到服务器),例如每次单击commandButton时.所以我不能使用你的提议.
编辑2:谢谢Luiggi Mendoza和BalusC!除了Luiggi Mendoza的解决方案之外,正如评论所述,Omnifaces 1.6也将拥有ViewScope.
问题是在@PostConstruct public void init()创建托管bean并注入字段后调用该方法.由于你的bean是@SessionScoped,它将一直存在直到用户会话到期.
一种解决方法是使用<f:event type="preRenderView" listener="{bean.init}" />如下所述:<f:metadata>,<f:viewParam>和<f:viewAction>可以用于什么?(不需要<f:metadata>像BalusC在这里解释的那样:是否将f:event中的事件放在f:元数据中是否重要?).
根据您的问题更新,此问题在第一个链接中处理.我将发布相关代码来处理这种情况(取自BalusC答案):
public void init() {
FacesContext facesContext = FacesContext.getCurrentInstance();
if (!facesContext.isPostback() && !facesContext.isValidationFailed()) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
如果您迁移到JSF 2.2,那么就有一个@ViewScopedCDI bean 的注释,您可以相应地减少@SessionScopedbean 的范围.
| 归档时间: |
|
| 查看次数: |
14181 次 |
| 最近记录: |