You could also set up a bean that has session scope.
http://static.springframework.org/sp...factory-scopes
I just did this and it's quite slick. The bean is injected by spring and its internal state is kept in the session so each object that gets it injected gets what's in it. The first object (e.g., some controller) that gets it can set it up, calling its setters, then the rest can use its getters and setters.
applicationContext.xml:
Code:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.1.xsd">
<bean id="userMeta" class="jmemento.domain.user.impl.UserMeta" scope="session">
<aop:scoped-proxy proxy-target-class="false" />
</bean>
myApp-servlet.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
<!--
## convention over configuration handler
-->
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<!--
## controller beans
-->
<bean class="jmemento.web.controller.user.UserHomeController">
<constructor-arg ref="userMeta" />
</bean>
web.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>myApp</display-name>
<!--
## Location of the Log4J config file, for initialization and
## refresh checks. Applied by Log4jConfigListener.
-->
<context-param>
<param-name>
log4jConfigLocation
</param-name>
<param-value>
/WEB-INF/log4j.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!-- bootstraps the WebApplicationContext -->
<!-- must come after the Log4jConfigListener -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- listener for beans with session scope -->
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
UserHomeController.java:
Code:
public final class UserHomeController extends AbstractCommandController {
private final transient Log log = LogFactory.getLog(getClass());
private final IUserMeta userMetaSession;
/**
* @param _userMetaSession
*/
public UserHomeController(final IUserMeta _userMetaSession) {
if (_userMetaSession == null)
throw (new IllegalArgumentException("userMetaSession can't be null"));
userMetaSession = _userMetaSession;
setSupportedMethods(new String[] {
METHOD_GET
});
// no longer used
setCommandClass(UserMeta.class);
}
@Override
protected ModelAndView handle(final HttpServletRequest request,
final HttpServletResponse response, final Object command,
final BindException errors) throws Exception {
// no longer using the command
log.debug(String.format("command: %s", command));
log.debug(String.format("user: %s", userMetaSession));
if (userMetaSession.getId() == null)
return (new ModelAndView("notsignedin"));
final ModelAndView mav = new ModelAndView();
mav.addObject("user", userMetaSession);
return (mav);
}
}