• Post Reply Bookmark Topic Watch Topic
  • New Topic

evaluate ${variable} where variable contains ${}

 
Maciej Grula
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have an article tag that reads articles from database. I want to use jsp variables in the articles body. But when the page is rendered the content is displayed literally (variables are not evaluated).

Example:
A. Article
Article.content="this is my sample content ${helloWorld.text}"

B. HelloWorld bean
helloWorld.text = "parsed"

C. JSP
(...)
<jsp:useBean id="helloWorld" class="XYZ" scope="request"></jsp:useBean>
(...)
<cms:article category="XMMM" var="introArticle"/>
(...)
<div class="intro">I hope that it will be parsed: ${introArticle.content}</div>
(...)

Unfortunatelly I receive:
<div class="intro">I hope that it will be parsed: this is my sample content ${helloWorld.text}</div>

instead of
<div class="intro">I hope that it will be parsed: this is my sample content parsed</div>

Any ideas how to cope with this ?

Regards,
Maciek
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The EL expressions get parsed during the translation phase of the JSP. When you grab the strings during the execution phase, it's much too late to get the expressions parsed.

You might want to poke around the classes in the javax.servlet.jsp.el package to see if there's anything there that you can use to help with late evaluation, but it's not something i have any experience with.
 
Maciej Grula
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, thanks for the hint !

I'll need to work it out a bit. Meanwhile I suppose that I'm left with manual parsing of the content and probably using ExpressionEvaluator to evaluate expressions. Maybe I'll use the evaluate(java.lang.String expression, java.lang.Class expectedType, VariableResolver vResolver, FunctionMapper fMapper). Any ideas where from get the FunctionMapper instance ? There must be one as we can use default functions in jsp. Do You think this is the right direction ?

Regards,
Maciek
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, no hints. As I said,not anything I've even thought of doing.

To be honest I'd back up a bit and evaluate why you are doing it this way in the first place to see if there's an alternate means of achieving the same goal with less mayhem.
 
Maciej Grula
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to have a mechanism for displaying articles (html content) from database. Articles are created by business users. They want the possibility to use some dynamic variables in the article body (cms redactor). For instance - we agree (me and the business) that somewhere on the order confirmation page the confirmation article will be displayed and in that article's body one can use ${orderId} variable to present the id of the currently placed order.

I have a workaround - I'll simply pass a map of evaluated key/value pairs as an another tag parameter and replace content accordingly in the tag's doTag method (I extend SimpleTagSupport class). But I'm not proud of the beauty of the workaround.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If well-constructed, I can think of no objections to your approach.
 
Maciej Grula
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Workaround works just fine

To sum up: solution is based on Dynamic Attributes in custom tags. I pass evaluated variables as dynamic attributes so I'm not constraint to any particular parameters' names. Therefore it's only a matter of a contract with the business people how they will name their dynami variables in article's content. My tag implementation class implements DynamicAttributes interface (of course one need to amend also the .tld definition file with <dynamic-attributes>true</dynamic-attributes>). In setDynamicAttribute method I put all dynamic attributes in the hash map (attribute name as key and value as value). In the doTag method I do String.replaceAll on the content elements that should be evaluated/parsed.

Example:
A. JSP
<jsp:useBean id="street" class="XYZ" scope="request"></jsp:useBean>
(...)
<cms:article category="XYZ" var="mainArticle" streetName="${street.name}"/>
(...)
<div class="intro">${mainArticle.content}</div>

B. doTag
(...)
[loop]
source.setContent(source.getContent().replaceAll("\\$\\{"+key+"\\}", dynamicAttributes.get(key)));

C. And the article's content:
content="this is content with evaluation ${streetName}"


Bear - many thanks for Your help and attention.

Regards,
Maciek
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!