如何使用 JSF2 获取 GET 参数? [英] How get GET parameters with JSF2?
问题描述
例如我有这个网址:
http://example.com?parameter=content
当用户单击此链接时,我应该能够获得内容"的参数值.我正在阅读 BalusC 教程但是是 JSF 1.2,我正在学习 JSF2.
When the user click in this link, then I should be able to get the parameter value which is 'content'. I was reading BalusC tutorial but is JSF 1.2 and I'm learning with JSF 2.
我怎么能这样做?
推荐答案
两种方式(两个示例都假设参数名称是您的问题中的 parameter
):
Two ways (both examples assume that the parameter name is parameter
as in your question):
使用
@ManagedProperty
在所需的 bean 属性上:
Use
@ManagedProperty
on the desired bean property:
@ManagedProperty("#{param.parameter}")
private String parameter;
这仅适用于请求范围的 bean,不允许细粒度的转换和验证.
This works on request scoped beans only and does not allow for fine grained conversion and validation.
Use <f:viewParam>
in the view pointing to the desired bean property:
<f:metadata>
<f:viewParam name="parameter" value="#{bean.parameter}" />
</f:metadata>
这也适用于视图范围的 bean,并允许使用标准验证器进行细粒度转换和验证,就像在普通输入组件上一样.它甚至允许附加
.
This works on view scoped beans as well and allows for fine grained conversion and validation using the standard validators like as on normal input components. It even allows for attaching a <h:message>
.
另见:
- JSF 2.0 中的通信 - 处理 GET 请求参数
- ViewParam vs @ManagedProperty(value = "#{param.id}")
这篇关于如何使用 JSF2 获取 GET 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!