0

I have a HashMap<Long, ClientProperties> that I'm putting on the ServletContext at startup.

//During application-startup:
//getProperties() returns HashMap<Long, ClientProperties>
context.setAttribute("clientProps", clientManager.getProperties());    

ClientProperties is a POJO with 5 attributes that i need to access in my jsp. Basicly I need to get the correct POJO (by HashMap-key) and access its properties in my jsp.

More spesific (for example purposes), one of the POJO attributes is clientLogo In my jsp i now have: <img src="<c:url value='/images/logo.png'/>" alt="Logo">

I need to replace the path to the logo-file with the clientLogo-property of the POJO. The HashMap-key to use should be extracted from the User-object stored in the session. It can be retrieved like this: ${sessionScope['user'].clientId}

Any ideas? Using struts2 and spring btw if that matters.

1 Answer 1

0

To get an attribute foo from the servlet context, you use the same syntax as to get it from the session, but replace sessionScope by applicationScope.

But you have so many nested things here that you should define variables:

<c:set var="map" value="${applicationScope['clientProps']}"/>
<c:set var="mapKey" value="${sessionScope['user'].clientId}"/>
<c:set var="pojo" value="${map[mapKey]}"/>
<c:set var="clientLogo" value="${pojo.clientLogo}"/>

<c:url value="${clientLogo}"/>

Note that this is typically the kind of hard work that you should not have to do in the view. Implement the retrieval of the image path in the controller, in Java, and make it available as a property of your action/form, and access it directly from your view.

2
  • Thanks for your answer. Very helpful! The reason im doing it in the view is because this is the header.jsp, decorated by sitemesh. Can the header.jsp access struts-action-properties?
    – user829237
    May 9, 2012 at 15:36
  • I don't see why it could not. But I don't have much experience with Struts2 and don't know how your app is configured.
    – JB Nizet
    May 9, 2012 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.