【问题标题】:How to access Session or Request object in Velocity Template如何在 Velocity 模板中访问 Session 或 Request 对象
【发布时间】:2012-08-23 02:56:29
【问题描述】:

我试图在一些速度模板中访问 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>

【问题讨论】:

    标签: java spring-mvc velocity


    【解决方案1】:
    $request.getParameter("parameterName")
    

    【讨论】:

      【解决方案2】:

      对于 VelocityTools,正确的引用是 $request 和 $response,而不是 $req 和 $res

      方法名是getAttribute,不是get。所以你可以这样做:

      $request.getAttribute('foo')

      或者只是 $request.foo

      但不是 $request.get('foo')

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          【解决方案4】:

          您需要根据会话滚动自己的课程才能正确执行此操作。

          我立即遇到了这个问题,现在我要创建一个会话类,我将通过 values 属性作为 HashMap 列表访问它。

          那么你需要做的就是在使用前给velocity的上下文赋值一次。

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

          【讨论】:

            【解决方案5】:

            获取具体参数:

            $!request.getParameter('parameterName')
            

            获取整个查询字符串:

            $!request.getQueryString()
            

            【讨论】:

              【解决方案6】:

              试试这个

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

              【讨论】:

                猜你喜欢
                • 2016-02-28
                • 2013-02-07
                • 2010-10-02
                • 2016-05-08
                • 2010-09-13
                • 2015-09-22
                • 2016-09-23
                • 2020-09-14
                • 2012-07-22
                相关资源
                最近更新 更多