何时使用f:viewAction/preRenderView与PostConstruct? [英] When to use f:viewAction / preRenderView versus PostConstruct?
问题描述
什么时候应该使用f:viewAction
或preRenderView
事件来初始化页面数据而不是使用@PostConstruct
注释?是基于后备bean的范围类型使用一个或另一个的基本原理,例如如果后备bean是@RequestScoped
,那么在渲染视图之前使用f:viewAction
或preRenderView
而不是@PostConstruct
来初始化后备bean的选择是否无关紧要,因为两者会产生相同的效果?>
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()
{
}
}
何时应该使用@PostConstruct批注使用f:viewAction或preRenderView事件初始化页面经文的数据?
如果要在呈现HTML之前执行方法,请使用<f:viewAction>
.如果要在更新模型值阶段基于<f:viewParam>
设置的模型值执行操作,这将特别有用.即,它们在@PostConstruct
运行时不可用.在JSF 2.0/2.1中,此标记不存在,您必须使用preRenderView
解决方法.
如果后备bean是@RequestScoped,它们是否有效地执行了完全相同的操作? (因此,这取决于开发人员的选择?(@ PostConstruct看起来更干净").
不,他们绝对做不到同样的事情. @PostConstruct
旨在在bean构建并设置所有注入的依赖项和托管属性(例如@EJB
,@Inject
,@ManagedProperty
等)之后直接执行 动作.即,注入的依赖项在bean的构造函数中不可用.因此,当bean在视图,会话或应用程序范围内时,每个视图,会话或应用程序仅运行一次.默认情况下,仅在初始GET请求上调用<f:viewAction>
,但也可以通过onPostback="true"
属性将其配置为在回发请求上调用.在每个HTTP请求上调用preRenderView
事件(是的,这还包括ajax请求!).
总结一下,如果要对注入的依赖项和托管属性执行操作,请使用@PostConstruct
,这些依赖项和托管属性是在bean的构造过程中由@EJB
,@Inject
,@ManagedProperty
等设置的.如果还想要对<f:viewParam>
设置的属性执行操作,请使用<f:viewAction>
.如果您仍在使用JSF 2.0/2.1,请使用preRenderView
而不是<f:viewAction>
.如有必要,您可以在FacesContext#isPostback()
上添加检查以仅对初始请求执行preRenderView
动作.
另请参见:
- 什么可以< f:metadata> ;、< f:viewParam>和< f:viewAction>用于吗?
- ViewParam与@ManagedProperty(值="## param.id}")
- 是否可以禁用f:event type ="preRenderView"回传中的侦听器?
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 or preRenderView
<f:metadata>
<f:viewAction action="#{myBean.initialize}" />
</f:metadata>
<f:metadata>
<f:event type="preRenderView" listener="#{myBean.initialize}"/>
</f:metadata>
or
@PostConstruct
public class MyBean
{
@PostConstruct
public void initialize()
{
}
}
When should one use the f:viewAction or preRenderView event to initialize data for a page verses using the @PostConstruct annotation?
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.
If the backing bean is @RequestScoped, do they effectively do the exact same thing? (and so then it is up to developer choice? (@PostConstruct seems "cleaner").
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!).
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.
See also:
- What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
- ViewParam vs @ManagedProperty(value = "#{param.id}")
- Is it possible to disable f:event type="preRenderView" listener on postback?
这篇关于何时使用f:viewAction/preRenderView与PostConstruct?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!