< f:metadata&gt ;、< f:viewParam>可以做什么?和< f:viewAction>用于? [英] What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?

查看:136
本文介绍了< f:metadata&gt ;、< f:viewParam>可以做什么?和< f:viewAction>用于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以阐明我们如何在一般示例或实际示例中使用此代码段吗?

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参数

<f:viewParam> 管理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获取请求参数值.
  • 如有必要,对其进行转换和验证(您可以使用requiredvalidatorconverter属性,并像在<h:inputText>中一样在其中嵌套<f:converter><f:validator>)
  • 如果转换和验证成功,则将其设置为由#{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 and converter 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 the value attribute is absent, then set it as request attribtue on name id so that it's available by #{id} in the view.

因此,当您以foo.xhtml?id=10的形式打开页面时,就在呈现视图之前,以这种方式在Bean中设置了参数值10.

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:viewAction> .


Performing business action on GET parameters

You can use the <f:viewAction> for this.

<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() {
    // ...
}

然而,<f:viewAction>是JSF 2.2以来的新功能(<f:viewParam>自JSF 2.0起就已经存在).如果您无法升级,那么最好的选择是使用 <f:event> 代替.

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>本质上是一种变通方法/破解,这就是<f:viewAction>在JSF 2.2中引入的原因.

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在相同的参数上同时具有 一个<f:viewParam>,否则将不会通过.

This approach only requires that next.xhtml has also a <f:viewParam> on the very same parameter, otherwise it won't be passed through.

<f:viewParam>也可以与普通HTML" GET表单结合使用.

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);
}

请注意,<h:message>用于<f:viewParam>,而不是纯HTML <input type="text">!还要注意,当#{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).

  • ViewParam vs @ManagedProperty(value = "#{param.id}")
  • Communication in JSF 2.0 - Processing GET request parameters

这篇关于&lt; f:metadata&gt ;、&lt; f:viewParam&gt;可以做什么?和&lt; f:viewAction&gt;用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