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

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

问题描述

我正在使用<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>

仅在加载/刷新页面时才可以处理此<f:viewParam>吗?

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

这仅仅是因为<f:viewParam>指定的转换器成本很高,它将通过查询字符串传递的值转换为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.

因此,例如,当单击<p:commandLink>( 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="#{not facesContext.postback}"这样对facesContext.postback评估rendered属性,但是属性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视图状态存储在JSF视图状态下,默认为<f:viewParam>.在请求结束时,将销毁所有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 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评估渲染的属性时,就像rendered =#{not facesContext.postback}"一样,但是未记录渲染的属性.因此,这是不可靠的.

那是因为 <f:viewParam> 本质上是 UIInput 组件(否则它将无法执行转换,验证,模型更新以及诸如普通输入组件之类的所有工作),因此它只是一个

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天全站免登陆