Velocity模板是否有隐式请求对象?

发布于 2024-12-07 10:19:36 字数 1060 浏览 0 评论 0 原文

我正在将现有问题改写为更通用的问题。我想知道 Velocity 是否有像 JSP 那样的隐式对象引用。 我对了解请求对象特别感兴趣。 在JSP中,我们可以获取请求范围内的属性,例如 <%= request.getAttribute("req1") %>

我知道JSP是一个servlet并且<%= request .getAttribute("req1") %> 最终作为 _jspService() 方法的一部分,该方法在请求范围结束之前拥有可用的请求对象。

我不确定 Velocity 在幕后如何工作(它在发挥作用时可能会将请求对象留在后面)

为了测试我做了以下事情,这是我上一个问题的一部分。

我有一个 Spring MVC TestController,在其中设置请求属性。我正在使用 Velocity 模板来渲染视图。

@RequestMapping(value="/test", method=RequestMethod.GET)
public ModelAndView display(HttpServletRequest req, HttpServletResponse resp){
    ...
    req.setAttribute("req1", "This should be present for first request");
    ...
}

在 Velocity 模板中,我正在执行类似的操作

Request: $request.getAttribute('req1')

,但我没有获得 req1 的值。我知道我应该将 req1 放在模型映射中而不是 request 中,但我想了解隐式请求对象引用。

我也尝试了 $req1 但它不起作用。

当我对模型执行相同的操作并将其返回时,一切正常。

我哪里错了?

更新: req.getSession().setAttribute("req1", testObject) 也发生了同样的事情。

I'm rephrasing my existing question to a more generic one. I want to know if Velocity has got implicit object references like JSP does.
I'm particularly interested in knowing about the request object.
In JSP we can get the attribute in the request scope like <%= request.getAttribute("req1") %>

I know that JSP is a servlet and <%= request.getAttribute("req1") %> ends up as a part of _jspService() method which has the request object available to it before the scope of the request ends.

I'm not sure how Velocity works behind the scenes (it may be leaving the request object behind by the time it plays it role)

To test that I did the following thing which was a the part of my previous question.

I have a Spring MVC TestController in which I'm setting a request attribute. I'm using Velocity templates for rendering the views.

@RequestMapping(value="/test", method=RequestMethod.GET)
public ModelAndView display(HttpServletRequest req, HttpServletResponse resp){
    ...
    req.setAttribute("req1", "This should be present for first request");
    ...
}

In the Velocity template I'm doing something like

Request: $request.getAttribute('req1')

but I'm not getting the value of req1. I know I should have put req1 in model map instead of request but I want to know about implicit request object ref.

I tried $req1 as well but its not working.

When I'm doing the same thing with the model and returning it back, everything is working correctly.

Where am I going wrong?

Update: The same thing is happening with req.getSession().setAttribute("req1", testObject) also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-12-14 10:19:36

Salaam,

req.getSession().getAttribute("req1", testObject)  ==  $req1

AFAIK,您无法在 VelocityViewServlet 模板中访问请求对象,除非您在上下文中显式设置请求对象或使用 v-tool

Salaam,

req.getSession().getAttribute("req1", testObject)  ==  $req1

AFAIK, you cannot access the request object at VelocityViewServlet's templates, unless you explicity set the request object in context or use a v-tool .

高速公鹿 2024-12-14 10:19:36

看看这个问题:Velocity + Spring。 Spring 人员并没有及时更新与 Velocity 的集成。

一旦您创建了该扩展并将其设置为在您的 servlet 配置中正确使用,您就可以简单地将对象放在 ModelAndView 上,并从那里使用它执行您需要的任何操作。

Take a look at this question: Velocity + Spring. The Spring folks haven't kept the integration with Velocity very up to date.

Once you've created that extension and set it up to be used properly in your servlet configuration, you'd be able to simply put the object on the ModelAndView and from there do whatever you need with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文