问题描述
与使用 @PostConstruct
注释相比,什么时候应该使用 f:viewAction
或 preRenderView
事件来初始化页面的数据?使用一种或另一种的基本原理是基于支持 bean 的范围类型,例如如果支持 bean 是 @RequestScoped
,那么会选择使用 f:viewAction
或 preRenderView
而不是 @PostConstruct
在呈现视图之前初始化您的支持 bean 无关紧要,因为两者会导致相同的效果?
When should one use the f:viewAction
or preRenderView
event to initialize data for a page versus using the @PostConstruct
annotation? Is the rationale to use one or the other based on the type of scope of the backing bean e.g. If the backing bean is @RequestScoped
, then would the choice of using f:viewAction
or preRenderView
over @PostConstruct
to initialize your backing bean prior to rendering the view be irrelevant as the two would result in the same effect?
f:viewAction 或 preRenderView
<f:metadata>
<f:viewAction action="#{myBean.initialize}" />
</f:metadata>
<f:metadata>
<f:event type="preRenderView" listener="#{myBean.initialize}"/>
</f:metadata>
或
@PostConstruct
public class MyBean
{
@PostConstruct
public void initialize()
{
}
}
推荐答案
如果您想在呈现 HTML 之前执行方法,请使用 .如果您想在更新模型值阶段根据
设置的模型值执行操作,这将特别有用.即,它们在
@PostConstruct
运行时不可用.在 JSF 2.0/2.1 中,此标记不存在,您必须使用 preRenderView
解决方法.
Use the <f:viewAction>
when you want to execute a method before the HTML is been rendered. This is particularly useful if you want to perform actions based on model values set by <f:viewParam>
during update model values phase. Namely, they are not available at the moment the @PostConstruct
runs. In JSF 2.0/2.1, this tag didn't exist and you have to use the preRenderView
workaround.
如果支持 bean 是@RequestScoped,它们是否有效地做完全相同的事情?(那么这取决于开发人员的选择?(@PostConstruct 看起来更干净").
不,他们绝对不能有效地做同样的事情.@PostConstruct
旨在在 bean 构建和设置所有注入的依赖项和托管属性(例如 @EJB
、)之后直接执行操作@Inject
、@ManagedProperty
等,即注入的依赖项在 bean 的构造函数中不可用.因此,当 bean 是视图、会话或应用程序范围时,每个视图、会话或应用程序只运行一次. 默认仅在初始 GET 请求时调用,但可以通过
onPostback="true"
属性配置为在回发请求时调用好.preRenderView
事件在每个 HTTP 请求上被调用(是的,这也包括 ajax 请求!).
No, they do definitely not effectively do the same thing. The @PostConstruct
is intented to perform actions directly after bean's construction and setting of all injected dependencies and managed properties such as @EJB
, @Inject
, @ManagedProperty
, etc. Namely, the injected dependencies are not available inside the bean's constructor. This will thus run only once per view, session or application when the bean is view, session or application scoped. The <f:viewAction>
is by default only invoked on initial GET request, but can via onPostback="true"
attribute be configured to be invoked on postback requests as well. The preRenderView
event is invoked on every HTTP request (yes, this also includes ajax requests!).
总结,如果您想对由@EJB
、@Inject
设置的注入依赖项和托管属性执行操作,请使用@PostConstruct
, @ManagedProperty
等在 bean 的构建过程中.如果您还想对由 设置的属性执行操作,请使用
.如果您仍在使用 JSF 2.0/2.1,请使用
preRenderView
而不是 .如有必要,您可以在
FacesContext#isPostback()
上添加检查,以仅对初始请求执行 preRenderView
操作.
Summarized, use @PostConstruct
if you want to perform actions on injected dependencies and managed properties which are set by @EJB
, @Inject
, @ManagedProperty
, etc during bean's construction. Use <f:viewAction>
if you also want to perform actions on properties set by <f:viewParam>
. If you're still on JSF 2.0/2.1, use preRenderView
instead of <f:viewAction>
. You can if necessary add a check on FacesContext#isPostback()
to perform the preRenderView
action on initial request only.
- 什么可以<f:metadata>、<f:viewParam>和 <f:viewAction>用于?
- ViewParam vs @ManagedProperty(value = "#{param.id}")
- 是否可以禁用 f:event type=preRenderView"?回传的监听器?
这篇关于何时使用 f:viewAction/preRenderView 与 PostConstruct?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!