Ifi
Ifi

Reputation: 540

How to access Session or Request object in Velocity Template

I am trying to access HttpServletRequest in some velocity template but never succeed. I have already tried following flavor of syntax

Current URL: $req.get("attributes").get("CURRENT_URL")) Result > Current URL: $req.get("attributes").get("CURRENT_URL"))

Current URL: $request.get("attributes").get("CURRENT_URL")) Result > Current URL: $request.get("attributes").get("CURRENT_URL"))

Current URL: $request.get("attributes").get("CURRENT_URL")) Result > Current URL: $request.get("attributes").get("CURRENT_URL"))

Current URL: ${request.get("attributes").get("CURRENT_URL"))} Result > Current URL: ${request.get("attributes").get("CURRENT_URL"))}

Note : Web.xml looks like

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<!-- Define Velocity template compiler -->
<servlet>
  <servlet-name>velocity</servlet-name>
  <servlet-class>
    org.apache.velocity.tools.view.servlet.VelocityViewServlet
  </servlet-class>

  <!-- 
   Unless you plan to put your toolbox.xml and velocity.properties
   under different folders or give them different names, then these
   two init-params are unnecessary as of VelocityTools 1.3.  The
   VelocityViewServlet will automatically look for these files in
   the following locations.
 -->
  <init-param>
    <param-name>org.apache.velocity.toolbox</param-name>
    <param-value>/WEB-INF/toolbox.xml</param-value>
  </init-param>

  <init-param>
    <param-name>org.apache.velocity.properties</param-name>
    <param-value>/WEB-INF/velocity.properties</param-value>
  </init-param>
</servlet>

<!-- Map *.vm files to Velocity -->
<servlet-mapping>
  <servlet-name>velocity</servlet-name>
  <url-pattern>*.vm</url-pattern>
</servlet-mapping>

Upvotes: 5

Views: 17122

Answers (6)

Diego Ramos
Diego Ramos

Reputation: 1069

$request.getParameter("parameterName")

Upvotes: 5

Ego Slayer
Ego Slayer

Reputation: 2067

Try this

$request.getSession().getAttribute('userId')

Upvotes: 0

Mike
Mike

Reputation: 41

To get specific parameter:

$!request.getParameter('parameterName')

To get entire query string:

$!request.getQueryString()

Upvotes: 0

Gary Blake
Gary Blake

Reputation: 11

You'll need to roll your own class based on session to do this properly.

I hit this problem instantly and am now going to create a session class that i will access via a values property as a List of HashMaps.

Then all you need to do is assign values once to velocity's context before use.

context.put("session", MySessionClass.values));

Upvotes: 0

Nathan Bubna
Nathan Bubna

Reputation: 6933

For VelocityTools, the proper references are $request and $response, not $req and $res

The methods name is getAttribute, not get. So you can do:

$request.getAttribute('foo')

or just $request.foo

but not $request.get('foo')

Upvotes: 3

Evan Haas
Evan Haas

Reputation: 2574

You won't have access to the HttpServletRequest by default in your Velocity template; you'll only have access to objects that have been placed in to the Context for you. So in the backing Java class, add the info you want into the conext:

context.put("url", request.getAttribute("CURRENT_URL"));

Then in your Velocity template, you can simply reference $url.

Upvotes: 2

Related Questions