在一个普通的HTML属性使用JSF EL [英] Using JSF EL in a plain HTML attribute

查看:481
本文介绍了在一个普通的HTML属性使用JSF EL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用JSF EL一个HTML标记内?例如,一个普通的HTML内< TD> 元素,我们可以使用EL #{bean.color} 的在的bgcolor 属性?

 < TD BGCOLOR =#{} bean.color>


解决方案

问题的答案取决于JSF的版本,使用的视图技术。你要找的是模板文本使用EL(即没有任何标记/组件内)。

的技术术语

根据你的问题记录你使用JSF 1.2在WebSphere上。我假设你还在使用旧的JSP,Facelets的的predecesor。无论是JSF EL #{} 模板文字工作取决于所使用的JSP版本。 JSP版齐头并进从Servlet版本。

当您的容器支持的Servlet 2.5和的web.xml 声明符合Servlet 2.5的,然后你使用JSP 2.1。在这种情况下,你可以在JSP中使用#{豆} 。 JSF的EL #{} 从JSF 1.1统一EL的名义下,即转移到JSP 2.1。

 < TD BGCOLOR =#{} bean.color>

然而,当你的容器最多支持Servlet 2.4的,那么你基本上是使用JSP 2.0,你必须使用 $ {豆} 代替。

 < TD BGCOLOR =$ {} bean.color>

这只有一个prerequirement:在同一文件中,某处的之前在这里您可以通过 $引用JSF豆上面的行{豆} ,你需要确保你已经的的由在JSF标签事先#{豆} 引用非常相同的豆,否则豆将不会是precreated。

当你使用JSP的继任 Facelets的,即使在Servlet 2.4环境,那么你可以只用

 < TD BGCOLOR =#{} bean.color>

参见:


无关的问题,的bgcolor 属性的德precated 在HTML中。你应该使用CSS 风格属性来代替。

 < TD风格=背景:#{} bean.color>

即便如此,上述被认为是穷人的做法。把CSS中,你通过包括的.css 样式表文件<链接> / &LT ; H:outputStylesheet> 和使用合理的类名(例如: .odd 。即使 .success .cancelled 等)和渲染的CSS样式类来代替。例如,如果颜色取决于一些状态:

 < TD类=#{} bean.status>

Can we use JSF EL inside a HTML tag? For example, inside a plain HTML <td> element, can we use EL #{bean.color} for the bgcolor attribute?

<td bgcolor="#{bean.color}">

解决方案

The answer depends on the JSF version and the view technology used. The technical term you're looking for is "using EL in template text" (i.e. not inside any tag/component).

As per your question history you're using JSF 1.2 on Websphere. I assume that you're still using old JSP, the predecesor of Facelets. Whether JSF EL #{} works in template text depends on the JSP version used. JSP version goes hand in hand with Servlet version.

When your container supports Servlet 2.5 and the web.xml is declared conform Servlet 2.5, then you're using JSP 2.1. In that case, you can just use #{bean} in JSP. The JSF EL #{} was namely moved from JSF 1.1 to JSP 2.1 under the name "unified EL".

<td bgcolor="#{bean.color}">

However, when your container supports at most Servlet 2.4, then you're basically using JSP 2.0 and you have to use ${bean} instead.

<td bgcolor="${bean.color}">

This has only one prerequirement: in the same document, somewhere before the above line where you reference the JSF bean by ${bean}, you need to ensure that you've already referenced the very same bean by #{bean} in a JSF tag beforehand, otherwise the bean won't be precreated.

When you're using the JSP's successor Facelets, even though in a Servlet 2.4 environment, then you can just use

<td bgcolor="#{bean.color}">

See also:


Unrelated to the problem, the bgcolor attribute is deprecated in HTML. You should be using CSS style attribute instead.

<td style="background: #{bean.color}">

Even then, the above is considered poor practice. Put CSS in a .css stylesheet file which you include via <link>/<h:outputStylesheet> and use sensible classnames (e.g. .odd, .even, .success, .cancelled, etc) and render a CSS style class instead. For example, if the color depends on some status:

<td class="#{bean.status}">

这篇关于在一个普通的HTML属性使用JSF EL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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