码迷,mamicode.com
首页 > 编程语言 > 详细

重新学习之spring第三个程序,整合struts2+spring

时间:2014-09-01 12:11:13      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   java   strong   

第一步:导入Struts2jar包+springIOC的jar包和Aop的Jar包

bubuko.com,布布扣

第二步:建立applicationContext.xml文件+struts.xml文件+web.xml文件

web.xml

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   
 8     <context-param>
 9               <description>
10                 将applicationContext.xml放在src目录下,依然能够找到该配置文件
11             </description>
12           
13             <param-name>contextConfigLocation</param-name>
14               <param-value>classpath:applicationContext.xml</param-value>
15     </context-param>
16   
17     <listener>
18           <description>
19             项目启动时,创建Ioc容器,将项目下所有费数据类创建对象,并注入,建立对象之间的关系 
20         </description>
21           
22         <listener-class>
23             org.springframework.web.context.ContextLoaderListener</listener-class>
24     </listener>
25   
26   
27   
28   <filter>
29         <filter-name>struts2</filter-name>
30         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
31     </filter>
32 
33     <filter-mapping>
34         <filter-name>struts2</filter-name>
35         <url-pattern>/*</url-pattern>
36     </filter-mapping>
37   
38 </web-app>
View Code

applicationContext.xml

 

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4         xmlns:aop="http://www.springframework.org/schema/aop"
 5         xmlns:tx="http://www.springframework.org/schema/tx"
 6         xsi:schemaLocation="
 7             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 8             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
 9             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
10     
11     <!-- 
12         lazy-init:true 懒加载,初始ioc化容器的时候,不建立对象
13         lazy-init:false(默认) 不懒加载,初始化ioc容器的时候,就讲applicationContext.XML中所有配置的类的对象创建,并建立关系
14         scope:prototype每次取对应id的类的对象,都新创建一个
15         scope:singleton(默认)类似于单例模式,只要容器初始化一个类对象,以后所有的取用,都是取这一个
16       -->
17       
18       <!-- 通过setter方法,依赖注入对象。控制反转 -->
19     <bean id="UserAction " class="com.bjsxt.sxf.action.UserAction"  scope="prototype" >
20         <property name="userDao" ref="UserDao" ></property>
21     </bean>
22     <bean id="UserDao" class="com.bjsxt.sxf.dao.UserDao" scope="prototype"></bean>
23     
24     
25     
26     <!-- 定义一个切面(连接点,切入点,通知) -->
27     <bean id="log" class="com.bjsxt.sxf.aop.MyAop"></bean>
28     <aop:config >
29         <!-- 切入点,将dao包下所有有参数或无参数传入的,有返回值或无返回值类型的公共方法,进行代理。 -->
30         <aop:pointcut expression="execution(public * com.bjsxt.sxf.dao.*.*(..))" id="logCut"/>
31         <!-- 通知,也就是代理对象中增强功能代码的编写 -->
32         <aop:aspect ref="log">
33             <!-- 环绕通知,有参数的传递,可对参数传递进行处理 -->
34             <aop:around method="aroundRun" pointcut-ref="logCut"/>
35         </aop:aspect>
36     </aop:config>
37 </beans>
View Code

 

struts.xml

 

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <!-- 如果请求地址=actionName!methodName ,则该配置需要进行设置,否则访问地址错误-->
 8     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 9     
10     <!-- 开发模式 -->
11     <constant name="struts.devMode" value="true" />
12     
13     <!-- 编码格式过滤 -->
14     <constant name="struts.i18n.encoding" value="utf-8"></constant>
15     
16    <!-- 告诉struts.xml不要自己通过反射new,对象,去spring的ioc容器中找
17             action中的class=‘spring中Ioc容器中对象的id‘
18             annotation注解生成对象默认情况下id值为是:类名首字符小写
19             需要加jar包struts-spring-plugin..jar
20          -->
21     <constant name="struts.objectFactory" value="spring"></constant>
22     
23     
24     <package name="default" namespace="/" extends="struts-default">
25         <!-- actionName!methodName请求方式的配置 -->
26         <action name="userAction" class="UserAction">
27             <result name="success">/page/success.jsp</result>
28         </action>
29     </package>
30 </struts>
View Code

第三步:测试(省略相关代码。)

bubuko.com,布布扣

bubuko.com,布布扣

 

重新学习之spring第三个程序,整合struts2+spring

标签:des   style   blog   http   color   os   io   java   strong   

原文地址:http://www.cnblogs.com/shangxiaofei/p/3948792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!