Spring Struts2 整合
Spring整合Struts2
整合什么?——用IoC容器管理Struts2的Action
如何整合?
第一步:配置Struts2
1.加入Struts2的jar包。
2.配置web.xml文件。
3.加入Struts2的配置文件struts.xml
第二步:配置Spring
1.加入Spring的jar包
Spring的标准 jar包。
struts-spring-plugin-plugin-2.3.31.jar包。
2.添加Spring的配置文件——beans.xml
3.在Spring的IoC容器中配置Struts2的Action——注意配置中需要把Action配置成非单例模式scope=prototype.
<beans id="infoAction" class="com.itnba.maya.controller.InfoAction" scope="prototype"> <property name="infoService" ref="com.itnba.maya.services.InfoService"></property> </beans>
4.配置struts.xml。把<action>的class属性指向IoC容器相应bean的id。
<action name="*_*" class="infoAction" method="{2}"> <result> {1}/{2}.jsp </result> </action>
5.在Web.xml中添加配置
<!--配置Spring配置文件的名称和位置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!--启动IoC容器的监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
web.xml配置的作用:
1)创建IoC容器的对象。
2)把对象放到Application中。
实例:
导入前面说的各种包
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" 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>Struts Blank</display-name> <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> <!--配置Spring配置文件的名称和位置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!--启动IoC容器的监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
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"> <action name="*_*" class="{1}Action" method="{2}"> <result> {1}/{2}.jsp </result> </action> </package> <include file="example.xml"/> <!-- Add packages here --> </struts>
Action类
package com.itnba.maya.controller; import com.opensymphony.xwork2.ActionSupport; public class InfoAction extends ActionSupport { public String show(){ return SUCCESS; } }
beans.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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <bean class="com.itnba.maya.controller.InfoAction" id="infoAction"></bean> </beans>
jsp页面
运行: