从JSP文件中的会话范围的Bean访问数据 [英] Accessing data from session-scoped beans in JSP files
问题描述
我试图在Spring Web MVC 3中开始使用会话范围的bean.我将这一行放入调度程序配置中:
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" />
这里是net.sandbox.sessionbeans.UserInfo
:
package net.sandbox.sessionbeans;
public class UserInfo {
public String username;
public UserInfo() {
this.username = "Unregistered User";
}
}
如何访问代表应用程序的View部分的JSP文件内的会话范围的bean?我试过了...
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>
相反,我只是得到
<p align="right"></p>
我在做什么错了?
推荐答案
此答案部分基于一些建议,这些建议已张贴在问题的注释中,但后来被张贴者删除.我将此添加到需要使用bean的每个JSP页面中:
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:
<jsp:useBean id="userInfo" scope="session" class="net.sandbox.sessionbeans.UserInfo" />
然后我发现了这篇文章详细介绍了您可以在JSP页面中使用bean.
I then found this article detailing how you can use beans in a JSP page.
这篇关于从JSP文件中的会话范围的Bean访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!