Satellite Internet QuickBooks Advice international calling cards calling cards
JavaBeat Certifications Certifications Kits Articles Tips QNA Interview Questions SCJP 1.5 SCBCD 5.0 Java/J2EE Feeds
Java Interview Questions Submit Your Blog Feedback Request Article Print Email

Integrating Struts With Spring

Author : MunafSahaf
Date : Sun May 13th, 2007
Topic : spring struts 
Pages :
Enter email address:

Latest JavaBeat Articles Delivered

B) Delegate Struts Action management to the Spring framework.

A much better solution is to delegate Struts action management to the Spring framework. You can do this by registering a proxy in the struts-config action mapping. The proxy is responsible for looking up the Struts action in the Spring context. Because the action is under Spring's control, it populates the action's JavaBean properties and leaves the door open to applying features such as Spring's AOP interceptors.

The delegation method of Spring integration


<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
 <form-beans>
    <form-bean name="searchEmpForm" 
      type="org.apache.struts.validator.DynaValidatorForm">
               <form-property name="empId"    type="java.lang.String"/>
    </form-bean>
   </form-beans>
 <global-forwards type="org.apache.struts.action.ActionForward">
     <forward   name="welcome”                path="/index.do"/>
     <forward   name="searchEmployee"            path="/searchEmp.do"/>
     <forward   name="submitSearch"           path="/submitSearch.do"/>
 </global-forwards>
 <action-mappings>
    <action    path="/welcome" forward="/WEB-INF/views/welcome.htm"/>
    <action    path="/searchEmployee" forward="/WEB-INF/views/search.jsp"/>
    <action    path="/submitSearch" 
                   type="org.springframework.web.struts.DelegatingActionProxy" (1)
                   input="/searchEmployee.do"
                   validate="true"
                   name="searchEmpForm">
              <forward name="success" path="/WEB-INF/views/empDetail.jsp"/>
              <forward name="failure" path="/WEB-INF/views/search.jsp"/>
    </action>  
 </action-mappings>
 <message-resources parameter="ApplicationResources"/>
 <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property 
    property="pathnames" 
    value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
 </plug-in>
 <plug-in 
    className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/>
 </plug-in>
</struts-config>

Explaination:

This is simply a struts config file, but here we do not declare the action’s class name but we set the Spring’s DelegatingActionProxy. The DelegatingActionProxy will lookup context for the action name in the spring’s config and map it to its class. The context is declared in the ContextLoaderPlugIn.

Now just register the Struts action as a bean in the Spring config file. The properties for this action’s bean will be automatically populated just like any other Spring bean.

Register a Struts action in the Spring context


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="empSearchService" class="net.javabeat.example1.employee.business.EmpServiceImpl"/>
  <bean name="/submitSearch"   
        class="net.javabeat.example1.employee.actions.SubmitSearch">
     <property name="empSearchService">
        <ref bean="empSearchService"/>
     </property>
  </bean>
</beans>

Disadvantages:

If you have the option to choose a method then this is the best method the only disadvantage here is this method leaves your code less readable because the dependencies are not clear.

Submit Your Blog Feedback Request Article Print Email

Favorites
C# problem error
Free Newsgroups
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Website (2004-2009), India
javabeat | about us | useful resources
Copyright (2004 - 2009), JavaBeat