Results 1 to 4 of 4

Thread: Velocity access to application scope attributes?

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Posts
    8

    Default Velocity access to application scope attributes?

    Hi,
    I wonder if anyone can help. I can currently working through a proof of concept to determine view technology suitability for our application. I've put together a working application using JSP as the view; and am now trying to 'convert' it to VM.
    One of the things the application does is setup a series of 'common'/shared list objects in the application scope (yes/no, titles, genders etc). I only want these lists defined and instantiated once, so I have a bean which implements ServletContextAware and InitializingBean, and I add attributes to the ServletContext:
    Code:
    		ArrayList<GIOLPListItem> list = new ArrayList<GIOLPListItem>();
    		list.add(new GIOLPListItem("Y", "Yes"));
    		list.add(new GIOLPListItem("N", "No"));
    		m_context.setAttribute("yesNoList", list);
    In my JSP I can reference the yesNoList like this:
    Code:
    		<select name="<c:out value="${status.expression}"/>" id="<c:out value="${id}"/>">
    		<c:forEach var="listItem" items="${applicationScope['yesNoList']}">
    			<option value="<c:out value="${listItem.id}"/>" <c:if test="${listItem.id == status.value}">selected="selected"</c:if>><c:out value="${listItem.value}"/></option>
    		</c:forEach>
    		</select>
    The important syntax I think is this: ${applicationScope['yesNoList']}

    This works fine for JSP; but I cannot work out how to reference the application scope in VM

    Any ideas?

    Cheers

    JD

  2. #2

    Default

    Thread revival, but have you found an answer? I'm facing the same issue and it's really annoying. I assume Spring does some kind of masking, but I really have no idea how to access a variable in the ServletContext in a VM-file. Especially when Velocity clearly states on their site "The HttpServletRequest, HttpSession, ServletContext, and their attributes are automatically available in your templates.".

  3. #3
    Join Date
    Dec 2010
    Posts
    8

    Default

    We've moved on from Velocity now, we've now decided on Thymeleaf. But we had exactly the same issue trying to access attributes in the servletContext.
    In the end a very helpful person on the Thymeleaf discussion board and StackOverflow answered it:
    http://stackoverflow.com/questions/1...servletcontext

    Basically in Thymeleaf there appear to be 3 syntaxes for doing what we want - we've opted for the simpler (looking) option of just referencing a variable called 'application' in the Spring EL

    The bit I dont know is if the variable 'application' (and the others mentioned in the SO post) are Thymeleaf specific, or are exposed by Spring EL. Either way, its another one of those hard to find/badly documented things!

    Definately worth a go with Velocity I think - it may well solve it for you.

    Hope this helps

    JD

  4. #4

    Default

    Quote Originally Posted by Jakes Daddy View Post
    The bit I dont know is if the variable 'application' (and the others mentioned in the SO post) are Thymeleaf specific, or are exposed by Spring EL. Either way, its another one of those hard to find/badly documented things!
    The 'application' variable scope is thymeleaf-specific. You have 'session' (for session attributes), 'param' (for request parameters) and 'application' (for ServletContext attributes). All of them are thymeleaf-specific (although some other frameworks might choose the same names for such containers.

    Regards,
    Daniel.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •