Spring 学习日记 (四) Spring 整合Mybaits 和 struts2 框架的配置文件

时间:2022-03-23 20:32:04



其实也是挺无奈的  这东西永远这一棒子那一锤子的  太不系统了 哎 没办法 跟着项目走吧


首先准备的JAR包


Spring 学习日记 (四) Spring 整合Mybaits 和 struts2 框架的配置文件



需要配置的几个配置文件  


配置spring


applicationContext.xml  


配置mybaits


mybatis-config.xml


配置Struts2


struts.xml 


配置web


web.xml


配置映射文件


xxxxxxMapper.xml 


这些清楚了  就来看每个配置文件具体的设置


-------------------------------我是无耻的分割线 啦啦啦啦啦啦啦啦啦--------------------------


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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<!-- 采用c3p0数据源 这个是在企业中用的比较多的一个数据源 -->
<!-- destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="user" value="luob"/>
<property name="password" value="luob"/>
<!-- 连接池中的最大连接数 -->
<property name="maxPoolSize" value="150"/>

<!-- 连接池中的最小连接数 -->
<property name="minPoolSize" value="1"></property>

<!-- 初始化连接池中的 连接数,取值 在 minPoolSize 和 maxPoolSize 之间,default:3-->
<property name="initialPoolSize" value="3"/>

<!-- 最大空闲时间,60s内该连接没有被使用则被丢弃,若为0 永不丢弃.default:0 -->
<property name="maxIdleTime" value="60"/>

<!-- 当连接数不够时,每次同时创建多少个连接 -->
<property name="acquireIncrement" value="1"/>

<!-- 每60s检查连接池中的所有空间连接,如果没有被使用,就被放弃, default:0 -->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>

<!-- 从c3p0数据源中抽取出JDBC的代理对象-->
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor" lazy-init="true" />

<!--9i: org.springframework.jdbc.support.lob.OracleLobHandler -->
<!--10g以后:org.springframework.jdbc.support.lob.DefaultLobHandler(mysql,DB2等都可以用这个) -->
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
<!-- 9i: 指定操作lob类型数据的jdbc代理对象 如果上面的 lobHandler 换了下面的就不需要了 -->
<property name="nativeJdbcExtractor">
<ref local="nativeJdbcExtractor" />
</property>
</bean>

<!-- 使用jdbc 来管理事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 配置 mybatis 的sqlSessionFactory 由 spring 的 SqlSessionFactoryBean 代理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

<!-- 使用spring 的 SqlSessionTemplate 创建一个 可以批量操作的sqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>


<!-- =============================== -->
<!-- ///////// dao 的配置 /////-->
<!-- =============================== -->
<bean id="studentDAO" class="com.mybatis.student.IStudentDAOImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>

<!-- 使用 sqlSessionTemplate 创建的 sqlSession -->
<bean id="studentDAO1" class="com.mybatis.student.IStudentDAOImpl_sqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

<bean id="studentDAO2" class="com.mybatis.student.IStudentDAOImpl_sqlSessionDaoSupport">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<!-- 或者 使用 sqlSessionTemplate 如果两个都配置了 会忽略 sqlSessionFactory -->
</bean>

<!-- 采用MapperFactoryBean -->
<bean id="classesDAO" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.mybatis.classes.IClassesDAO"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- =============================== -->
<!-- ///// Serivce 的配置 /////-->
<!-- =============================== -->
<bean id="studentService" class="com.mybatis.student.IStudentServiceImpl">
<property name="sudentDAO" ref="studentDAO"/>
</bean>

<bean id="classesService" class="com.mybatis.classes.IClassesServiceImpl">
<property name="classesDAO" ref="classesDAO"/>
</bean>

</beans>

Mybatis-config.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<!-- 配置的元素顺序 properties?, settings?, typeAliases?, typeHandlers?,
objectFactory?, objectWrapperFactory?, proxyFactory?, plugins?,
environments?, databaseIdProvider?, mappers -->

