java 如何在 Velocity 模板中访问 Session 或 Request 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12084132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access Session or Request object in Velocity Template
提问by Ifi
I am trying to access HttpServletRequest in some velocity template but never succeed. I have already tried following flavor of syntax
我试图在某些速度模板中访问 HttpServletRequest 但从未成功。我已经尝试过以下语法风格
Current URL: $req.get("attributes").get("CURRENT_URL")) Result > Current URL: $req.get("attributes").get("CURRENT_URL"))
当前 URL:$req.get("attributes").get("CURRENT_URL")) 结果 > 当前 URL:$req.get("attributes").get("CURRENT_URL"))
Current URL: $request.get("attributes").get("CURRENT_URL")) Result > Current URL: $request.get("attributes").get("CURRENT_URL"))
当前 URL:$request.get("attributes").get("CURRENT_URL")) 结果 > 当前 URL:$request.get("attributes").get("CURRENT_URL"))
Current URL: $request.get("attributes").get("CURRENT_URL")) Result > Current URL: $request.get("attributes").get("CURRENT_URL"))
当前 URL:$request.get("attributes").get("CURRENT_URL")) 结果 > 当前 URL:$request.get("attributes").get("CURRENT_URL"))
Current URL: ${request.get("attributes").get("CURRENT_URL"))} Result > Current URL: ${request.get("attributes").get("CURRENT_URL"))}
当前 URL:${request.get("attributes").get("CURRENT_URL"))} 结果 > 当前 URL:${request.get("attributes").get("CURRENT_URL"))}
Note : Web.xml looks like
注意: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>
回答by Diego Ramos
$request.getParameter("parameterName")
$request.getParameter("参数名")
回答by Nathan Bubna
For VelocityTools, the proper references are $request and $response, not $req and $res
对于 VelocityTools,正确的引用是 $request 和 $response,而不是 $req 和 $res
The methods name is getAttribute, not get. So you can do:
方法名称是 getAttribute,而不是 get。所以你可以这样做:
$request.getAttribute('foo')
$request.getAttribute('foo')
or just $request.foo
或者只是 $request.foo
but not $request.get('foo')
但不是 $request.get('foo')
回答by Evan Haas
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:
HttpServletRequest
默认情况下,您将无法访问Velocity 模板中的 ;您只能访问已Context
为您放入的对象。因此,在支持 Java 类中,将所需的信息添加到上下文中:
context.put("url", request.getAttribute("CURRENT_URL"));
Then in your Velocity template, you can simply reference $url
.
然后在您的 Velocity 模板中,您可以简单地引用$url
.
回答by Gary Blake
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.
我立即遇到了这个问题,现在我将创建一个会话类,我将通过 values 属性作为 HashMap 列表访问该类。
Then all you need to do is assign values once to velocity's context before use.
然后您需要做的就是在使用之前为速度的上下文分配一次值。
context.put("session", MySessionClass.values));
回答by Mike
To get specific parameter:
获取具体参数:
$!request.getParameter('parameterName')
To get entire query string:
获取整个查询字符串:
$!request.getQueryString()
回答by Ego Slayer
Try this
试试这个
$request.getSession().getAttribute('userId')