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

查看:22
本文介绍了在 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.

不要不要使用.当不用于将参数传递给 中定义的 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天全站免登陆