Spring MVC应用程序-如何设置会话范围的Bean值


问题内容

在我的应用程序中,我需要在一个屏幕上收集信息,然后在下一个屏幕上显示它。

我选择将此信息存储在一个范围设置为会话的bean中(它将在初始数据收集屏幕之后的其他几个屏幕中使用)

Manager的配置如下:

 <bean name="/springapp.htm" class="foo.bar.controller.springcontroller">
        <property name="sessionBeanManager" ref="sessionBeanManager" />
    </bean>

Bean的配置如下:

<bean id="sessionBean" class="foo.bar.sessionBean" scope="session">
    <aop:scoped-proxy/>
   <property name="beanValue" value="defaultValue" />
</bean>

<bean id="sessionBeanManager" class="foo.bar.sessionBeanManagerImpl">
    <property name="sessionBean" ref="sessionBean"/>
</bean>

我在jsp页面上输出

<c:out value="${sessionBean.beanValue}"></c:out>

但是每当我加载页面时,该值为空吗?

在我看来,该Bean正在加载OK,但未填充该值,这使我认为是未填充会话Bean还是未将其创建为会话Bean?


问题答案:

除非首先将Spring bean添加到模型中,否则它们在视图中(在您的情况下为JSP)是不可见的。

您必须将sessionBean添加到控制器中的模型中,以使其可用于视图。

model.addAttribute("sessionBean", sessionBean);