Structs 整合Spring
2015-06-05 21:31
555 查看
之前在学写SSH框架的时候 每个部分是分开学的,所以在Struts下用 Spring 也想当然的用了配置文件 还自作聪明的使用了static 来避免配置文件反复加载
= =然后Action里面就出现了很多奇怪的 ApplicationContextUtil.getBean 来获取对象
现在需要使用到Spring中task 来实现定时任务才发现 Spring可以被整合到Struts中 Struts的action对象也可以交给Spring实现 来达到IOC 控制反转的效果
背景扯完了 下面来点干货吧
首先是配置web.xml 这部的目的是在web项目启动的时候扫描配置文件,配置的内容就是给web项目加一个监听器
这一步需要加包 struts2-spring-plugin.jar 分析配置项也能知道需要这个包
这样我们的配置工作就完成了
最后放上demo
spring 中配置bean
struts > package > actoin 的class 属性可以指定为spring中的bean name
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ApplicationContextUtil { static ApplicationContext applicationContext; static{ applicationContext = new ClassPathXmlApplicationContext("app*.xml"); } public static synchronized Object getBean(String target){ return applicationContext.getBean(target); } }
= =然后Action里面就出现了很多奇怪的 ApplicationContextUtil.getBean 来获取对象
现在需要使用到Spring中task 来实现定时任务才发现 Spring可以被整合到Struts中 Struts的action对象也可以交给Spring实现 来达到IOC 控制反转的效果
背景扯完了 下面来点干货吧
首先是配置web.xml 这部的目的是在web项目启动的时候扫描配置文件,配置的内容就是给web项目加一个监听器
<context-param> <param-name>contextConfigLocation</param-name><!-- 固定的 --> <param-value>classpath:applicationContext.xml</param-value><!--指定spring配置文件的位置--> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>第二步指定struct 的objectFactory ,实现的目的是 告诉struts 是用Spring配置文件来生成对象
这一步需要加包 struts2-spring-plugin.jar 分析配置项也能知道需要这个包
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
这样我们的配置工作就完成了
最后放上demo
spring 中配置bean
<!-- 配置 action--> <bean id="loginAction" class="com.yueguang.actions.LoginAction"> <property name="baseDao" ref="baseDao"> </property> </bean> <bean id="testAction" class="com.yueguang.actions.TestAction"> </bean>structs 中使用
<package name="test" namespace="/test" extends="struts-default"> <action name="haha" class="testAction" method="test"> <result name="success">/Error.jsp</result> <result name="fail" type="chain"> <param name="actionName">kaka</param> <param name="namespace">/test</param> </result> </action> <action name="kaka" class="testAction" method="testkaka"> <result name="success">/Error.jsp</result> <result name="fail">/Login.jsp</result> </action> </package> <package name="struts2" namespace="/" extends="struts-default"> <global-results> <result name="login">/Login.jsp</result> <result name="input">/Error.jsp</result> </global-results> <action name="Login_*Login" class="loginAction" method="{1}Login"> <result name="success" type="redirectAction"> <param name="actionName">haha</param> <param name="namespace">/test</param> </result> <result name="fail">/Login.jsp</result> </action> <action name="Upload" class="com.yueguang.actions.Upload"> <result name="success">/index.jsp</result> <result name="fail">/Error.jsp</result> </action> </package>
struts > package > actoin 的class 属性可以指定为spring中的bean name
相关文章推荐
- &quot;XX cannot be resolved to a type &quot; eclipse报错及解决说明
- eclipse里不支持泛型的解决方法
- 【转载】补充myeclipse10 优化
- 【转】解决eclipse连接不到genymotion的问题
- java基础(十)--- 异常
- JDK动态代理和CGLIB动态代理+源码下载
- Spring 实现定时任务
- java开发工具快捷键使用大全(系列一)
- Java间隔计时触发器
- Mybatis-spring整合源码解析
- IOS 基于APNS消息推送原理与实现(JAVA后台)
- java中的匿名内部类总结
- Java for LeetCode 165 Compare Version Numbers
- SpringMVC+MyBatis项目总结(三)
- java 内部类
- JAVA关于图片处理的工具类
- java之树的子结构
- Java 静态代码块与构造方法执行次序
- eclipse配置问题汇总
- Java集合框架22课后编程题