Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to escape "<", "&" and ">" characters in JSF?  RSS feed

 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys

I have not quite figure out a way to do this basically what I am trying to accomplish is I have a dynamic datatable and I construct the datatable in the backing bean. I don't use <rich:datatable> or <rich:column> at all. I would like the text in some rows to display in different color so I have try



However, "<" was display as it is in web page and in the HTML source code "<" was translated to "<" so I know I need a way to escape "<"

I have also looked up



and have try either escapeHtml, escapeXml, unescapeHtml, unescapeXml methods like



still displays <span style="{color:red}">6</span> in the rows

anyone have any idea what I did wrong?

 
Jakim Rachev
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at this:
http://www.w3schools.com/tags/ref_entities.asp
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was never a good idea to have business logic generate raw HTML and JSF is especially insistent on keeping the distance between View, Model, and Controller.

A better solution than jamming in hard-wired HTML is to dynamically specify style class on the items in question.

For example:


pair this with a "smart" property:


This arrangement will evaluate to things like 'styleClass="basic red"'. Where "basic" is the CSS style class for all items (typically font size, typeface, etc. and "red" is simply a style that indicates that the item will be colored red. So depending on the value of the column named "total" in the datamodel, you'll get an item that's red (negative total), blue (zero total) or black (positive total).
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to the previous poster, your link didn't help me much.

I found my solution, it is <h:outputFormat escape="false">

for anyone that might be interested in the solution.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Davie Lin wrote:Sorry to the previous poster, your link didn't help me much.

I found my solution, it is <h:outputFormat escape="false">

for anyone that might be interested in the solution.


However, I repeat: Putting raw HTML display logic in the business application component is NOT a good idea!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!