Results 1 to 4 of 4

Thread: How to access ServletContext in Velocity

  1. #1
    Join Date
    Aug 2004
    Location
    Oxford, Ohio, USA
    Posts
    18

    Default How to access ServletContext in Velocity

    Sorry to be dense, but what are the steps to access the ServletContext in a Velocity view?

    Thanks,
    Jon

  2. #2

    Default

    Seems as though the powers-that-be with Spring don't want you to. If in the configuration of your VelocityViewResolver, you set requestContextAttribute equal to something, say "context" for example, then you would be able to access like this:

    Code:
    $!{context.servletContext.serverInfo}
    But RequestContext.getServletContext() is protected, so that doesn't work. A way to do it that actually works is do this in your Controller:

    Code:
    request.setAttribute("servletContext",getServletContext());
    And then set exposeRequestAttributes equal to true in your VelocityViewResolver, then you can access it in Velocity like this:

    Code:
    $!{servletContext.serverInfo}

  3. #3
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    Another approach would perhaps be to specifically get the properties you need from ServletContext and put them in your ModelAndView, for Spring MVC seems to promote passing data to views through ModelAndView.
    --Jing Xue

  4. #4
    Join Date
    Aug 2004
    Location
    Oxford, Ohio, USA
    Posts
    18

    Default

    Thanks for the suggestion Paul.

    I modified what you suggested and came up with the following:

    In my viewResolver definition:

    Code:
    ...
    <property name="requestContextAttribute"><value>context</value></property>
    ...
    Which places the RequestContext attribute into the request under the name "context", just like you mentioned.

    Then, to get around getServletContext() being a protected method of RequestContext, I do the following in my Velocity templates:

    Code:
    $context.webApplicationContext.servletContext.getAttribute&#40; "serverInfo" &#41;
    This works great! However, does anyone know of a less wordy way to do this? It seems like:

    Code:
    $context.webApplicationContext.servletContext.getAttribute&#40; "serverInfo" &#41;
    just to get one variable is a heck of a lot of typing.

    Thanks to both of you for your suggestions.

    Jon

Similar Threads

  1. How to configure Velocity tools?
    By alr in forum Web
    Replies: 7
    Last Post: Jan 31st, 2007, 09:38 AM
  2. Context initialization failed
    By kanonmicke in forum Container
    Replies: 7
    Last Post: Sep 29th, 2005, 12:35 AM
  3. FreeMarker vs Velocity
    By Martin Kersten in forum Architecture
    Replies: 8
    Last Post: May 30th, 2005, 09:21 AM
  4. Velocity OutOfMemoryError during UnitTests
    By camach in forum Container
    Replies: 2
    Last Post: May 26th, 2005, 05:28 PM
  5. Exposing velocity context
    By masum in forum Web
    Replies: 1
    Last Post: Oct 28th, 2004, 02:27 AM

Posting Permissions

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