<!-- 配置mybatis的缓存,延迟加载等等一系列属性 -->
<settings>
<!-- 全局映射器启用缓存 -->
<setting name="cacheEnabled" value="true" />
<!-- 查询时,关闭关联对象即时加载以提高性能 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
<setting name="aggressiveLazyLoading" value="false" />
<!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
<setting name="multipleResultSetsEnabled" value="true" />
<!-- 允许使用列标签代替列名 -->
<setting name="useColumnLabel" value="true" />
<!-- 允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
<!-- <setting name="useGeneratedKeys" value="true" /> -->
<!-- 给予被嵌套的resultMap以字段-属性的映射支持 -->
<setting name="autoMappingBehavior" value="FULL" />
<!-- 对于批量更新操作缓存SQL以提高性能 -->
<setting name="defaultExecutorType" value="SIMPLE" />
<!-- 数据库超过25000秒仍未响应则超时 -->
<setting name="defaultStatementTimeout" value="25000" />
</settings>

<!-- 使用属性文件 而且可以在这里这是 覆盖文件中的值 -->


<!-- 别名的配置 -->
<typeAliases>
<typeAlias type="com.mybatis.student.Student" alias="Student"/>
<typeAlias type="com.mybatis.classes.Classes" alias="Classes"/>
<!--
也可以使用 包范围来配置
<package name="com.mybatis"/>
-->
</typeAliases>

<!-- 环境的配置 -->

<!-- 映射文件的配置 -->
<mappers>
<mapper resource="com/mybatis/student/StudentMapper.xml"/>
<mapper resource="com/mybatis/classes/ClassesMapper.xml"/>
</mappers>

</configuration>

Stutrs.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>
<package name="student" namespace="/student" extends="struts-default">
<action name="allStudent" class="com.mybatis.action.StudentAction" method="getAllStudent">
<result name="success">/index.jsp</result>
</action>
<action name="updateAndSelect" class="com.mybatis.action.StudentAction" method="getAllStudentAfterupdate">
<result name="success">/index.jsp</result>
</action>
<action name="delStudentById" class="com.mybatis.action.StudentAction" method="delStudentById">
<result name="success">/index.jsp</result>
</action>
</package>

<package name="classes" namespace="/classes" extends="struts-default">
<action name="queryClassesById" class="com.mybatis.action.ClassesAction" method="queryClassesById">
<result name="success">/index1.jsp</result>
</action>
<action name="delClassesById" class="com.mybatis.action.ClassesAction" method="delClassesById">
<result name="success">/index1.jsp</result>
</action>
</package>
</struts>

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">

<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>
<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>

xxxxxxxxxMapper.xml  这里援引某位同学的代码 举个栗子


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.student">
<!-- <!ELEMENT mapper (
cache-ref | cache | resultMap* | parameterMap* | sql*
| insert* | update* | delete* | select* )+> -->

<!-- 设置缓存 如果用户需要登录 需要设置这种类型 type=org.mybatis.caches.oscache.LoggingOSCache-->
<cache eviction="FIFO" readOnly="true" size="256" flushInterval="60000"/>

<!-- 定义可以重用的sql 代码片段 -->
<sql id="studentColumns">sid,sname,score</sql>

<!-- 自定义结果集 -->
<resultMap type="Student" id="studentResultMap">
<id property="sid" column="SID"/>
<result property="sname" column="SNAME"/>
<result property="score" column="SCORE"/>
</resultMap>

<resultMap type="Student" id="studentAllResultMap">
<id property="sid" column="SID"/>
<result property="sname" column="SNAME"/>
<result property="major" column="MAJOR"/>
<result property="birth" column="BIRTH"/>
<result property="score" column="SCORE"/>
<result property="cid" column="CID"/>
<result property="status" column="STATUS"/>
</resultMap>

