JavaBeat
Search JavaBeat

JAVABEAT
home
articles
tips
QnA
Books
forums
ARTICLE TOPICS
All Articles
Java 5.0
Java 6.0
EJB 3.0
JCA
Struts
JSF
Spring
Groovy
JBoss Seam
Hibernate
Eclipse
JavaFx
Google Guice
J2ME
GWT
WebServices
AJAX
ARCHIVE
2007 | 12 11 10 09 08 07 06 05 04 03
2008 | 07 06 05 04 03 02 01
CERTIFICATION KITS
350 SCJP 1.5 Mock Exams
400 SCJP 1.6 Mock Exams
300 SCWCD 5.0 Mock Exams
300 SCBCD 5.0 Mock Exams
Enter email address:

Latest JavaBeat Articles Delivered by FeedBurner
OUR NETWORK
javabeat
planetoss
Sponsors
Java Jobs eCommerce software Get Ubuntu 8.04 Get FireFox 3.0  

Pages : 1 2 3 4 5

Integrating Struts With Spring

Author : MunafSahaf
Date : Sun May 13th, 2007
Topic : spring struts 
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.

 
Pages : 1 2 3 4 5
 

Sponsors
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
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 Media (2004-2008), India
javabeat | planetoss | links directory | advertise
Copyright (2004 - 2008), JavaBeat