Spring and struts provide good support for the Model-view-controller model. Struts, by contrast, is a relatively simple MVC framework in which developers prefer to use struts to complete the design of MVC, making it necessary to integrate spring with struts.
The key to consolidation is that the instance generation of the action in struts is no longer owned by struts, but is delivered to the spring container for management. Therefore, a technical prerequisite for consolidation is that the Actionservlet as controller in struts must be able to mount the application environment of spring. And Spring's Org.springframework.web.struts.ContextLoaderPlugin provides this support exactly. In the configuration file struts-config.xml of struts, the Contextloaderplugin is registered, as the following example:
<?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>
...
<plug-in className=”org.springframework.web.struts.ContextLoaderPlugin”>
<set-property property=”contextConfigLocation” value=”/WEB-INF/config.xml”/>
</plug-in>
...
</struts-config>
The Config.xml is the configuration file for spring.
After that, we can incorporate spring and struts in one of three ways:
The 1.Struts action inherits the Actionsupport class of spring and takes spring's applicationcontext in action. This is the simplest form of integration, but has three drawbacks: first, struts and spring are tightly coupled and cannot be changed to other IOC containers; second, the Spring AOP feature is difficult to use; third, there is nothing for struts applications that require the use of dispatchaction.
2. In struts ' configuration file, replace struts ' Requestprocessor class with Spring's Delegatingrequestprocessor class, and define it in spring's configuration file with the Struts profile < Action-mappings> the corresponding bean, separating the action of struts from spring and placing the action of struts under spring control. The advantage of this consolidation is that it will no longer depend on the particular IOC container of spring, but must rely on the Requestprocessor class of struts.
3. Through spring's Delegatingactionproxy class proxy struts action, that is, in the struts configuration file, define <action-mappings> The type attribute is changed to Delegatingactionproxy instead of the specific class name, and the bean that corresponds to the Struts action map is defined in the spring configuration file, thus separating the action of struts from spring. And put the action of struts under spring's control. Undoubtedly, this is the most flexible way of integration.
The following is a helloworld example to analyze the implementation steps of the third integration approach.
Step 1: Modify struts configuration file Struts-config.xml
<?xml version= "1.0" encoding= "iso-8859-1"
<! DOCTYPE struts-config Public "-//apache Software foundation//dtd struts Configuration 1.2//en" "http:// Jakarta.apache.org/struts/dtds/struts-config_1_2.dtd "
<struts-config>
<form-beans>
<form-bean name= "HelloWorld" type= "Com.strutstest.action.HelloWorld"/>
</form-beans>
< Action-mappings>
<!--Note that the type attribute here is defined as Delegatingactionproxy class;
<action path= "/helloworld" type= " Org.springframework.web.struts.DelegatingActionProxy "Name=" HelloWorld "validate=" true "input="/web-inf/jsp/ Input.jsp ""
<forward name= "index" path= "/web-inf/jsp/index.jsp"/>
<forward name= "show" path= " web-inf/jsp/show.jsp "/>
</action>
<action
path="/input "
type=" Org.apache.struts.actions.ForwardAction "
parameter="/web-inf/jsp/input.jsp "/>
</action-mappings
<!--registered Contextloaderplugin
<plug-in classname= "Org.springframework.web.struts.ContextLoaderPlugIn ""
<set-property property= "Contextconfiglocation" Value= "/web-inf/config.xml"/>
</plug-in>
<message-resources parameter= "Messages"/>
< /struts-config>