开发者

Getting URL value from HashMap in a JSP page (Using Struts2)

开发者 https://www.devze.com 2023-01-11 07:06 出处:网络
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 m

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开发者_Go百科, 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消