views:

408

answers:

2

Hi,

I am trying to get my page to link to a certain URL that is stored in a HashMap (the key is some name, and the value is the URL I want to link to). I'm not very good at describing this, but here is my code:

For the JSP page:

<table>
  <s:iterator value="dependenciesList" id="dependency">
    <tr><td>
    <a href="<s:url value="productDocumentationMap.getKey(%{dependency})"/>">
    <s:property value="dependency"/> </a>
    </td></tr>
  </s:iterator> 
  </table>

Note: productDocumentationMap is a HashMap of <String, String>, and dependenciesList is an ArrayList<String>.

For instance, if dependenciesList contains three elements [A, B, C], the first link would link to something like: http:///productDocumentationMap.getKey(A) but what I want is for the link to be the actual value of

productDocumentationMap.getKey("A");

I know I might be doing something stupid (I'm still new to all this Struts2 business), but is there a way I can get my link to work? Thanks!

A: 

you action needs to have a getProductDocumentationMap() method:

try:

<a href="<s:property value="%{productDocumentationMap.getKey(#dependency)}"/>">
stage
Hi,I do have a getProductDocumentationMap method in my Action class, but when I tried your proposed fix, now I get a link back to the current page I am on... Would it be possible that my getter method is not working properly? I am able to retrieve the values from dependenciesList and other things though.
Raymond Chang
do check that the getter is working: productDocumentationMap: <s:property value="%{productDocumentationMap}"/>productDocumentationMap.get('samplekey'): <s:property value="%{productDocumentationMap.get('samplekey')}"/>
stage
Hi,I put the code you provided, and it printed out the contents of the map, then the content of the specific key I provided. There is one possibility I'm thinking of though: some of the products / keys do not have a value in the HashMap; I don't want those elements to have a link. Maybe that's why the links are not showing up correctly?
Raymond Chang
Thanks for your help stage, but I got it figured out with sdeer's answer above. Thanks for offering to help though!
Raymond Chang
+1  A: 

In OGNL, you can access a map using "mapName[indexName]", where indexName is the key you want.

e.g.

 <a href="<s:url value='productDocumentationMap[#dependency]'/>">

I think this is the right syntax for resolving 'dependency' as a variable, instead of as the string 'dependency', but this should call 'getProductDocumentationMap()', and if it returns a Map object, attempt to lookup the value of the iterator. I assume you really want the value, rather than the key, since 'dependency' itself is the key.

This page gives some example OGNL expressions that you might find helpful as a reference. I find half the time I just end up fiddling with no parenthesis, %{} and/or # until it works. :-)

http://www.vaannila.com/struts-2/struts-2-example/struts-2-ognl-expression-language-example-1.html


For your follow-up question:

I use this for testing nulls against simple properties and including a section. I imagine it could probably be applied to returned values from maps.

<s:if test="%{licenseStatusString != null}">
 ... something that uses licenseStatusString
</s:if>
<s:else>
 ... optional thing to include if the license status string is null.
</s:else>

maybe

<s:if test="%{productDocumentationMap[#dependency] != null}">

Try that and see if it works. Probably some permutation of that.

Shawn D.
Thanks a lot sdeer, this worked for me! On another note, would you happen to know how to create a link only if productDocumentationMap.get(dependency) returns a non-null value? Some of the products don't have any links, so I don't want to create a hyperlink to non-existent pages. Thanks a lot!
Raymond Chang
moved the comment into my original answer to preserve formatting
Shawn D.
I ended up using something like:<s:if test="productDocumentationMap[#dependency] != ''">(Apparently even if there is no value inside the Map, it won't return null, but it does return an empty String)Thanks a lot, you're a lifesaver!
Raymond Chang
Shawn D.