Reputation: 245
I want to put an anchor tag into a Struts2 variable that I can then display at various points in a JSP. That way I can avoid duplicating the complicated if/then statements that are used to build various forms of the href tag.
I'm using the Struts 's:set' tag to do this.
The problem is that Struts converts the '<' characters to htmlentities and when the page displays I see the actual "a href=someURL" tag displayed, not a working link.
If I add the "escape='true'" argument to s:set it does the same thing, only it displays the htmlentities for the '<' and '>' tags.
How do I put a valid anchor tag into a Struts2 variable and then display it as a working link?
Here's what I'm doing:
<s:set name="composerName">
<s:property value="'a href=%{viewRecordURL}>'" escape="false"/>
<s:property value="#composer.title" />
<s:property value="#composer.firstName" />
<s:property value="#composer.lastName" />
<s:property value="'</a>'" escape="false" />
</s:set>
Upvotes: 1
Views: 7720
Reputation: 352
I had the same issue, and I just solved it as following:
In the main bean, I have the following variables with Setters and Getters:
private int ReqNo;
private String hyperLinkToPage;
In the JSP page I have the TD as following:
<td class="displayValue" style="width: 10%;"><s:a href="%{hyperLinkToPage}"><s:property value="%{ReqNo}"/></s:a></td>
I hope that it will help.
Upvotes: 0
Reputation: 192
do this easy way to set the link in Struts2 // add the Struts2 action in href
<s:a href="forgetPasswordPage.action" >Forget Password</s:a>
Upvotes: 1
Reputation: 4225
We can use url tag to create URL like mention below
<s:url id="hLink" action="yourStrutsActionName">
<s:param name="propertyName" value="%{propertyName}" />
</s:url>
<td><s:a href="%{hLink}"><s:property value="%{propertyName}"/></s:a></td>
Upvotes: 2
Reputation: 245
I'll answer my own question. Maybe someone else has a more elegant solution. Once again, we're trying to get both an anchor tag and a url into a s:set variable. The issues were two: 1) getting s:set to treat the tag string as a literal string and not an Object (which it wants to fetch from ActionContext or somewhere), and 2) turning off escaping so that the string text of the anchor tag isn't converted into htmlentities like '& lt;'.
For 1, I set the var to null, but provide a default argument, which s:property always treats as a literal string.
For #2, it's just a matter of using escape="false" in the right places.
Unfortunately, the string has to be built up with separate bits of s:property, but what can you do?
<s:set name="composerName" >
<s:property value="" default="<a href=" escape="false"/>
<s:property value="" default="'" />
<s:property value="%{viewRecordURL}" escape="true"/>
<s:property value="" default="'>" escape="false" />
<s:property value="#composer.title" />
<s:property value="#composer.firstName" />
<s:property value="#composer.lastName" />
<s:property value="" default="</a>" escape="false"/>
</s:set>
I haven't tested it yet with UTF-8 characters or potentially problematic characters like '&', apostrophe, or single quote in the #composer part.
Upvotes: 0
Reputation: 10555
Use s:url
to create a valid url and then use it in s:a
to generate the link.
You can refer the link below for more details:
http://struts.apache.org/2.1.8/docs/a.html
Upvotes: 2
Reputation: 160191
This isn't how you want to build this--what you have is two different things, a URL, and the text string of that url. The URL you already have. Only use <s:set>
for the link text.
If you really need to, you can wrap this up into a JSP-based custom tag, but I wouldn't bother.
Upvotes: 0