Spring整合Struts2
生活随笔
收集整理的這篇文章主要介紹了
Spring整合Struts2
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
①導(dǎo)入Struts2 jar包
②在web.xml文件中創(chuàng)建過濾器
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"><display-name></display-name> <!-- 配置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-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list> </web-app>
③創(chuàng)建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="struts.devMode" value="true" /><package name="default" namespace="/" extends="struts-default"><!-- Spring 整合 Struts2 時, 在 Struts2 中配置的 Spring 的 Action 的 class 需要指向 IOC 容器中該 bean 的 id--><action name="person-save" class="personAction"><result>/success.jsp</result></action></package></struts>
④創(chuàng)建PersonService類
public class PersonService {public void save(){System.out.println("person's save.....");}}
⑤創(chuàng)建PersonAction類
import com.atguigu.spring.struts.service.PersonService;public class PersonAction {private PersonService personService;public void setPersonService(PersonService personService) {this.personService = personService;}public String execute(){System.out.println("execute.....");return "SUCCESS";}}
⑥導(dǎo)入Spring的jar包
⑦創(chuàng)建applicationContext.xml文件
<?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"><bean id="person" class="com.atguigu.spring.struts.beans.Person"><property name="username" value="spring"></property></bean><bean id="personService" class="com.atguigu.spring.struts.service.PersonService"></bean><!-- 注意在IOC容器中配置Struts2的Action時組要配置scope屬性 --><bean id="personAction" class="com.atguigu.spring.struts.actions.PersonAction" scope="prototype"><property name="personService" ref="personService"></property></bean></beans>
⑧導(dǎo)入一個jar包——struts2-spring-plugin-2.3.15.3.jar
?
OK!
?
轉(zhuǎn)載于:https://www.cnblogs.com/sdnu-zhang/p/8528417.html
總結(jié)
以上是生活随笔為你收集整理的Spring整合Struts2的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Educational Codeforc
- 下一篇: 求一个影子个性签名