问题描述
我想通过一个会话范围的域bean围绕我的控制器一致性和简单性 - 但这似乎不可能OOTB。希望有人可以建议。
问题:会话范围的bean可以作为MVC控制器参数显示
这似乎是一个注释:@SessionAttributes(myBean),但这只保持一个控制器级别的范围。
我希望避免与HttpSession进行交互,而是通过我的控制器一直传递我的域对象图。这似乎是一个相当标准的要求。
这有好处:
- -
- 抽象 - 避免Servlet模型的关注和只是业务问题
这是当前的配置:
@Controller
@SessionAttributes
public class LoginController {
@Inject客户;
@RequestMapping(value =/ login,method = RequestMethod.GET)
public String welcome(Customer customer){
...
returnloginDetailsView ;
}
public String processLogin(@Valid客户,BindingResult bindingResult){
...
if(bindResult.hasErrors()){
returnloginDetailsView;
else {
returnhomePageView;
}
}
'Customer'会话bean是使用XML的常规POJO配置aop代理(CGLIB)以允许将会话作用域bean注入到单例(控制器和服务类)中:
< ; beans:bean id =customerclass =com.mypackage.domain.Customerscope =session>
< aop:scoped-proxy proxy-target-class =true/>
< / beans:bean>
以下问题实际上是一样的,但没有没有涉及扩展到Spring的答案3核心框架。
这是Spring的设计师有意忽略的吗?我的目的是使用域模型来回复表单,而不使用form beans和一层mapper。
任何人都可以建议一种方式/或指示这是否不好的做法?
预订问题与定制解决方案:
请注意:试图最小化对Spring框架的依赖,并使用注释支持esp JSR-303等。将采用任何OOTB解决方案 - 请不要建议自定义扩展。
Spring MVC可以将应用程序上下文的bean暴露给视图层,如果这是你想做的。 p>
例如,可以指示InternalResourceViewResolver暴露上下文中的每个bean,或者只显示指定的bean。请查看和属性。
例如,将bean beanA和beanB暴露给您的JSP。您将在上下文中声明视图解析器:
< bean class =org.springframework.web.servlet.view .InternalResourceViewResolver>
< property name =exposedContextBeanNames>
< list>
< value> beanA< / value>
< value> beanB< / value>
< / list>
< / property>
< / bean>
或者,为了公开每个bean:
< bean class =org.springframework.web.servlet.view.InternalResourceViewResolver>
< property name =exposeContextBeansAsAttributesvalue =true/>
< / bean>
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.
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>
这篇关于Spring 3 MVC:在MVC Controller方法参数中公开会话作用域bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!