Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 489
Ifi Avatar asked Aug 23 '12 02:08

Ifi


People also ask

How do I test a Velocity template?

Approach 1: The obvious approach: Run and check. Run Velocity against the template to be checked. Look at the output and see if it is what you desired. This must be the most primitive testing approach, but most people nowadays are too lazy and want this done by the computer.

What are Velocity templates?

Velocity is a server-side template language used by Confluence to render page content. Velocity allows Java objects to be called alongside standard HTML. If you are are writing a user macro or developing a plugin you may need to modify Velocity content.


2 Answers

$request.getParameter("parameterName")
like image 168
Diego Ramos Avatar answered Nov 14 '22 23:11

Diego Ramos


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')

like image 21
Nathan Bubna Avatar answered Nov 14 '22 21:11

Nathan Bubna