大家好,我这里有大问题。我的控制器中的会话无法正常工作。我的目标是为每个会话设置一个 comp 对象,但每次都得到相同的comp(comp只被初始化一次而不是每个会话)。
示例:
的Mozilla: 首先我的comp是null。在一个屏幕中,我选择一个comp,初始化新的comp,一切都OK。
铬: 当我到达我要选择的屏幕时,我的comp已经初始化(Mozilla comp),所以当我选择我的comp时,来自mozilla的comp被覆盖。
控制器:
package com.test;
@SessionAttributes({"comp", "userDetails"})
@Controller
@RequestMapping(value="/arep")
public class ARepController{
@Autowired AdmUserDetails userDetails;
@Autowired
private ARepService aRepService;
@Autowired
private Component comp;
}
组件:
package com.test;
@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "session")
public class Component implements Serializable {
private static final long serialVersionUID = 587780902400791285L;
private List<Component Item> items = new ArrayList<ComponentItem>();
private Integer length;
private Integer height;
private Integer depth;
public Component () {
}
public Component (Integer length, Integer height,Integer depth) {
this.length = length;
this.height = height;
this.depth = depth;
}
}
根context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd" >
<tx:annotation-driven/>
<context:component-scan base-package="com.test" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
servlet的context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.test" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans:beans>
我只复制了对我来说至关重要的代码。我要感谢任何愿意花时间帮助我的人。
答案 0 :(得分:0)
您还需要使控制器Bean会话作用域
答案 1 :(得分:0)
我认为你在MVC中略微混淆了M和C的使用。通常,控制器应该是单例,因此不能包含用户数据。控制器中的userDetails和comp属于模型,在大多数Spring MVC实现中,它们将存储在会话中。您的aRepService看起来像是所有用户共享的服务,因此它绝对属于控制器。
Spring可以创建你的模型bean(我相信),但这是一个简单的任务,所以我总是手动完成。这也避免了Spring bean创建的繁重开销。