1. Use spring's delegatingrequestprocessorReplace the requestprocessor of struts.
1) do not set the automatic loading of applicationcontext in Web. XML, In the struts-config.xml through plug-in settings.
<Plug-in classname = "org. springframework. Web. Struts. contextloaderplugin">
<Set-property value = "/WEB-INF/applicationcontext. XML,/WEB-INF/appcontext. xml" property = "contextconfiglocation"/>
</Plug-in>
2) set the replacement class for requestprocessor in the struts-config.xml.
<Controller processorclass = "org. springframework. Web. Struts. delegatingrequestprocessor"> </controller>
3) do not set the type attribute of action in the <action> element in the struts-config.xml.
<Action Path = "/login" input = "/index. jsp"
Validate = "true" Scope = "request">
<Forward name = "Forward" Path = "/success. jsp"> </forward>
</Action>
4) set the bean forwarded by delegatingrequestprocessor in applicationcontext. xml or other spring bean configuration files. This bean is the action class.
<Bean name = "/login" class = "mypack. loginaction" Singleton = "false">
<Property name = "property1" ref = "otherbean"/>
</Bean>
2. Use delegatingactionproxyThis method forwards the request to the action defined in applicationcontext. xml.
1) Same as the first method 1 ).
2) If you try the first method, remove
<Controller processorclass = "org. springframework. Web. Struts. delegatingrequestprocessor"> </controller>
Element.
3) You need to define the type = "org. springframework. Web. Struts. delegatingactionproxy" in the struts-config.xml.
<Action Path = "/login" input = "/index. jsp" Validate = "true"
Scope = "request" type = "org. springframework. Web. Struts. delegatingactionproxy">
<Forward name = "Forward" Path = "/success. jsp"> </forward>
</Action>
4) Same as the first method 4 ).
III.Use spring actionsupport.
Spring's actionsupport inherits from org. Apache. Struts. action. Action.
The subclass of actionsupport can be a global variable of the webapplicationcontext type. You can use getwebapplicationcontext () to obtain this variable.
This is the servlet code:
Public class loginaction extends Org. springframework. web. struts. actionsupport {public actionforward execute (actionmapping mapping, actionform Form, httpservletrequest request, httpservletresponse response) {loginform = (loginform) form; // obtain the webapplicationcontext object webapplicationcontext CTX = This. getwebapplicationcontext (); logindao Dao = (logindao) CTX. getbean ("logindao"); User u = new user (); U. setname (loginform. getname (); U. setpwd (loginform. getpwd (); If (Dao. checklogin (u) {Return Mapping. findforward ("success");} else {Return Mapping. findforward ("error ");}}}Configuration in applicationcontext. xml <beans> <bean id = "logindao" class = "com. Cao. Dao. logindao"/> </beans> |