Spring MVC Application - How do I set a session scoped bean value

点点圈 提交于 2019-12-01 06:34:48

问题


In my application I need to gather information on one screen and then display it on the next.

I have selected to store this information in a bean with a scope set as session ( it will be used in several other screens after the initial data gathering screen)

The Manager is configured as follows:

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

The bean is configured as follows :

<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>

And I am outputting on the jsp page with

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

but whenever I load the page the value is empty?

It seems to me that the bean is loading OK but is not populated with the value, which leads me to think that either the session bean is not being populated or the bean is not being created as a session bean?


回答1:


Spring beans are not visible in the views (JSPs in your case) unless you add them first to the model.

You have to add your sessionBean to the model in the controller to make it available to the view.

model.addAttribute("sessionBean", sessionBean);



回答2:


You can reference spring session beans with the following syntax in your EL in the jsp.

${sessionScope['scopedTarget.messageUtil'].flashMessages}

That calls getFlashMessages() on this bean

<bean id="messageUtil" class="mypackage.MessageUtilImpl" scope="session">
    <aop:scoped-proxy proxy-target-class="false"/>
    <property name="messageSource" ref="messageSource" />
</bean>


来源:https://stackoverflow.com/questions/2132928/spring-mvc-application-how-do-i-set-a-session-scoped-bean-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!