仅在页面加载时处理 f:viewParam [英] Process f:viewParam only on page load

查看:19
本文介绍了仅在页面加载时处理 f:viewParam的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 传递参数,如下所示.

I'm using an <f:viewParam> to pass a parameter as follows.

<ui:define name="metaData">
    <f:metadata>
        <f:viewParam name="id" value="#{bean.entity}" converter="#{converter}"/>
    </f:metadata>
</ui:define>

是否可以仅在页面加载/刷新时处理此?

Is it possible to process this <f:viewParam>, only when the page is loaded/refreshed?

这只是因为使用 指定的转换器将通过查询字符串传递的值转换为 JPA 实体的成本很高.因此,它涉及昂贵的数据库事务,即使在使用 <p:commandButton><p:commandLink> 等组件进行 ajaxical 回发时> 这是不必要的.

It is just because the converter as specified with the <f:viewParam> is costly that converts the value passed through the query-string to a JPA entity. Hence, it involves an expensive database transaction, even when doing ajaxical postbacks using components like <p:commandButton>, <p:commandLink> which is unnecessary.

因此,例如,当点击 (ajaxical) 时,不应执行昂贵的业务服务(在转换器中).可以这样做吗?

So, when for example, a <p:commandLink> (ajaxical) is clicked, the expensive business service (in the converter) should not be executed. Can this be done?

rendered 属性针对 facesContext.postback 进行评估时,如 rendered="#{not facesContext.postback}" 但属性 rendered未记录.因此,它是不可靠的.

This somehow works (strange enough nevertheless), when the rendered attribute is evaluated against facesContext.postback like rendered="#{not facesContext.postback}" but the attribute rendered is not documented. Hence, it is unreliable.

推荐答案

您可以通过创建扩展 <f:viewParam> 的自定义标记来实现此目的,其中您将提交的值存储为实例变量它不存储在 JSF 视图状态中,而不是像 <f:viewParam> 默认那样存储在 JSF 视图状态中.请求结束时,所有 UI 组件实例都将被销毁.它们在请求开始时重新创建.当提交的值为 null 时,它不会调用转换器或模型设置器.Arjan Tijms 的博客.

You can achieve this by creating a custom tag extending <f:viewParam> wherein you store the submitted value as an instance variable which isn't stored in JSF view state instead of in the JSF view state as the <f:viewParam> by default does. By end of request, all UI component instances are destroyed. They are recreated in beginning of the request. When submitted value is null, then it won't call the converter nor the model setter. This all is elaborated in Arjan Tijms' blog.

OmniFaces 自 1.0 版以来就已经提供了一个具有 ,另见 我自己的博客.根据您的问题历史记录,您已经在使用 OmniFaces,因此您基本上需要做的就是将 f: 替换为 o:.

OmniFaces offers already since version 1.0 a ready to use solution in flavor of <o:viewParam>, see also my own blog on that. Based on your question history, you're already using OmniFaces, so all you basically need to do is to replace f: by o:.

<ui:define name="metaData">
    <f:metadata>
        <o:viewParam name="id" value="#{bean.entity}" converter="#{converter}"/>
    </f:metadata>
</ui:define>

这不会在同一视图的回发期间调用模型设置器(也不会调用转换器).

This won't call the model setter (nor the converter) during postbacks on the same view.

这以某种方式起作用(尽管如此很奇怪),当渲染的属性针对 facesContext.postback 进行评估时,如 render="#{not facesContext.postback}" 但未记录渲染的属性.因此,它是不可靠的.

那是因为<f:viewParam> 本质上是一个 UIInput 组件(否则它将无法执行转换、验证、模型更新和所有像通常的输入组件一样的东西),因此它只是一个 UIComponent 支持 渲染 属性.然而,这没有明确记录,因为它实际上不会向 HTML 输出呈现任何内容(这也是为什么它是 f:xxx,而不是 h:xxx).但是使用此属性,您实际上可以控制回发期间的行为,因为此属性是 也在processDecodes() 在应用请求值阶段调用的方法.

That's because the <f:viewParam> is in essence an UIInput component (else it wouldn't be able to perform conversion, validation, model-update and all that stuff like usual input components) which is thus just an UIComponent supporting a rendered attribute. This is however not explicitly documented as it actually doesn't render anything to the HTML output (that's also why it's a f:xxx, not a h:xxx). But with this attribute you can actually control the behavior during postback as this attribute is also evaluated in processDecodes() method which is invoked during apply request values phase.

这篇关于仅在页面加载时处理 f:viewParam的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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