Struts2 and Spring are good open-source frameworks, and spring and STRUTS2 are integrated and developed to combine the two to create better results and leverage efficiency. Here's how to integrate spring with Struts2 to develop. Seven-step strategy:
1, add STRUTS2 support jar package ; Note: Add a struts-spring-plugin (integrated) plug-in package 2, adding spring support jar 3, Write Struts.xml profile Note: The action is managed by spring, so the action class property should be consistent with the action Bean ID configured in spring
<!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.or G/dtds/struts-2.3.dtd "> <Struts> < Packagename= "Default"namespace="/"extends= "Struts-default"> <!--Configure user Action action by spring Maintenance class is the ID of the action bean in spring - <Actionname= "useraction*"class= "Useraction"Method= "{1}"> <resultname= "Success"type= "Redirect">success.jsp</result> <resultname= "Login"type= "Redirect">index.jsp</result> </Action> </ Package> </Struts>
4. Configuring the Struts2 filter in Web. xml
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "2.5"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!--Configuring the STRUTS2 core 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-pattern>/*</Url-pattern> </filter-mapping> <!--Specify where spring configuration files are located - <Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:applicationContext.xml</Param-value> </Context-param> <!--Configuring the Sprig listener - <Listener> <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class> </Listener> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list></Web-app>
5. Write Spring configuration file Applicationcontext.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5 . xsd "> <!--Configure persistence layer new Userdao () - <BeanID= "Userdao"class= "DAO.impl.UserDaoimpl"Scope= "Prototype"></Bean> <!--Create business Tier New Userbiz () - <BeanID= "Userbiz"class= "BIZ.impl.UserBizimpl"Scope= "Prototype"> <!--inject persistence layer DAO iuuserdao Userdao = new Userdao () - < Propertyname= "Userdao"ref= "Userdao"></ Property> </Bean> <BeanID= "Useraction"class= "Action.useraction"Scope= "Prototype"> <!--inject business Layer Biz iuserbiz userbiz = new Userbiz () - < Propertyname= "Userbiz"ref= "Userbiz"></ Property> </Bean></Beans>
6. Configure the spring listener in Web. XML and the location of the specified configuration file (Note: If the file is under Web-inf/applicatoncontext.xml, you do not need to specify it)
<!--Specify where spring configuration files are located - <Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:applicationContext.xml</Param-value> </Context-param> <!--Configuring the Sprig listener - <Listener> <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class> </Listener>
7, write three-layer structure; Inject a layer of the DAO layer into the action layer by injecting each dependent object into a biz in a spring
Spring and STRUTS2 integrated development