<!-- 只用构造函数 创建对象 对于那些 比较少的列 -->
<resultMap type="Student" id="studentAndClassesResultMap">
<constructor>
<idArg column="SID" javaType="int"/>
<arg column="SNAME" javaType="String"/>
<arg column="SCORE" javaType="float"/>
</constructor>
<association property="classes" javaType="Classes" resultMap="com.mybatis.classes.classesResultMap"/>
</resultMap>


<select id="selectStudentAndClassBySname" parameterType="String" resultMap="studentAndClassesResultMap">
select s.sid,s.sname,s.score,c.cid,c.cname,c.teacher,c.createdate from student s left join classes c on s.cid=c.cid where s.sname=#{sname}
</select>

<insert id="addStudentBySequence" parameterType="Student" >
<selectKey keyProperty="sid" resultType="int" order="BEFORE">
select STUDENT_SEQ.nextVal from dual
</selectKey>
insert into student(sid,sname,major,birth,score)
values (#{sid},#{sname},#{major},#{birth},#{score})
</insert>

<insert id="addStudent" parameterType="Student">
insert into student(sid,sname,major,birth,score)
values (#{sid},#{sname},#{major},#{birth},#{score})
</insert>

<delete id="delStudentById" parameterType="int">
delete student where sid=#{sid}
</delete>

<select id="queryAllStudent" resultType="Student" useCache="true" flushCache="false" timeout="10000">
select * from student order by sid
</select>

<!-- 参数可以指定一个特定的数据类型 还可以使用自定类型处理: typeHandler=MyTypeHandler -->
<select id="queryStudentByName" resultType="Student" parameterType="String">
select * from student where sname like #{property,javaType=String,jdbcType=VARCHAR}
</select>

<!-- 参数可以指定一个特定的数据类型 对于数字类型 ,numericScale=2 用于设置小数类型 -->
<select id="queryStudentById" resultType="Student" parameterType="int">
select * from student where sid=#{property,javaType=int,jdbcType=NUMERIC}
</select>


<update id="updStudentById" parameterType="Student">
update student
<trim prefix="SET" suffixOverrides=",">
<if test="sname !=null">sname=#{sname},</if>
<if test="major !=null">majoir=#{major},</if>
<if test="birth !=null">birth=#{birth},</if>
<if test="score !=null">score=#{score}</if>
</trim>
where sid=#{sid}
</update>

<!-- 在这里 利用了 可重用的sql代码片段 -->
<select id="selectMapResult" resultMap="studentResultMap" parameterType="String">
select <include refid="studentColumns"/> from STUDENT where sname like #{sname}
</select>

<!-- Dynamic Sql 使用 if -->
<select id="selectStudentByDynamicSql" parameterType="Student" resultType="Student">
select * from student
<where>
<if test="sname !=null">
sname like #{sname}
</if>
<if test="sid !=null">
AND sid=#{sid}
</if>
</where>
</select>

<!-- 采用 OGNL 表达式 来配置动态sql 使用trim 去掉 where 中多余的 and 或者 or where choose when otherwise-->
<select id="selectStudentByDynamicSqlChoose" parameterType="Student" resultType="Student">
select * from student
<trim prefix="WHERE" prefixOverrides="AND | OR ">
<choose>
<when test=" sname !=null and sname.length() >0 ">
sname like #{sname}
</when>
<when test="sid !=null and sid>0">
AND sid = #{sid}
</when>
<otherwise>
AND status='1'
</otherwise>
</choose>
</trim>
</select>

<!-- 使用foreach 遍历list 只能小写-->
<select id="selectStudentByIds" resultType="Student">
select * from student
where sid in
<foreach collection="list" item="itm" index="index" open="(" separator="," close=")">
#{itm}
</foreach>
</select>

<!-- 使用foreach 遍历arry 只能小写 -->
<select id="selectStudentByIdArray" resultType="Student">
select * from student
where sid in
<foreach collection="array" item="itm" index="index" open="(" separator="," close=")">
#{itm}
</foreach>
</select>

<parameterMap type="map" id="procedureParam">
<parameter property="sid" javaType="int" jdbcType="NUMERIC" mode="IN" />
<parameter property="sname" javaType="String" jdbcType="VARCHAR" mode="IN" />
<parameter property="studentList" javaType="ResultSet" jdbcType="CURSOR" mode="OUT" resultMap="studentAllResultMap"/>
</parameterMap>
<!--传入map集合参数 ,调用 待用游标存储过程(先执行 修改后然后查询所有) -->
<select id="getAllStudentAfterupdate" statementType="CALLABLE" useCache="false" parameterMap="procedureParam">
{call LUOB.pro_getallstudent(?,?,?)}
</select>

</mapper>

附录Struts2的标签 这个默认的


<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<!--struts2中工厂bean的定义-->

<bean class="com.opensymphony.xwork2.ObjectFactory" name="xwork" />

<bean type="com.opensymphony.xwork2.ObjectFactory" name="struts" class="org.apache.struts2.impl.StrutsObjectFactory" />

<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="xwork" class="com.opensymphony.xwork2.DefaultActionProxyFactory"/>

<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="struts" class="org.apache.struts2.impl.StrutsActionProxyFactory"/>

<!--类型检测bean的定义-->

<bean type="com.opensymphony.xwork2.util.ObjectTypeDeterminer" name="tiger" class="com.opensymphony.xwork2.util.GenericsObjectTypeDeterminer"/>

<bean type="com.opensymphony.xwork2.util.ObjectTypeDeterminer" name="notiger" class="com.opensymphony.xwork2.util.DefaultObjectTypeDeterminer"/>

<bean type="com.opensymphony.xwork2.util.ObjectTypeDeterminer" name="struts" class="com.opensymphony.xwork2.util.DefaultObjectTypeDeterminer"/>

<!--文件上传bean的定义-->

<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="struts" class="org.apache.struts2.dispatcher.mapper.DefaultActionMapper" />

<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="composite" class="org.apache.struts2.dispatcher.mapper.CompositeActionMapper" />

<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="restful" class="org.apache.struts2.dispatcher.mapper.RestfulActionMapper" />

<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="restful2" class="org.apache.struts2.dispatcher.mapper.Restful2ActionMapper" />

<bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="struts" class="org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest" scope="default" optional="true"/>

<bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="jakarta" class="org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest" scope="default" optional="true" />

<!--标签库bean的定义-->

<bean type="org.apache.struts2.views.TagLibrary" name="s" class="org.apache.struts2.views.DefaultTagLibrary" />

<!--一些常用视图bean的定义-->

<bean class="org.apache.struts2.views.freemarker.FreemarkerManager" name="struts" optional="true"/>

<bean class="org.apache.struts2.views.velocity.VelocityManager" name="struts" optional="true" />

<bean class="org.apache.struts2.components.template.TemplateEngineManager" />

<bean type="org.apache.struts2.components.template.TemplateEngine" name="ftl" class="org.apache.struts2.components.template.FreemarkerTemplateEngine" />

<bean type="org.apache.struts2.components.template.TemplateEngine" name="vm" class="org.apache.struts2.components.template.VelocityTemplateEngine" />

<bean type="org.apache.struts2.components.template.TemplateEngine" name="jsp" class="org.apache.struts2.components.template.JspTemplateEngine" />

<!--类型转换bean的定义-->

<bean type="com.opensymphony.xwork2.util.XWorkConverter" name="xwork1" class="com.opensymphony.xwork2.util.XWorkConverter" />

<bean type="com.opensymphony.xwork2.util.XWorkConverter" name="struts" class="com.opensymphony.xwork2.util.AnnotationXWorkConverter" />

<bean type="com.opensymphony.xwork2.TextProvider" name="xwork1" class="com.opensymphony.xwork2.TextProviderSupport" />

<bean type="com.opensymphony.xwork2.TextProvider" name="struts" class="com.opensymphony.xwork2.TextProviderSupport" />


<!-- Struts2中一些可以静态注入的bean,也就是不需要实例化的 -->

<bean class="com.opensymphony.xwork2.ObjectFactory" static="true" />

<bean class="com.opensymphony.xwork2.util.XWorkConverter" static="true" />

<bean class="com.opensymphony.xwork2.util.OgnlValueStack" static="true" />

<bean class="org.apache.struts2.dispatcher.Dispatcher" static="true" />

<bean class="org.apache.struts2.components.Include" static="true" />

<bean class="org.apache.struts2.dispatcher.FilterDispatcher" static="true" />

<bean class="org.apache.struts2.views.util.ContextUtil" static="true" />

<bean class="org.apache.struts2.views.util.UrlHelper" static="true" />

<!-- 定义Struts2默认包-->

<package name="struts-default" abstract="true">

<!-- 结果类型的种类-->

<result-types>

<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>

<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>

<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>

<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>

<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>

<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>

<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>

<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>

<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>

<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />

<result-type name="redirect-action" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>

<result-type name="plaintext" class="org.apache.struts2.dispatcher.PlainTextResult" />

</result-types>



<!--struts2中拦截器的定义-->

<interceptors>

<!--实现在不同请求中相似参数别名的准换-->

<interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>

<!--与Spring整合时自动装配的拦截器-->

<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>

<!--构建一个action链,使当前action可以访问前一个action,与<result-type="chain" />配合使用-->

<interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>

<!--负责类型转换的拦截器-->

<interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>

<!--使用配置的name,value来是指cookies -->

<interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>

<!--负责创建httpSession-->

<interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />

<!--输出调试信息-->

<interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />

<!--扩展引用-->

<interceptor name="externalRef" class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>

<!--后台执行action负责发送等待画面给用户-->

<interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>

<!--异常处理-->

<interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>

<!--文件上传,解析表单域的内容-->

<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>

<!--支持国际化-->

<interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>

<!--日志记录-->

<interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>

<!--模型拦截器,当action实现了ModelDriven接口时,负责把getModel的结果放入valueStack中-->

<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>

<!--有生命周期的ModelDriven-->

<interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>

<!--负责解析请求中的参数,并赋值给action中对应的属性-->

<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

<!--实现该Preparable接口的action,会调用拦截器的prepare方法-->

<interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>

<!--负责将action 标签下的param参数值传递给action实例-->

<interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>

<!--范围转换-->

<interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>

<!--用于访问Servlet API-->

<interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>



<interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>

<!--输出action执行时间-->

<interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>

<!--防止表单重复提交-->

<interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>

<!--与token拦截器相似,只是把token保存到HttpSession-->

<interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>

<!--负责表单字段的验证 *-validation.xml-->

<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>

<!--负责执行action的validate()-->

<interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>

<!--存储和重新获取Action 消息/错误/字段错误为Action,实现ValidationAware接口到seesion-->

<interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />

<!--添加自动checkbox处理代码,这样检探测checkbox和添加它作为一个参数使用默认值(通常’false’).使用一个指定名字隐藏字段探测没提交的checkbox-->

<interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />

<interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />

<!--JAAS服务拦截器-->

<interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />



<!-- 一个基本的拦截器栈 -->

<interceptor-stack name="basicStack">

<interceptor-ref name="exception"/>

<interceptor-ref name="servletConfig"/>

<interceptor-ref name="prepare"/>

<interceptor-ref name="checkbox"/>

<interceptor-ref name="params"/>

<interceptor-ref name="conversionError"/>

</interceptor-stack>



<!-- 简单的validtion和webflow栈 -->

<interceptor-stack name="validationWorkflowStack">

<interceptor-ref name="basicStack"/>

<interceptor-ref name="validation"/>

<interceptor-ref name="workflow"/>

</interceptor-stack>



<!-- 文件上传的拦截器栈 -->

<interceptor-stack name="fileUploadStack">

<interceptor-ref name="fileUpload"/>

<interceptor-ref name="basicStack"/>

</interceptor-stack>



<!-- model-driven 栈 -->

<interceptor-stack name="modelDrivenStack">

<interceptor-ref name="modelDriven"/>

<interceptor-ref name="basicStack"/>

</interceptor-stack>



<!-- action链的拦截器栈 -->

<interceptor-stack name="chainStack">

<interceptor-ref name="chain"/>

<interceptor-ref name="basicStack"/>

</interceptor-stack>



<!-- i18n 拦截器栈 -->

<interceptor-stack name="i18nStack">

<interceptor-ref name="i18n"/>

<interceptor-ref name="basicStack"/>

</interceptor-stack>



<!-- 结合preparable和ModenDriven拦截器-->

<interceptor-stack name="paramsPrepareParamsStack">

<interceptor-ref name="exception"/>

<interceptor-ref name="alias"/>

<interceptor-ref name="params"/>

<interceptor-ref name="servletConfig"/>

<interceptor-ref name="prepare"/>

<interceptor-ref name="i18n"/>

<interceptor-ref name="chain"/>

<interceptor-ref name="modelDriven"/>

<interceptor-ref name="fileUpload"/>

<interceptor-ref name="checkbox"/>

<interceptor-ref name="staticParams"/>

<interceptor-ref name="params"/>

<interceptor-ref name="conversionError"/>

<interceptor-ref name="validation">

<param name="excludeMethods">input,back,cancel</param>

</interceptor-ref>

<interceptor-ref name="workflow">

<param name="excludeMethods">input,back,cancel</param>

</interceptor-ref>

</interceptor-stack>



<!--定义默认的拦截器栈 -->

<interceptor-stack name="defaultStack">

<interceptor-ref name="exception"/>

<interceptor-ref name="alias"/>

<interceptor-ref name="servletConfig"/>

<interceptor-ref name="prepare"/>

<interceptor-ref name="i18n"/>

<interceptor-ref name="chain"/>

<interceptor-ref name="debugging"/>

<interceptor-ref name="profiling"/>

<interceptor-ref name="scopedModelDriven"/>

<interceptor-ref name="modelDriven"/>

<interceptor-ref name="fileUpload"/>

<interceptor-ref name="checkbox"/>

<interceptor-ref name="staticParams"/>

<interceptor-ref name="params">

<param name="excludeParams">dojo\..*</param>

</interceptor-ref>

<interceptor-ref name="conversionError"/>

<interceptor-ref name="validation">

<param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

<interceptor-ref name="workflow">

<param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

</interceptor-stack>


<interceptor-stack name="completeStack">

<interceptor-ref name="defaultStack"/>

</interceptor-stack>


<interceptor-stack name="executeAndWaitStack">

<interceptor-ref name="execAndWait">

<param name="excludeMethods">input,back,cancel</param>

</interceptor-ref>

<interceptor-ref name="defaultStack"/>

<interceptor-ref name="execAndWait">

<param name="excludeMethods">input,back,cancel</param>

</interceptor-ref>

</interceptor-stack>


<interceptor name="external-ref" class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>

<interceptor name="model-driven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>

<interceptor name="static-params" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>

<interceptor name="scoped-model-driven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>

<interceptor name="servlet-config" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>

<interceptor name="token-session" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>



</interceptors>

<!--定义默认拦截器为"defaultStack"-->

<default-interceptor-ref name="defaultStack"/>

</package>


</struts>

这里需要提一下spring xml文件注入的两种标签


constructor-arg:通过构造函数注入


property:通过setxx方法注入


分类说明是这么一个样子

constructor-arg:通过构造函数注入


</pre><pre name="code" class="html"><pre name="code" class="java">    public class Man {

private String name ;
private int age;
private List hobby;
private Map friends;
private Set set;
private boolean ifMarried;

public Man() {

}

public Man(String name, int age,List hobby,Map friends,Set set,boolean ifMarried){
this.name = name;
this.age = age;
this.hobby = hobby;
this.friends = friends;
this.set = set;
this.ifMarried = ifMarried;
}

public String getInfo(){

String info = "姓名:"+this.name+"\n年龄:"+this.age+"\n爱好:"+this.hobby+"\n朋友:"+this.friends+"\n婚否:"+this.ifMarried+"\n其他的:"+this.set;
return info;
}

}


对应上面的java类 在xml这样配置

</pre><pre name="code" class="html"><pre name="code" class="html"><bean id="man" class="com.spring.test.man.Man">
<constructor-arg value="zzy" index="0" >
</constructor-arg>

<constructor-arg value="10" index="1">
</constructor-arg>

<constructor-arg>
<list>
<value>movie</value>
<value>music</value>
</list>
</constructor-arg>

<constructor-arg>
<set>
<value>Lady is GaGa</value>
<value>GaGa is Lady</value>
</set>
</constructor-arg>

<constructor-arg>
<map>
<entry key="liuhua" value="man"></entry>
<entry key="xujinglei" value="female"></entry>
</map>
</constructor-arg>

<constructor-arg index="5" value="0">
</constructor-arg>
</bean>

同时这样还有其他的写法 比如写一个类 有下列参数

private String name;  

private String tel;

private Dept dept;//注意 这里的是另外一个类Dept 所以会在xml中ref 而不是value 切记 切记

private String password;

Dept类


private String dname;  

private String deptno;

那么注入会有下列的形式


第一种方法:根据索引赋值,索引都是以0开头的

<constructor-arg index="0" value="zhangsan" />  
<constructor-arg index="1" value="10086" />
<constructor-arg index="2" ref="dept"/>
<constructor-arg index="3" value="男" />

第二种方法是根据所属类型传值 这种方法要注意取到的值得顺序

<constructor-arg type="java.lang.String" value="张三"   
<constructor-arg type="java.lang.Double" value="10086" />
<constructor-arg type="www.ccav.Dept" ref="dept"/>
<constructor-arg type="java.lang.String" value="男" />

第三种方法:根据参数的名字传值:(推荐用法)


<constructor-arg name="name" value="张三" />  
<constructor-arg name="tel" value="10086" />
<constructor-arg name="dept" ref="dept"/>
<constructor-arg name="password" value="男" />

第四种方法:直接传值直接给参数赋值,这种方法也是根据顺序排的,所以一旦调换位置的话,就会出现bug这种方法已经很原始了

<constructor-arg  value="zhangsan" />  
<constructor-arg value="10086" />
<constructor-arg ref="dept"/>
<constructor-arg value="男" />
</bean>

当然无论上那种方法 Dept也是需要对应的

<bean id="dept" class="www.csdn.spring01.constructor.Dept" >  

<property name="dname" value="北航"/>
<property name="deptno" value="00001"/>
</bean>

同样的所有的配置都要写在

<bean></beans>  

那么另外一种就是 property:通过setxx方法注入。 

public class Doctor {  

private String name;
private String sex;


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}



public String getSex() {
return sex;
}


public void setSex(String sex) {
this.sex = sex;
}


public void init(){
System.out.println("88888888888");
}
public void init(String name,String sex){
this.name = name;
this.sex = sex;
}
}
<pre name="code" class="html">  <bean id="doctor" class="com.spring.test.man.Doctor" init-method="init">  <property name="name" value="doctor"></property>  <property name="sex" value="i don't know"></property></bean>


 

tips  在这里我配置了一个init-method="init"表示在容易实例化这个doctor的时候,调用一个Doctor类的init方法,本来还以为可以通过这个init方法来注入要注入的信息,但是尝试过后才知道这个init方法是不能带参数的。 

以上就是整个配置文件的所有目录的 后面估计会写一些标签的用法和具体的例子吧 其实很多都是网上找到 但是东西万变不离其中 我这里只是理了一下思路 借花献佛 顺便自己留一个笔记 将来不会了 还可以来找