I haven't written Java for more than a year! Forget it! Now I have a project on hand. Just take a look! And bask in the past! Article 1 Integration of several struts and spring! Open-source frameworks have developed rapidly, and Struts 2 is now popular. This integration is about struts 1.29 and spring 2.0. There are still a lot of enterprise applications in this version hierarchy. They are proven to be stable.
Method 1 of struts integration with spring:
Step 1: Load appliationcontext:
Method 1:
Web. xml
<! -- Path of the spring applicationcontext configuration file -->
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value> classpath *: spring/*. xml </param-value>
</Context-param>
<! -- Load spring applicationcontext -->
<Listener>
<Listener-class> org. springframework. Web. Context. contextloaderlistener </listener-class>
</Listener>
Method 2:
Struts-config.xml
<Plug-in classname = "org. springframework. Web. Struts. contextloaderplugin">
<Set-Property = "contextconfiglocation" value = "/WEB-INF/spring-config/applicationcontext. xml"/>
</Plug-in>
Step 2: let action inherit actionsupport
Public class loginaction extends actionsupport {
...
}
Step 3: Call the actionsupport method getwebapplicationcontext to obtain the webapplicationcontext. Use webapplicatiocontext to obtain the service managed by the spring container.
Public class loginaction extends actionsupport {
Public actionforward execute (actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response) throws exception {
// Obtain the spring Context
Applicationcontext context = getwebapplicationcontext ();
// Retrieve the bean managed by spring
Studentservice Ss = (studentservice) Context. getbean ("studentservice ");
...
}
...
}
Struts integration with spring:
Step 1: Load appliationcontext:
Step 2: Use the action proxy class (delegatingactionproxy): The role of delegatingactionproxy is to find the managed action in the spring container Based on the path
Public class loginaction extends action {
Private studentservice SS;
// General IOC Injection
Public void setstudentservice (studentservice SS ){
This. Ss = SS;
}
...
}
Struts-config.xml
<Action Path = "/liststudents" type = "org. springframework. Web. Struts. delegatingactionproxy"/>
Step 3: configure action in the spring configuration file
Applicationcontext. xml
<Bean name = "SS"...>
</Bean>
<Bean name = "/liststudents" class = "...">
<Property name = "SS">
<Ref bean = "SS"/>
</Property>
</Bean>
Struts integrates spring in three ways:
Step 1: Load appliationcontext:
Step 2: configure the new controller delegatingrequestprocessor
Struts-config.xml
<Action Path = "/liststudents"/>
<Controller processorclass = "org. springframework. Web. Struts. delegatingrequestprocessor"/>
When a/liststudents request is received, delegatingrequestprocessor automatically searches for a bean named/liststudents from the context of the spring application.