从 JSP 文件中的会话范围 bean 访问数据
我正在尝试在 Spring Web MVC 3 中开始使用会话范围的 bean。我将这一行放入我的调度程序配置中:
<bean id="userInfo" class="net.sandbox.sessionbeans.UserInfo" scope="session" />
Here is net.sandbox.sessionbeans.UserInfo
:
package net.sandbox.sessionbeans;
public class UserInfo {
public String username;
public UserInfo() {
this.username = "Unregistered User";
}
}
How can I access session- JSP 文件中代表应用程序视图部分的作用域 bean?我尝试了这个......
<p align="right">${userInfo.username}</p>
但这并没有给我预期的结果,即
<p align="right">Unregistered User</p>
相反我只是得到
<p align="right"></p>
我做错了什么?
I'm trying to get started with session-scoped beans in Spring Web MVC 3. I put this line in my dispatcher configuration:
<bean id="userInfo" class="net.sandbox.sessionbeans.UserInfo" scope="session" />
Here is net.sandbox.sessionbeans.UserInfo
:
package net.sandbox.sessionbeans;
public class UserInfo {
public String username;
public UserInfo() {
this.username = "Unregistered User";
}
}
How can I access session-scoped beans inside the JSP files that represent the View part of my application? I tried this...
<p align="right">${userInfo.username}</p>
... but that didn't give me the expected result, i.e.
<p align="right">Unregistered User</p>
Instead I just get
<p align="right"></p>
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以按照问题中的说明进行操作。问题可能出在您的配置上。看看你是否在视图中暴露了你的bean,如下所示:
You can do it as you show in your question. The problem is probably in your configuration. Look if you expose your beans in the view, like this:
您可以使用 Spring 将各个 bean 公开给 JSTL。
You can expose individual beans to JSTL with Spring.
该答案部分基于问题评论中发布的一些建议,但后来被发帖者删除。我将其添加到需要使用 bean 的每个 JSP 页面中:
然后我发现 这篇文章详细介绍了如何在 JSP 页面中使用 bean。
This answer is partially based on some advice that was posted in the question's comments but was later deleted by the poster. I added this to every JSP page that needs to use the bean:
I then found this article detailing how you can use beans in a JSP page.
您需要确保您已
在 xml 配置中启用。
例如:
您可以在 Spring 参考指南的“Bean 范围”一章中阅读更多相关信息。
You need to make sure that you have
enabled in your xml configuration.
For Example:
You can read more about it in Spring reference guide, "Bean scopes" chapter.
下面详细阐述@sinuhepop 建议。
一个简单的方法是配置 spring,使其将 spring bean id 作为变量公开给 JSTL,这可以通过将 ExposureContextBeansAsAttributes 属性设置为 true 来完成。
如果您有一个像这样配置的 bean,
那么在 JSP 页面中您可以访问bean 使用名称
${someBean.someProp}
因为这是 Spring 将自动分配给 SomeBean 的名称,除非该 bean 是使用以下名称定义的@Component("someName")
那么 JSTL 将是${someName.someProp}
Elaborating on @sinuhepop suggestion below.
An easy way to do this is to configure spring so that it exposes the spring bean ids as varibales to JSTL this can be done by setting the exposeContextBeansAsAttributes property to true
If you have a bean configured like so
Then in the JSP page you can access the bean using the name
${someBean.someProp}
because that is the name that Spring will auto assign to the SomeBean unless the bean is defined with a name such as@Component("someName")
then the JSTL would be${someName.someProp}
对于那些想要使用 Spring 5 Java 配置的人来说,这个答案的更新。将其添加到您的
WebMvcConfigurer
中相当于其他人提到的 XML 配置:
请注意,注册表的“方便”流畅 API (
registry.jsp(). ...
) 不提供配置exposeContextBean...
属性的可能性。如果可能,您应该考虑使用
exposeContextBeanNames
。不过,请尽可能多地对 bean 名称使用常量,以避免在代码中重复字符串文字。因此,也许在某个类中定义一个字符串数组,它收集所有这些常量并将它们公开给您的视图解析器。An update to this answer for those, who want to use Spring 5 Java configuration. Addding this to your
WebMvcConfigurer
is equivalent to this XML config mentioned by others:
Note, that the "convenient" fluent API of the registry (
registry.jsp(). ...
) does not offer you the possibility to configure theexposeContextBean....
properties.If possible, you should consider using
exposeContextBeanNames
. Use constants as much as possible for your bean names, though, to avoid duplicating string literals in your code. So maybe define a String Array within some class, which collects all theses constants and exposes them to your view resolver.