传递给 JSP 的 Spring 模型去了哪里?
我一直在阅读有关 spring (3.0) 的文档和教程,因此我学会了如何返回带有 JSP 名称和 Map 作为模型的 ModelAndView
。我还了解到,在 JSP 中,如果您想访问该映射的一个键,您可以执行 ${attributename}
等等。这就是 JSP EL。现在我的问题是:
- EL 正在访问页面的哪个对象?那是
PageContext
吗?我试图在PageContext.getAttribute
中找到这些键,但它们不在那里。 - 无论对象是什么,模型中的东西是否会自动到达那里?
请随意添加资源来阐明我的想法
I've been reading docs and tutorials about spring (3.0), so I've learnt how to return a ModelAndView
with the JSP name and a Map as the model. I've learnt also that in a JSP, if you want to access one key of that map you do ${attributename}
and so on. That's JSP EL. Now my questions:
- What object of the page is the EL accessing? Is that
PageContext
? I tried to find those keys inPageContext.getAttribute
but they are not there. - Whatever the object is, is it automatic that the things in the model go there?
Feel free to add resources to clarify my ideas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您执行诸如
${attributename}
之类的操作时,JSP EL 将检查各种来源来查找它,包括页面和请求上下文(按此顺序)。当您的 Spring 控制器返回一个模型(例如在 ModelAndView 内部)时,该模型将由 Spring 的 AbstractView 类分解并插入到请求上下文中(这是“神奇”的部分) ),这样当您的 JSP EL 表达式引用模型中的某个项目时,就可以使用它。
When you do something like
${attributename}
, JSP EL will check a variety of sources to find it, including the page and request contexts (in that order).When your Spring controller returns a model (e.g. inside the
ModelAndView
), this model is decomposed by Spring'sAbstractView
class and inserted into the request context (this is the "magic" part), so that when your JSP EL expression refers to an item from the model, it's available to be used.它们通常存储在请求中。因此
request.getAttributeNames()
应该为您提供所有模型(但仅将其用于教育目的 - 否则使用 EL 访问模型)。也就是说,它也应该可以从pageContext
(在请求范围内)访问。是的,它是自动添加的。
They are usually stored in the request. So
request.getAttributeNames()
should give you all your model (but use that only for educational purposes - otherwise use EL to access the model). That said, it should also be accessible from thepageContext
(in request scope).And yes, it is automatically added.