Spring, my favorite Java application framework, doesn't provide an easy way to bind Spring-managed session beans to JSP pages. I have had success doing this by binding the bean to the request at creation time. When doing it this way, be sure to call the session bean somewhere in your dispatch servlet configuration or it will never be initialized when individual requests are created.
Note that for most applications, session beans are better left as plain objects managed independently of Spring for less overhead and to keep them as simple as possible. Each of these is going to take up space for each user. However, I have found some cases where accessing Spring objects from within a session bean is desirable; if you have as well or curiosity prevails, details are below.
Below are some sample config file changes and code to do this. First define the beans in application context:
applicationContext.xml
. . .
<bean id="requestHolder" class="com.example.RequestHolder" scope="request">
<aop:scoped-proxy />
</bean>
<bean id="userSession" class="com.example.Session" scope="session" >
<aop:scoped-proxy />
<property name="request">
<ref local="requestHolder" />
</property>
</bean>
. . .
Add a Spring request listener to web.xml so it explicitly stores the request for autowiring:
web.xml
. . .
<listener>
<description>Request listener to feed request objects to request holder</description>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
. . .
A sample RequestHolder; a location to store a pointer to the request so it can be accessed directly by the session bean; separate because it needs to be created upon a 'request' in the configuration:
RequestHolder.java
package com.example;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
public class RequestHolder {
@Autowired
private HttpServletRequest request;
@Autowired
private HttpSession session;
public HttpServletRequest getRequest() {
return request;
}
public HttpSession getSession() {
return session;
}
}
Here's the sorcery that binds the bean to the request so we can use it in JSPs:
Session.java
. . .
public void setRequest(RequestHolder requestHolder) {
//Any initialization required needs to be done here
initialize(requestHolder.getRequest());
//Inject ourself into the session so JSPs can see us
HttpSession session = requestHolder.getSession();
session.setAttribute("userSession", this);
}
. . .
Make sure something in your dispatcher calls for the userSession bean. Spring objects are only created upon request. I.E. there must be a
p:userSession-ref="userSession"
in your dispatcher-servlet.xml or an annotated call in each controller loading a page where you want to use userSession or it simply won't work in your JSPs (with no error messages to clue you in). Call the session object in referenceData so it is initialized properly on page loads.
Once set up, JSP JSTL can access the values; for example if we're storing uid's in the session and want to retrieve it for this user
${userSession.uid}