Forums Register Login

evaluate ${variable} where variable contains ${}

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
If well-constructed, I can think of no objections to your approach.
+Pie Number of slices to send: Send
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
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2079 times.
Similar Threads
how comws varaible in the tag
using usebean action
JSTL & EL, not resolving my ${identifier} syntax?
how to send my object reference from .jsp to .class
Jsp & css
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:09:57.