在JSF页面中定义和重用EL变量 [英] Defining and reusing an EL variable in JSF page

查看:92
本文介绍了在JSF页面中定义和重用EL变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在EL表达式中定义变量并在以后重新使用该变量?

Is it possible to define variable and reuse the variable later in EL expressions ?

例如:

<h:inputText 
   value="#{myBean.data.something.very.long}"
   rendered="#{myBean.data.something.very.long.showing}"
/>

我的想法是:

<!-- 
     somehow define a variable here like : 
     myVar = #{myBean.data.something.very.long} 
-->
<h:inputText 
   value="#{myVar}"
   rendered="#{myVar.showing}"
/>

有什么想法吗?谢谢!

推荐答案

您可以使用

You can use <c:set> for this:

<c:set var="myVar" value="#{myBean.data.something.very.long}" scope="request" />

该EL表达式随后将被评估一次并存储在请求范围内.请注意,这仅在查看构建时间期间可用时有效.如果不是这种情况,那么您需要删除scope属性,以使其成为真正的别名":

This EL expression will then be evaluated once and stored in the request scope. Note that this works only when the value is available during view build time. If that's not the case, then you'd need to remove the scope attribtue so that it becomes a true "alias":

<c:set var="myVar" value="#{myBean.data.something.very.long}" />

因此请注意,这不会将请求值缓存在请求范围内!每次都会重新评估.

Note thus that this does not cache the evaluated value in the request scope! It will be re-evaluated everytime.

不要不要使用<ui:param>.如果不使用该参数将参数传递给<ui:composition><ui:decorate>中定义的template,从而实质上滥用了该参数,则该行为是未指定的,实际上,这在JSF实现中是一个错误.如果可能的话使用.绝不应该依赖这一点.另请参见 JSF2 Facelets中的JSTL ...有意义吗?

Do NOT use <ui:param>. When not used in order to pass a parameter to the template as defined in <ui:composition> or <ui:decorate>, and thus in essence abusing it, then the behavior is unspecified and in fact it would be a bug in the JSF implementation being used if it were possible. This should never be relied upon. See also JSTL in JSF2 Facelets... makes sense?

这篇关于在JSF页面中定义和重用EL变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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