什么时候使用 preRenderView 和 viewAction? [英] When to use preRenderView versus viewAction?

查看:15
本文介绍了什么时候使用 preRenderView 和 viewAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与使用 viewAction 相比,什么时候应该使用 preRenderView 事件来初始化页面的数据?它们在使用上是否相同并且它们具有相同的效果?

When should one use the preRenderView event to initialize data for a page versus using the viewAction? Are they equal in use and do they have the same effect?

preRenderView 事件

<f:metadata>
  <f:event type="preRenderView" listener="#{myBean.initialize}"/>
</f:metadata>

viewAction

<f:metadata>
  <f:viewAction action="#{myBean.initialize}"/>
</f:metadata>

推荐答案

在实践中,它们可以用来实现相同的效果,但是 viewAction(JSF2.2 的新功能)附带了以下内容增强功能:

In practice, they can be used to achieve the same effect, but viewAction (new with JSF2.2) comes with the following enhancements:

  1. onPostback:viewAction 带有此属性,允许您指定是否要在回发时执行操作相同的视图(即页面刷新或按钮提交等).它默认为 false,因此如果您不需要,您甚至不必指定它.要使用 preRenderView 实现相同的效果,您需要

  1. onPostback: viewAction comes with this attribute that allows you to specify whether you want the action to be executed on postback to the same view (that is, on page refresh or button submit etc). It defaults to false, so you don't even have to specify it if you don't need to. To achieve the same effect with preRenderView, you'll need

 <f:metadata>
     <f:event type="preRenderView" rendered="#{facesContext.postBack}" listener="#{myBean.initialize}"/>
 </f:metadata>

  • phase:此属性允许您指定在特定 JSF 阶段执行的操作.它默认为 INVOKE_APPLICATION,但所有其他 JSF 阶段 ID 在这里都有效.

  • phase: this attribute allows you to specify that the action be executed during a specific JSF phase. It defaults to INVOKE_APPLICATION, but all the other JSF Phase Ids are valid here.

    if:此属性允许您提供计算结果为布尔结果的值表达式.视图操作只会在此表达式的结果上执行.

    if: This attribute allows you to supply a value expression that evaluates to a boolean result. The view action will only be executed on the outcome of this expression.

    immediate:此属性现在允许在 APPLY_REQUEST_VALUES 期间执行 viewAction> 阶段(相对于默认的 INVOKE_APPLICATION 阶段),使其表现得像一个常规的 UIComponent

    immediate: This attribute now grants the ability for a viewAction to be executed during the APPLY_REQUEST_VALUES phase (as against the default INVOKE_APPLICATION phase), allowing it to behave like a regular UIComponent

    总的来说,viewAction 是一种更简洁的设计方法来执行视图命令.

    Overall, the viewAction is a cleaner design approach to carrying out view commands.

    这篇关于什么时候使用 preRenderView 和 viewAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