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.
|