问题描述
谁能解释一下我们一般如何使用这个片段,或者在现实世界中如何使用这个片段?
Can anyone clarify how we can use in general, or a in real world example, this snippet?
<f:metadata>
<f:viewParam id="id" value="#{bean.id}" />
<f:viewAction action="#{bean.init}" />
</f:metadata>
推荐答案
处理GET参数
管理 GET 参数的设置、转换和验证.就像
<h:inputText>
,但是对于 GET 参数.
Process GET parameters
The <f:viewParam>
manages the setting, conversion and validation of GET parameters. It's like the <h:inputText>
, but then for GET parameters.
下面的例子
<f:metadata>
<f:viewParam name="id" value="#{bean.id}" />
</f:metadata>
基本上执行以下操作:
- 通过名称
id
获取请求参数值. - 如有必要,转换并验证它(您可以使用
required
、validator
和converter
属性并嵌套一个<f:转换器>
和就像
)
- 如果转换和验证成功,则将其设置为由
#{bean.id}
值表示的 bean 属性,或者如果value
属性不存在,则设置它作为名称id
上的请求属性,以便它在视图中通过#{id}
可用.
- Get the request parameter value by name
id
. - Convert and validate it if necessary (you can use
required
,validator
andconverter
attributes and nest a<f:converter>
and<f:validator>
in it like as with<h:inputText>
) - If conversion and validation succeeds, then set it as a bean property represented by
#{bean.id}
value, or if thevalue
attribute is absent, then set it as request attribtue on nameid
so that it's available by#{id}
in the view.
因此,当您以 foo.xhtml?id=10
的形式打开页面时,参数值 10
以这种方式在 bean 中设置,就在视图呈现之前.
So when you open the page as foo.xhtml?id=10
then the parameter value 10
get set in the bean this way, right before the view is rendered.
至于验证,以下示例将参数设置为 required="true"
并仅允许 10 到 20 之间的值.任何验证失败都会导致显示一条消息.
As to validation, the following example sets the param to required="true"
and allows only values between 10 and 20. Any validation failure will result in a message being displayed.
<f:metadata>
<f:viewParam id="id" name="id" value="#{bean.id}" required="true">
<f:validateLongRange minimum="10" maximum="20" />
</f:viewParam>
</f:metadata>
<h:message for="id" />
对 GET 参数执行业务操作
<f:metadata>
<f:viewParam id="id" name="id" value="#{bean.id}" required="true">
<f:validateLongRange minimum="10" maximum="20" />
</f:viewParam>
<f:viewAction action="#{bean.onload}" />
</f:metadata>
<h:message for="id" />
与
public void onload() {
// ...
}
然而自 JSF 2.2 以来是新的(
自 JSF 2.0 以来已经存在).如果您无法升级,那么您最好的选择是使用
代替.
The <f:viewAction>
is however new since JSF 2.2 (the <f:viewParam>
already exists since JSF 2.0). If you can't upgrade, then your best bet is using <f:event>
instead.
<f:event type="preRenderView" listener="#{bean.onload}" />
然而,这是在每个请求时调用的.您需要明确检查请求是否不是回发:
This is however invoked on every request. You need to explicitly check if the request isn't a postback:
public void onload() {
if (!FacesContext.getCurrentInstance().isPostback()) {
// ...
}
}
当您想跳过转换/验证失败"时情况一样,然后执行以下操作:
When you would like to skip "Conversion/Validation failed" cases as well, then do as follows:
public void onload() {
FacesContext facesContext = FacesContext.getCurrentInstance();
if (!facesContext.isPostback() && !facesContext.isValidationFailed()) {
// ...
}
}
使用 <f:event>
这种方式本质上是一种解决方法/hack,这正是在 JSF 2.2 中引入 <f:viewAction>
的原因.
Using <f:event>
this way is in essence a workaround/hack, that's exactly why the <f:viewAction>
was introduced in JSF 2.2.
您可以通过"通过将 includeViewParams
属性设置为 true
或通过添加 includeViewParams=true
请求参数来获取导航链接中的视图参数.
You can "pass-through" the view parameters in navigation links by setting includeViewParams
attribute to true
or by adding includeViewParams=true
request parameter.
<h:link outcome="next" includeViewParams="true">
<!-- Or -->
<h:link outcome="next?includeViewParams=true">
用上面的<f:metadata>
示例生成基本上如下链接
which generates with the above <f:metadata>
example basically the following link
<a href="next.xhtml?id=10">
使用原始参数值.
这种方法只要求next.xhtml
也在完全一样的参数,否则不会通过.
This approach only requires that next.xhtml
has also a <f:viewParam>
on the very same parameter, otherwise it won't be passed through.
也可以与plain HTML"结合使用.获取表单.
The <f:viewParam>
can also be used in combination with "plain HTML" GET forms.
<f:metadata>
<f:viewParam id="query" name="query" value="#{bean.query}" />
<f:viewAction action="#{bean.search}" />
</f:metadata>
...
<form>
<label for="query">Query</label>
<input type="text" name="query" value="#{empty bean.query ? param.query : bean.query}" />
<input type="submit" value="Search" />
<h:message for="query" />
</form>
...
<h:dataTable value="#{bean.results}" var="result" rendered="#{not empty bean.results}">
...
</h:dataTable>
基本上使用这个 @RequestScoped
bean:
With basically this @RequestScoped
bean:
private String query;
private List<Result> results;
public void search() {
results = service.search(query);
}
请注意, 是用于
,而不是纯 HTML
!另请注意,当
#{bean.query}
为空时,输入值会显示 #{param.query}
,因为否则提交的值根本不会显示验证或转换错误.请注意,此构造对于 JSF 输入组件无效(它已经在幕后"这样做了).
Note that the <h:message>
is for the <f:viewParam>
, not the plain HTML <input type="text">
! Also note that the input value displays #{param.query}
when #{bean.query}
is empty, because the submitted value would otherwise not show up at all when there's a validation or conversion error. Please note that this construct is invalid for JSF input components (it is doing that "under the covers" already).
这篇关于<f:metadata>、<f:viewParam>和 <f:viewAction>用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!