Tutorial :Setting global variable in Struts with Spring



Question:

What is the best approach to set servlet context variable in Struts? This variable is displayed on every single page in header tile and has to be loaded from DB. For that purpose there is Hibernate DAO and Spring Service which returns the requested value.

My current approach was to extend Struts PlugIn class and inject my service into it. This is not possible to do directly in Struts but I found a page (http://opensource.atlassian.com/confluence/spring/display/DISC/Spring-enabling+Struts+PlugIns) where Ulrik Sandberg creates a proxy which allows such functionality. This is working when I deploy my application but JUnits are failing somewhere in servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:331) on java.lang.NullPointerException.

Is there another approach which I can take which is not going to involve debugging someone others classes?


Solution:1

Look at the spring reference guide and the section (3.4) on bean scopes. You can have a bean that's maintained over the the life of the browser session, and set it up as a proxy session bean.

<!-- a HTTP Session-scoped bean exposed as a proxy -->  <bean id="userPreferences" class="com.foo.UserPreferences" scope="session">          <!-- this next element effects the proxying of the surrounding bean -->        <aop:scoped-proxy />  </bean>  

That's from section 3.4.4.5.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »