First, spring and struts need to be integrated into the Java Web, so you need to register struts and spring in XML.
Second, the key to spring and struts consolidation is that the action class in struts is being created by the spring IOC container.
(1) Add jar Package
(2) configuration
(3) Action code Preparation
(4) Register the action (in spring and struts, respectively)
(5) Add JSP page
1. Add Jar Package
Introducing the Struts jar package and the Spring Web jar package
Struts's jar package (struts-2.3.29)
Commons-fileupload-1.3.1.jar Commons-io-2.2.jar Commons-lang3-3.2.jar Commons-logging-1.1.3.jar Freemarker-2.3.22.jar Javassist-3.11.0.ga.jar Ognl-3.0.17.jar Struts2-core-2.3.29.jar Xwork-core-2.3.29.jar
|
Spring-web's jar Package
Spring-web-3.2.5.release.jar (belongs to spring) Struts2-spring-plugin-2.3.29.jar (belongs to Struts2)
|
2. Configuration
2.1. Registering struts and spring in Web. xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "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" ><!-- Register spring --><context-param><param-name>contextconfiglocation</param-name>< param-value>classpath:applicationcontext.xml</param-value></context-param><listener>< Listener-class>org.springframework.web.context.contextloaderlistener</listener-class></listener ><!-- Registration struts --><filter><filter-name>struts</filter-name>< filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>< /filter><filter-mapping><filter-name>struts</filter-name><url-pattern>*.action</ url-pattern></filter-mapping><display-name>tax system</display-name><welcome-file-list><welcome-file>index.jsp </welcome-file></welcome-file-list></web-app>
2.2. Add Struts.xml File
<?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><!-- Disable dynamic method access -- > <constant name= "Struts.enable.DynamicMethodInvocation" value= "false" /> <!-- Configuration into development mode --> <constant Name= "Struts.devmode" value= "true" /><!-- configuration extension named Action --><constant name= "Struts.action.extention" value= "action" /><!-- Configure the theme to Simple --><constant name= "Struts.ui.theme" value= "simple" /><!-- set up factory to create objects --><constant name= "Struts.objectfactory" value= "Spring" /> <!-- <package name= "Default" namespace= "/" extends= "Struts-default" > <action name= " class=" "> <result name=" Success " ></result> </action> < /package> --> </struts>
3. Action Code Preparation
package com.rk.test.action;import java.util.map;import com.opensymphony.xwork2.action;import com.opensymphony.xwork2.actioncontext;import com.opensymphony.xwork2.actionsupport;import com.rk.test.entity.person;import com.rk.test.service.personservice;public class personaction extends actionsupport { Private personservice personservice;public void setpersonservice (PersonService Personservice) {this.personservice = personservice;} Public string findbyid () throws Exception {String id = " 4028d081564ba79401564ba7968e0000 "; Person p = personservice.findbyid (ID); Actioncontext context = actioncontext.getcontext (); map<string,object> request = (map<string, object>) context.get ("Request") ; Request.put ("person", p); return action.success;}}
4. Register the action (in spring and struts, respectively)
4.1. Registering the action class in spring
Register the action class in the Bean-action.xml file. Note: The action registration is to use scope= "prototype".
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans " xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance " xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "http// Www.springframework.org/schema/context " xmlns:tx=" Http://www.springframework.org/schema /tx " xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP " xsi: Schemalocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd &nbSp;http://www.springframework.org/schema/tx http://www.springframework.org/schema /tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <bean id= "Personaction" class= "Com.rk.test.action.PersonAction" scope= "prototype" >< Property name= "Personservice" ref= "Personservice" ></property></bean></beans>
Register Bean-action.xml in the Applicationcontext.xml file
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans " xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance " xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "http// Www.springframework.org/schema/context " xmlns:tx=" Http://www.springframework.org/schema /tx " xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP " xsi: Schemalocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd &nbSp;http://www.springframework.org/schema/tx http://www.springframework.org/schema /tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <import resource= "Classpath:bean-base.xml"/><import resource= "classpath:com/rk/*/config/ Bean-*.xml "/></beans>
In fact, we did not modify the content of Applictioncontext.xml, because the following sentence
<import resource= "Classpath:com/rk/*/config/bean-*.xml"/>
Because it uses the "*" number to match
4.2. Registering the action class in struts
Registering an action class in a Struts-test.xml file
<?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> <package name=" test_package "namespace="/test "extends=" Struts-default "> <action name= "psn_*" class= "personaction" method= "{1}" > <result name= "Success" >/web-inf/jsp/test/ Success.jsp</result> </action> </package></struts>
Registering the Struts-test.xml file with the Struts.xml file
<?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><!-- Disable dynamic method access -- > <constant name= "Struts.enable.DynamicMethodInvocation" value= "false" /> <!-- Configuration into development mode --> <constant Name= "Struts.devmode" value= "true" /><!-- configuration extension named Action --><constant name= "Struts.action.extention" value= "action" /><!-- Configure the theme to Simple --><constant name= "Struts.ui.theme" value= "simple" /><!-- set up factory to create objects --><constant name= "Struts.objectfactory" value= "Spring" /> <include file= "Com/rk/test/config/struts-test.xml"/><!-- &Nbsp;<package name= "Default" namespace= "/" extends= "Struts-default" > <action name= " class=" "> <result name= "Success" ></result> </action> </package> --> </struts>
The point is to add the following sentence
<include file= "Com/rk/test/config/struts-test.xml"/>
5. Add JSP page
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><% @taglib uri= "/struts-tags" prefix= "s" %><! doctype html public "-//w3c//dtd html 4.01 transitional//en" >Access address
Http://127.0.0.1:8080/tax/test/psn_findById.action
Show results
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/85/76/wKiom1ekMYHxwjbUAAATW-1eus0692.png "title=" Psn_ Findbyid.png "alt=" Wkiom1ekmyhxwjbuaaatw-1eus0692.png "/>
SSH series: (6) Integrate spring and struts