为了使struts2和spring集成,必须下载一个struts2的spring插件,实际上,struts2以上版本已经提供了这些插件,只要下载一个struts2.0以上版本就可以了。
总结一些,配置struts2+spring+ibatis项目开发环境需要的几个jar:
struts2-core-2.0.11.jar(struts2框架)
commons-lang.jar (struts2基础设施1,提供java常用操作API,比如字符串处理,XML解析等)
commons-logging.jar(struts2基础设施2,提供java日志操作API,抽象出日志接口,方便操作)
freemarker-2.3.8.jar(struts2基础设施3,freemarker模板视图支持,struts2标签主题支持)
xwork-2.0.4.jar(struts2基础设施4,命令模式框架支持 )
ognl-2.6.11.jar(struts2基础设施5,OGNL表达式支持)
struts2-spring-plugin-2.0.11.jar (struts2基础设施6,struts集成spring插件)
struts2-sitemesh-plugin-2.0.11.jar(struts2基础设施7,struts集成sitemesh插件)
spring.jar(spring框架)
ibatis-2.3.0.677.jar (ibatis框架)
sitemesh-2.3.jar(sitemesh框架)
以下文件上传需要的jar:
commons-codec.jar
commons-fileupload-1.2.jar
commons-io-1.4.jar
3.配置strut2.xml
/****** /web-inf/struts.xml ******/
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>game_proj</display-name>
<!-- 中文语言支持过滤器 -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.popoann.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<!-- Acegi过滤器 -->
<filter-name>acegiFilterChain</filter-name>
<filter-class>org.acegisecurity.util.FilterToBeanProxy</filter-class>
<init-param>
<param-name>targetClass</param-name>
<param-value>org.acegisecurity.util.FilterChainProxy</param-value>
</init-param>
</filter>
<!-- Acegi过滤器URL映射 -->
<filter-mapping>
<filter-name>acegiFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ActionContextCleanUp过滤器 -->
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- SiteMesh的核心过滤器 -->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Struts核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>struts.action.extension</param-name>
<param-value>htm</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 应用程序启动时,加载SpringIoC容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/beans-*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 欢迎页面 -->
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>
配置struts2拦截器以及spring的bean工厂
3.配置struts.xml
/****** /classes/struts.xml ******/
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
<include file="struts-default.xml"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.multipart.maxSize" value="2000000" />
<package name="action.game" extends="struts-default">
<action name="index" class="_index">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
配置struts.objectFactory常数为spring,标示action类查找路径由spring工厂提供
注意<action name="index" class="_index"> 这段,其中class属性指定spring工厂中的bean的ID号,name属性自然是action的url地址,关于struts2的详细配置,请参考其他文章。
2.配置bean-action.xml(spring工厂)
/****** /web-infor/bean-action.xml ******/
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- ActionBean命名规则 -->
<!-- 1.id: [<文件夹名>_]+无后缀文件名 -->
<!-- 2.class: [包名.]+首字母大写无后缀文件名+Action-->
<!-- Actionbean in "/" -->
<bean id="_index" class="action.IndexAction">
<property name="inforBo" ref="inforBo"/>
</bean>
</beans>
此处,"_index"显然是与struts.xml中action所指定的class属性是对应的。。。。
一个struts2+spring配置过程大致就是这样,struts2和spring配置详细介绍,将在以后的日志中展开。。。