6

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}">
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
greg
  • 237
  • 1
  • 4
  • 8

3 Answers3

7

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}">
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You're welcome. In the future questions please take care that you mention the exact JSF impl/version used (e.g. "Mojarra 1.2_15") and the view technology used (e.g. JSP or Facelets) and preferably also the servletcontainer make/version used. – BalusC Jun 08 '11 at 18:48
2

You can integrate el with html in facelets.

example:

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

example 2:

<script type="text/javascript">
    window.location = '#{bean.url}';
</script>
Dave Maple
  • 8,102
  • 4
  • 45
  • 64
1

It depends. If you're using Facelets as ViewHandler, yes. If you're using JSP, HTML must be in <f:verbatim> tag.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Cosmin Cosmin
  • 1,526
  • 1
  • 16
  • 34
  • Putting HTML in `` is only necessary for JSF 1.0/1.1. In JSF 1.2 that's not necessary anymore. As per the question history, OP is using JSF 1.2. – BalusC Jun 08 '11 at 18:36
  • 1
    Uh, just by experience :) No kidding, the view handler has been changed in JSF 1.2. Even more, that tag is deprecated in JSF 2.0. You may find this an interesting read: http://stackoverflow.com/questions/3623911/what-are-the-main-disadvantages-of-java-server-faces-2-0/3646940#3646940 – BalusC Jun 08 '11 at 19:28