如何在Velocity Template中访问Session或Request对象

Ifi*_*Ifi 4 java velocity spring-mvc

我试图在一些速度模板中访问HttpServletRequest但从未成功.我已经尝试过以下语法风格

当前URL:$ req.get("attributes").get("CURRENT_URL"))结果>当前URL:$ req.get("attributes").get("CURRENT_URL"))

当前URL:$ request.get("attributes").get("CURRENT_URL"))结果>当前URL:$ request.get("attributes").get("CURRENT_URL"))

当前URL:$ request.get("attributes").get("CURRENT_URL"))结果>当前URL:$ request.get("attributes").get("CURRENT_URL"))

当前网址:$ {request.get("attributes").get("CURRENT_URL"))}结果>当前网址:$ {request.get("attributes").get("CURRENT_URL"))}

注意:Web.xml看起来像

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

Die*_*mos 5

$用request.getParameter( "参数名称")


Eva*_*aas 2

默认情况下,您将无权访问HttpServletRequestVelocity 模板中的 ;您只能访问已放入的对象Context。因此,在支持 Java 类中,将所需的信息添加到上下文中:

context.put("url", request.getAttribute("CURRENT_URL"));
Run Code Online (Sandbox Code Playgroud)

然后在您的 Velocity 模板中,您可以简单地引用$url.