Spring整合struts
一、spring创建action
1.编写action类,并将其配置给spring ,spring可以注入service
2.编写struts.xml
struts xml文件,action标签class属性不用全限定名。
<struts> <!-- 开发模式 --> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- 底层自动从spring容器中通过名称获得内容, getBean("userAction") --> <action name="userAction_*" class="userAction" method="{1}"> <result name="success">/messag.jsp</result> </action> </package> </struts>
3.表单jsp页面
4.web.xml 配置
4.1.确定配置文件contextConfigLocation
4.2.配置监听器 ContextLoaderListener
4.3.配置前端控制器 StrutsPrepareAndExecuteFitler
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"> <!-- 确定spring xml位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- struts前端控制器 --> <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> </web-app>
二、struts创建action
1.struts配置文件中action标签class属性为类全限定名。
2.Action类中,必须提供service名称,并且与 spring配置文件一致。(如果名称一样,将自动注入)
3.spring文件中不需要action bean。
关键在于default.properties中struts.objectFactory的属性值的设置,SSH整合依赖包struts-spring-plugin.jar中struts-plugin中配置了struts.objectFactory属性为spring。
专注搬砖,擅长搬砖砸自己的脚~~~
Email:
ltwbuaa@163.com

浙公网安备 33010602011771号