java - 如何访问 Velocity 模板中的 Session 或 Request 对象

标签 java spring-mvc velocity

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

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

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

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

当前网址:${request.get("attributes").get("CURRENT_URL"))} 结果 > 当前 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>

最佳答案

$request.getParameter("parameterName")

关于java - 如何访问 Velocity 模板中的 Session 或 Request 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12084132/

相关文章:

java - Mockito when().thenReturn 不起作用

java - 如何在libGDX中以相反的方式射击子弹

javascript - 访问速度模板中的 jquery/javascript 变量

java - 如何在速度模板中获取动态属性

java - 在远程服务器上运行多个命令

java - 在 Android Studio 中以编程方式添加值的字符串名称

spring - JSTL c :out not showing the variable's value

rest - 何时在 RESTful API 中使用路径参数与查询参数?

java - 在创建应用程序 bean 之前初始化 Spring Batch DB

java - 每次更新查询耗时 0.1 秒