1: Use spring's own listener
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 "> <!--Configure the Spring configuration file name and location-- <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpa Th:applicationcontext.xml</param-value> </context-param> <!--launch Servletcontextlistener of the IOC container- <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!--configuration Struts2 Filter--<filter> <filter-name>struts2< ;/filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pat Tern>/*</url-pattern> </filter-mapping></web-app>
2: Add the jar package. Get the name and location of the spring configuration file by: Classpath: That's it. Spring automatically helps us to load it. and put it in the ServletContext.
3: Writing class
package Com.struts.spring.beans; public class person { private String username; public void Setusername (String username) { this . Usern Ame = username; public void Hello () {System.out.println (" My name is "+ username); } }
Package com.struts.spring.services; Public class Personservice { publicvoid Save () { System.out.println ("Test service layer" ); }}
Packagecom.struts.spring.actions;ImportCom.struts.spring.services.PersonService; Public classpersonaction {PrivatePersonservice Personservice; Public voidSetpersonservice (Personservice personservice) { This. Personservice =Personservice; } PublicString Execute () {System.out.println ("Action Layer"); Personservice.save (); return"Success"; }}
4: Configure Applicationcontext.xml
class= "Com.struts.spring.beans.Person" > <property name= "username" value= "is Lu Wei" ></property > </bean> class= "Com.struts.spring.services.PersonService"/> class= " Com.struts.spring.actions.PersonAction "scope=" prototype "> <property name=" Personservice "ref=" Personservice "></property> </bean>
Knowledge Point: Note: When configuring the Struts2 Action in an IOC container, the scope property needs to be configured with a value of prototype. each time a new object is regenerated to the requester .
5: Write Struts.xml
<?xml version= "1.0" encoding= "UTF-8"? ><!DOCTYPE Struts public"-//apache software foundation//dtd Struts Configuration 2.3//en" "Http://struts.apache.org/dtds/struts-2.3.dtd" > <struts> <constant name= "Struts.enable.DynamicMethodInvocation" value= "false"/> <constant name= "Stru Ts.devmode "value=" true "/> < PackageName= "Default" namespace= "/"extends= "Struts-default" > <!--when spring consolidates Struts2, the spring Action that is configured in Struts2classneed to point to the ID of the bean in the IOC container--<action name= "Person-save"class= "Personaction" > <result>/success.jsp</result> </action> </ Package></struts>
Spring Integrated STRUTS2