我们可以在 HTML 标签中使用 JSF EL 吗?例如,在纯 HTML <td> 元素中,我们可以将 EL #{bean.color} 用于 bgcolor 属性吗?
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}">
答案取决于 JSF 版本和使用的视图技术.您正在寻找的技术术语是在模板文本中使用 EL"(即不在任何标签/组件内).
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).
根据您的问题历史记录,您在 Websphere 上使用 JSF 1.2.我假设您仍在使用旧的 JSP,它是 Facelets 的前身.JSF EL #{} 是否适用于模板文本取决于所使用的 JSP 版本.JSP 版本与 Servlet 版本齐头并进.
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.
当您的容器支持 Servlet 2.5 并且 web.xml 被声明为符合 Servlet 2.5 时,那么您使用的是 JSP 2.1.在这种情况下,您可以在 JSP 中使用 #{bean}.JSF EL #{} 即以统一 EL"的名称从 JSF 1.1 移至 JSP 2.1.
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}">
但是,当您的容器最多支持 Servlet 2.4 时,您基本上使用的是 JSP 2.0,您必须改用 ${bean}.
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}">
这只有一个前提条件:在同一个文档中,在上面的某处通过 ${bean} 引用 JSF bean 的行,你需要确保您已经已经在 JSF 标记中事先通过 #{bean} 引用了同一个 bean,否则不会预先创建 bean.
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.
当您使用 JSP 的继任者 Facelets 时,甚至虽然在 Servlet 2.4 环境中,但您可以使用
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}">
与问题无关,bgcolor 属性在 HTML 中已弃用.您应该改用 CSS style 属性.
Unrelated to the problem, the bgcolor attribute is deprecated in HTML. You should be using CSS style attribute instead.
<td style="background: #{bean.color}">
即便如此,上述做法仍被认为是糟糕的做法.将 CSS 放入您通过 <link>/<h:outputStylesheet> 包含的 .css 样式表文件中,并使用合理的类名(例如.odd、.even、.success、.cancelled 等)并改为渲染 CSS 样式类.例如,如果颜色取决于某些状态:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Angular 2:在本地 .json 文件中找不到文件Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
使用模式 Angular 2 进行输入验证Input validation with pattern Angular 2(使用模式 Angular 2 进行输入验证)
如何在角度2中动态更改css类名How to change the css class name dynamically in angular 2(如何在角度2中动态更改css类名)
如何删除输入类型中的默认颜色?How to remove default color in input type?(如何删除输入类型中的默认颜色?)
如何将点击事件添加到打字稿中动态添加的html元How to add click event to dynamically added html element in typescript(如何将点击事件添加到打字稿中动态添加的html元素)
XPath 具有条件的多个属性值之一XPath one of multiple attribute values with condition(XPath 具有条件的多个属性值之一)