Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

ModelDriven, Preparable and SessionAware do not work with Spring

Shih Lee

2007-03-08

Replies:

Hi All,



I have been playing with Struts 2.0 sample application, struts-blank, and
integrates it with Spring 2.x. So far, I have not been having great success
with it.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


I have followed all the steps needed to integrate Struts 2.0 with Spring 2.0.
Here is what I have done so far:



1) Included struts2-spring-plugin-2.0.6.jar, spring.jar,
struts2-core-2.0.6.jar and the dependent jars.



2) Modified the example.xml to include spring object factory:



       <!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">



<struts>



   <constant name="struts.objectFactory" value="spring" />



  <package name="example" namespace="/example" extends="struts-default">



   <default-interceptor-ref name="defaultStack"/>



    <action name="helloWorld" class="helloWorld">

       <result>/example/HelloWorld.jsp</result>

    </action>



    <action name="Login_*" method="{1}" class="example.Login">

       <result name="input">/example/Login.jsp</result>

       <result type="redirect-action">Menu</result>

    </action>



    <action name="Menu" class="Menu">

       <result>/example/Menu.jsp</result>

    </action>



    <action name="*" class="example.ExampleSupport">

       <result>/example/{1}.jsp</result>

    </action>



    <!-- Add actions here -->

  </package>

</struts>



3) I have edited the spring applicationContext.xml



<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
http://www.springframework.org/dtd/spring-beans.dtd">



<beans default-autowire="autodetect">



   <!-- =============================== Interceptors
================================= -->

   <bean id="timeLoggingAdvice" class="advice.TimeLoggingInterceptor"/>



   <!-- =============================== Advisor
====================================== -->

   <bean id="timeLoggingAdvisor" class="
org.springframework.aop.support.NameMatchMethodPointcutAdvisor">

       <property name="mappedNames">

         <list>

           <value>execute</value>

         </list>

        </property>

       <property name="advice">

           <ref bean="timeLoggingAdvice"/>

       </property>

   </bean>



   <!-- ============================== Spring Beans
================================= -->



   <bean id="helloWorld" class="example.HelloWorld" singleton="false"/>



   <bean id="Menu" class="
org.springframework.aop.framework.ProxyFactoryBean">

       <property name="proxyInterfaces">

           <value>example.IMenu</value>

       </property>

       <property name="interceptorNames">

           <list>

               <value>timeLoggingAdvisor</value>

           </list>

       </property>

       <property name="target">

           <ref local="MenuTarget"/>

       </property>

   </bean>



   <bean id="MenuTarget" class="example.Menu" singleton="false"/>



 </beans>



4) Implemented ModelDriven, Preparable and SessionAware interfaces in
Menu.java:



  package example;



import java.util.Map;



import org.apache.struts2.interceptor.SessionAware;



import com.opensymphony.xwork2.ModelDriven;

import com.opensymphony.xwork2.Preparable;



/**

* <code>Set welcome message.</code>

*/

public class Menu extends ExampleSupport implements IMenu, ModelDriven,
Preparable, SessionAware {



       private Map _session;

       private User user;



  public String execute() throws Exception {



    return SUCCESS;

  }



       public Object getModel() {

               return user;

       }



       public void prepare() throws Exception {

               if (getSession().get("user") != null) {

                       user = (User) getSession().get("user");

               } else {

                       user = new User();

               }

       }



       public void setSession(Map session) {

               _session = session;

       }



       public Map getSession() {

               return _session;

       }

}







When I let Spring manages the creation of objects, the methods for the
implemented interfaces such as getModel() and prepare() are not invoked. If
I change the class="Menu" to class="example.Menu", the interfaces methods
were invoked since I am not letting spring manages object creation.



Could someone let me know what I may have done wrong? How can I use Spring
and Struts at the same time without losing either functionality?



Any help is greatly appreciated.



Thanks!

Lee
©2008 junlu.com - Jax Systems, LLC, U.S.A.