Spring与Struts整合
来源:互联网 发布:php files 编辑:程序博客网 时间:2024/05/19 10:53
最近一段时间闲下来了,就想整理整理自己的知识网,查漏补缺。博客就相当于自己整理的一个过程吧。
Spring和Struts整合
1.相关Jar的引入
注意: 除了基本的Struts和Spring jar包外,要特别加入Struts里的struts2-spring-plugin.jar和Spring里的spring-web.jar。
初始化Struts和Spring
1.配置web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app> <welcome-file-list> <wlecome-file>index.jsp</wlecome-file> </welcome-file-list> <!-- struts配置 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/bean-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>
2.创建struts.xml和bean.xml
注意: Spring配置上下文路径时,如果你的配置文件实在源目录下的话,不能直接写/WEB-INF/bean-*.xml,项目在发布的时候会直接把配置文件拷贝到/WEB-INF/classes/目录下。
整合
1.创建Action,Service,Dao
UserAction
package cn.hts.action;import com.opensymphony.xwork2.ActionSupport;import cn.hts.service.UserService;public class UserAction extends ActionSupport{ private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } public String execute() { // TODO Auto-generated method stub userService.save(); return SUCCESS; }}
UserService
package cn.hts.service;import cn.hts.dao.UserDao;public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save() { userDao.save(); }}
UserDao
package cn.hts.dao;public class UserDao { public void save() { System.out.println("DB:保存用户"); }}
2.IOC创建对象
bean-dao.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="userDao" class="cn.hts.dao.UserDao"></bean> </beans>
bean-service.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="userService" class="cn.hts.service.UserService"> <property name="userDao" ref="userDao"></property> </bean> </beans>
bean-action.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="userAction" class="cn.hts.action.UserAction"> <property name="userService" ref="userService"></property> </bean> </beans>
3.Struts对Action请求的处理
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="user" namespace="/" extends="struts-default"> <action name="user" class="userAction" method="execute"> <result name="success">/WEB-INF/index.jsp</result> </action> </package> </struts>
注意: 原先struts配置文件中class属性的值是有action的类名,现在变为由Spring来管理对象的创建。所以用的IOC容器中userAction。
结果
在浏览器中输入地址查看效果
控制台输出
注意:
因为没做跳转事件,所以浏览器的地址直接访问了action。
0 0
- 整合Struts 与Spring
- 整合Struts 与Spring
- 整合Struts 与Spring
- struts与spring整合
- spring与struts整合
- Spring与Struts整合
- Spring与Struts整合
- Spring与struts整合
- Spring与Struts整合
- spring 与struts 整合
- struts与spring整合
- Spring与Struts整合
- spring与struts整合
- Spring与Struts如何整合
- Struts与Spring的整合
- Spring 与 struts 2 整合
- struts与spring的整合
- Spring与Struts如何整合
- CentOS 6.5 下安装配置 mysql
- Android 动画用法+原理(Property Animation) 酷炫 UI动画
- linux命令
- 学习hector_slam随手记录(四)
- ARM进阶 002_1 ADS工程配置
- Spring与Struts整合
- java 技术:消息队列
- 棋盘覆盖问题
- 两种盒模型
- 实现带括号加减乘除运算
- nginx简介
- 区域内点的个数
- LDA-线性判别分析
- C++排序之数组,Vector,priority_queue