Spring 3 MVC: Expose session scoped bean in MVC Controller method arguments

我的未来我决定 提交于 2019-12-10 21:03:45

问题


I want to pass a session-scoped domain bean around my controllers for consistency and simplicity - but this doesn't seem possible OOTB. Hope someone can advise.

Question: Can a session-scoped bean be exposed as a MVC Controller argument


There appears to be an annotation for this: @SessionAttributes("myBean") however this only maintains a Controller-level scope.

I'm looking to avoid having to interact with HttpSession and instead pass around my domain object graph consistently through my controllers. This would seem a fairly standard requirement.

This has benefits:

  • Testability - just inject bean to test and not have to mock out HttpSession
  • Abstraction - avoids Servlet model concerns and just the business problem

Here's the current config:

@Controller
@SessionAttributes("customer")
public class LoginController {

   @Inject Customer customer;

   @RequestMapping(value = "/login", method = RequestMethod.GET)
   public String welcome(Customer customer) {
         ...
         return "loginDetailsView";
   }

   public String processLogin(@Valid Customer customer, BindingResult bindingResult) {
         ...
         if (bindResult.hasErrors()) {
             return "loginDetailsView";
         else {
             return "homePageView";
         }
   }

'Customer' session bean is a regular POJO using XML configured aop-proxy (CGLIB) to allow the session scoped bean to be injected into singletons (controller and service classes):

<beans:bean id="customer" class="com.mypackage.domain.Customer" scope="session">
            <aop:scoped-proxy proxy-target-class="true"/>
</beans:bean>

The following question is virtually the same but there are no answers that don't involve extension to the Spring 3 core framework.

Has this been intentionally omitted by Spring's designers? My intent is to use the domain model to back the forms without using 'form beans' and a layer of mappers.

Can anyone advise a way / or indicate if this is poor practice?

Prior question with bespoke solution:

How to pass a session attribute as method argument (parameter) with Spring MVC

Please note: attempting to minimise the dependencies on the Spring framework and use Annotation support esp JSR-303 etc. Will adopt any OOTB solution - please don't suggest custom extentions.


回答1:


Spring-MVC can expose the application context's beans to the view layer, if that is what you wish to do.

For example, the InternalResourceViewResolver can be instructed to expose every bean in the context, or just specified ones. Take a look at the exposeContextBeansAsAttributes and exposedContextBeanNames properties.

For example, say you wanted to expose the beans beanA and beanB to your JSPs. You would declare the view resolver in your context thus:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="exposedContextBeanNames">
      <list>
         <value>beanA</value>
         <value>beanB</value>
      </list>
   </property>
</bean>

Alternatively, to just expose every bean:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="exposeContextBeansAsAttributes" value="true"/>
</bean>


来源:https://stackoverflow.com/questions/13276391/spring-3-mvc-expose-session-scoped-bean-in-mvc-controller-method-arguments

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