ViewParam 与 @ManagedProperty(value = "#{param.id}")
有什么区别:
<f:metadata>
<f:viewParam name="id" value="#{someBean.id}"/>
</f:metadata>
像这样定义视图参数和像这样在 ManagedBean 中定义属性
@Inject @ManagedProperty(value = "#{param.id}")
private Integer id;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
:仅在更新模型值阶段设置值(因为它扩展了
UIInput
)。设置值在
@期间不可用PostConstruct
,因此您需要额外的3.0/vdldoc/f/metadata" rel="nofollow noreferrer">
根据设置的值进行初始化/预加载。从 JSF 2.2 开始,您可以使用
来代替。允许嵌套
和
进行更细粒度的转换/验证。甚至是
可以附加。可以使用
或includeViewParams=true
任何 URL 中的请求参数。可用于
@RequestScoped< /code>
bean,但它要求 bean 为
组件,因为@ViewScoped
如果您希望视图参数能够承受由视图中包含的表单导致的任何验证失败,否则您需要手动保留
中的 < code>UICommand
将在每个请求上设置。示例:
带有
@FacesConverter("userConverter")
。通过 http://example.com/context/user.xhtml?id=123 调用页面 将通过转换器传递id
参数,并将User
对象设置为bean 属性。@ManagedProperty
:在 bean 构造后立即设置值。
设置值在
@PostConstruct
允许根据设置值轻松初始化/预加载其他属性。
是否不允许允许在视图中进行声明性转换/验证。
不支持自动包含在结果目标 URL 中。
可以在任何范围的 bean 上使用,但它只会在 bean 构造期间设置,而不是在每个请求时设置。
示例:
请注意,只要
user
为null
,您就必须通过摆弄FacesContext#addMessage ()
在@PostConstruct
方法中。另请注意,当#{param.id}
不是有效数字时,在命中@PostConstruct
之前将引发异常。如果您想处理它,那么您最好将其设为私有字符串id
。但更好的是使用
。当
可以同时使用它们>@PostConstruct
和includeViewParams
是必需的。您将无法再应用细粒度的转换/验证。另请参阅:
<f:viewParam>
:Sets the value during update model values phase only (since it extends
UIInput
).The set value is not available during
@PostConstruct
, so you need an additional<f:event type="preRenderView" listener="#{bean.init}" />
inside the<f:metadata>
to do initialization/preloading based on the set values. Since JSF 2.2 you could use<f:viewAction>
for that instead.Allows for nested
<f:converter>
and<f:validator>
for more fine-grained conversion/validation. Even a<h:message>
can be attached.Can be automatically included as GET query string in outcome target URLs using
includeViewParams
attribute of<h:link>
orincludeViewParams=true
request parameter in any URL.Can be used on a
@RequestScoped
bean, but it requires the bean to be@ViewScoped
if you want the view parameters to survive any validation failures caused by forms enclosed in the view, otherwise you need to manually retain all request parameters for the subsequent requests by<f:param>
in theUICommand
components, because the<f:viewParam>
will be set on every request.Example:
with
and an
@FacesConverter("userConverter")
. Invoking page by http://example.com/context/user.xhtml?id=123 will pass theid
parameter through the converter and set theUser
object as a bean property.@ManagedProperty
:Sets the value immediately after bean's construction.
Set value is available during
@PostConstruct
which allows easy initialization/preloading of other properties based on the set value.Does not allow for declarative conversion/validation in view.
Does not support being automatically included in outcome target URLs.
Can be used on a bean of any scope, but it will be set only during bean's construction instead of on every request.
Example:
Do note that you have to manage conversion and validation yourself whenever
user
isnull
by fiddling withFacesContext#addMessage()
inside the@PostConstruct
method. Also note that when#{param.id}
is not a valid number, then an exception will be thrown before@PostConstruct
is hit. If you want to deal with it, then you'd probably better make it aprivate String id
. But much better is to use<f:viewParam>
.You can use them both when both
@PostConstruct
andincludeViewParams
are mandatory. You only won't be able to apply fine-grained conversion/validation anymore.See also:
2 个其他区别:
@ManagedProperty
仅适用于由 JSF 管理的 Bean,不适用于由 CDI 管理的 Bean (@Named
);
仅适用于 GET 请求的参数。2 other differences:
@ManagedProperty
is usable only with beans managed by JSF, not with beans managed by CDI (@Named
);<f:viewParam>
works only with parameters of GET requests.