Struts1.X与Spring集成——第一种方案
spring+struts(第一种方案)
集成原理:在Action中取得BeanFactory,通过BeanFactory取得业务逻辑对象,调用业务逻辑方法。
一,新建一个项目Spring_Struts_01
二,步骤
1、spring和Struts的依赖包配置
*Struts
(1)拷贝Struts和jstl的依赖包
(2)在web.xml文件里配置ActionServlet
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xmlversion="1.0" encoding="UTF-8"?>
<web-appversion="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">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet> <!-- Standard Action Servlet Mapping-->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> </web-app></span>
(3)提供struts-config.xml文件
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xmlversion="1.0" encoding="UTF-8" ?>
<!DOCTYPEstruts-config PUBLIC
"-//Apache SoftwareFoundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> </struts-config></span>
(4)提供国际化支持,提供缺省的国际化资源文件
<span style="font-family:KaiTi_GB2312;font-size:18px;"># -- standard errors--
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
# -- validator --
errors.invalid={0}is invalid.
errors.maxlength={0}can not be greater than {1} characters.
errors.minlength={0}can not be less than {1} characters.
errors.range={0} isnot in the range {1} through {2}.
errors.required={0}is required.
errors.byte={0} mustbe an byte.
errors.date={0} isnot a date.
errors.double={0}must be an double.
errors.float={0}must be an float.
errors.integer={0}must be an integer.
errors.long={0} mustbe an long.
errors.short={0}must be an short.
errors.creditcard={0}is not a valid credit card number.
errors.email={0} isan invalid e-mail address.
# -- other --
errors.cancel=Operationcancelled.
errors.detail={0}
errors.general=Theprocess did not complete. Details should follow.
errors.token=Requestcould not be completed. Operation is not in sequence.
# -- welcome --
welcome.title=StrutsBlank Application
welcome.heading=Welcome!
welcome.message=Toget started on your own application, copy the struts-blank.war to a new WARfile using the name for your application. Place it in your container's"webapp" folder (or equivalent), and let your container auto-deploythe application. Edit the skeleton configuration files as needed, restart yourcontainer, and you are on your way! (You can find the application.propertiesfile with this message in the /WEB-INF/src/java/resources folder.)</span>
*Spring
(1)拷贝spring相关依赖包
(2)提供spring配置文件
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> </beans></span>
2,在web.xml文件里配置ContextLoaderListener(作用:让web
Server在启动时将BeanFactory放到ServletContext中)
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xmlversion="1.0" encoding="UTF-8"?>
<web-appversion="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">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet> <!-- Standard Action Servlet Mapping-->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!--加入启动Spring监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app></span>
注:使用listener配置beanfactory,将其初始化交给servlet,使其维持在ServletContext中,节省资源。Listener初始化早于Servlet
3,在Action中採用WebApplicationContextUtils.getWebApplicationContext()从ServletContext中取得BeanFactory
4,通过BeanFactory从IOC容器中取得业务逻辑对象
存在缺点:
由于Action中存在依赖查找,所以Action依赖于Spring的API
进一步了解依赖查找和依赖注入,在同一个jvm中能够依赖注入,不同jvm中不能够依赖注入
三,代码演示样例
一个简单的用户登录演示样例,没有持久层
client:
1,jsp
(1)index.jsp主页面
<span style="font-family:KaiTi_GB2312;font-size:18px;"><%@ pagelanguage="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type" content="text/html;charset=GB18030">
<title>Inserttitle here</title>
</head>
<body>
<h1>Spring+Struts(第一种集成方案)</h1>
<hr>
<ahref="login.jsp">登录</a>
</body>
</html></span>
(2)login.jsp登录页面
<span style="font-family:KaiTi_GB2312;font-size:18px;"><%@ pagelanguage="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type" content="text/html;charset=GB18030">
<title>Inserttitle here</title>
</head>
<body>
<form action="login.do"method="post" >
用户:<input type="text"name="username"><br>
密码:<input type="text"name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html></span>
(3)login_success.jsp登录成功后的跳转页面
<span style="font-family:KaiTi_GB2312;font-size:18px;"><%@ pagelanguage="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type" content="text/html;charset=GB18030">
<title>Inserttitle here</title>
</head>
<body>
韩学敏,登录成功!!
</body>
</html></span>
服务端
2(1),ActionForm获取表单数据
loginActionForm.java:
<span style="font-family:KaiTi_GB2312;font-size:18px;">packagecom.bjpowernode.usermgr.web.forms;
importorg.apache.struts.action.ActionForm;
public classloginActionForm extends ActionForm {
private String username;
private String password;
public String getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username= username;
}
publicString getPassword() {
returnpassword;
}
publicvoid setPassword(String password) {
this.password= password;
}
}</span>
2(2),Action
LoginAction.java:
<span style="font-family:KaiTi_GB2312;font-size:18px;">packagecom.bjpowernode.usermgr.web.actions; importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse; importorg.apache.struts.action.Action;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
importorg.springframework.beans.factory.BeanFactory;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importorg.springframework.web.context.support.WebApplicationContextUtils; importcom.bjpowernode.usermgr.manager.UserManager;
importcom.bjpowernode.usermgr.manager.UserMangerImpl;
importcom.bjpowernode.usermgr.web.forms.loginActionForm; public classLoginAction extends Action { @Override
publicActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequestrequest, HttpServletResponse response)
throwsException {
//从LoginActionForm拿数据
loginActionFormlaf=(loginActionForm)form;
String username=laf.getUsername();//获取用户名
String password =laf.getPassword();//获取密码 /*UserManageruserManager=new UserMangerImpl();
userManager.Login(username, password);*/
//取BeanFactory拿到配置文件applicationContext.xml生成BeanFactory BeanFactory factory= new ClassPathXmlApplicationContext("applicationContext.xml"); /*//使用BeanFactory从IOC容器中取得相关对象(依据id取得)
UserManageruserManager=(UserManager)factory.getBean("userManager");
userManager.Login(username, password);//调用接口UserManager的登录方法Login()
*/
//例如以下方式不用每次都创建BeanFactory,并且不用在这里写死配置文件,配置文件写在了web.xml中;可是在此层中知道了Spring的存在
BeanFactory beanFactory =WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
UserManageruserManager=(UserManager)factory.getBean("userManager");
userManager.Login(username, password);//调用接口UserManager的登录方法Login()
returnmapping.findForward("success");//跳转到成功页面
} }</span>
2(3)业务层 接口+实现
接口UserManager.java:
<span style="font-family:KaiTi_GB2312;font-size:18px;">packagecom.bjpowernode.usermgr.manager;
public interfaceUserManager {
/**
* 用户登录
* @param username:用户名
* @param password:密码
*/
publicvoid Login(String username,String password);
}</span>
实现UserManagerImp.java:
<span style="font-family:KaiTi_GB2312;font-size:18px;">packagecom.bjpowernode.usermgr.manager;
public classUserMangerImpl implements UserManager {
@Override
publicvoid Login(String username, String password) {
System.out.print(this.getClass()+",username="+username);
}
}</span>
3,Struts配置文件 配置数据
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xmlversion="1.0" encoding="UTF-8" ?> <!DOCTYPEstruts-config PUBLIC
"-//Apache SoftwareFoundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config>
<!--生命ActionForm(即,告诉Struts哪个是ActionForm) -->
<form-beans>
<form-bean name="loginForm"type="com.bjpowernode.usermgr.web.forms.loginActionForm"/>
</form-beans> <!--描写叙述Action -->
<!--描写叙述Action -->
<action-mappings>
<action path="/login"
type="com.bjpowernode.usermgr.web.actions.LoginAction"
name="loginForm"
scope="request"> <!--转向信息 -->
<forward name="success"path="/login_success.jsp"/>
</action>
</action-mappings>
<message-resourcesparameter="MessageResources" />
</struts-config></span>
4,Spring配置文件
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!--这个IOC容器核心是个工厂,抽象工厂 -->
<beanid="userManager"class="com.bjpowernode.usermgr.manager.UserMangerImpl"/> </beans></span>
5,执行
启动Tomcat
浏览器中输入地址http://localhost:8080/Spring_Struts_01/
进入主页面index.jsp:
点击“登录”,进入用户登录页面login.jsp:
输入username、password
点击“登录”button,跳转到登录成功页面login_success:
6,总结:
(1)此方案的集成原理:在Action中取得BeanFactory,通过BeanFactory取得业务逻辑对象,调用业务逻辑方法。
(2)缺点:产生了依赖,spring的类在action中产生了依赖查找。(依赖查找和依赖注入的差别(前者为主动,后者是Spring主动提供))
Struts1.X与Spring集成——第一种方案的更多相关文章
- 集成Struts2+Spring+Hibernate_两种方案
集成Struts2+Spring+Hibernate 第一种方案:让Spring创建Struts2的Action,不让Spring完全管理Struts2的Action Struts2 Act ...
- Spring+Struts集成(第二种方案)
在上一篇文章中我们了解到了第一种Spring跟Struts集成的方案,但此集成方案的不足是WEB层中知道Spring的相关内容,因为需要去主动的查找对象:BeanFactory.方案二便是通过依赖注入 ...
- Struts2+Spring集成合并
前边单独总结了Struts2,Spring和Ibaits框架了,那么怎么结合使用呢?这次先来看一下Sturts2和Spring的集成合并.其实挺简单的,就是导入各自的jar包以及连接彼此的jar包,分 ...
- Quartz学习总结(1)——Spring集成Quartz框架
一.Quartz简介 Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简 ...
- Linux下实现秒级定时任务的两种方案(crontab 每秒运行)
第一种方案,当然是写一个后台运行的脚本一直循环,然后每次循环sleep一段时间. while true ;do command sleep XX //间隔秒数 done 第二种方案,使用crontab ...
- Linux下实现秒级定时任务的两种方案
Linux下实现秒级定时任务的两种方案(Crontab 每秒运行): 第一种方案,当然是写一个后台运行的脚本一直循环,然后每次循环sleep一段时间. while true ;do command s ...
- Android 验证码倒计时两种方案
使用 第一种方案:自定义控件 1.在布局中使用 <?xml version="1.0" encoding="utf-8"?> <Relativ ...
- [java代码库]-简易计算器(第一种)
简易计算器(效果如图所示) 第一种方案:采用Javascript+html完成计算器,支持+-*/,结果显示不允许使用input输入域(可以考虑使用<span>) <html> ...
- kettle 多表全删全插同步数据 两种方案
背景: 接到上级指示,要从外网某库把数据全部导入到内网,数据每天更新一次即可,大约几百万条数据,两个库结构一样,mysql的,两台数据库所在服务器都是windows server的,写个java接口实 ...
随机推荐
- ORACLE 实验二
实验二:数据操纵 实验学时:4学时 实验类型:综合型 实验要求:必修 一.实验目的 1.掌握SQL数据查询语句: 2.掌握SQL聚集函数的使用. 3.掌握SQL插入.改动.删除语句的使用. 二.实验内 ...
- JS正则验证邮箱的格式
一.相关的代码 1 function test() 2 { 3 var temp = document.getElementById("text ...
- 打开或导入项目,从脱机 Outlook 数据文件 (.ost)
打开或导入项目,从脱机 Outlook 数据文件 (.ost) Microsoft Outlook 2010 doesn\rquote t 支持手动打开或导入项目,从一个 脱机 Outlook 数据文 ...
- 强大的PropertyGrid
PropertyGrid, 做工具一定要用这东西..... 把要编辑的对象看成类的话, 全部要编辑的属性就是成员 嗯嗯, 近期看了几眼Ogitor, 它对于PropertyGrid的使用就非常不错 全 ...
- [C++基金会]位计算 游戏开发中的应用
定义的位操作:通俗点说,,位计算是计算机操作二进制整数. 无论整数可以用二的方式来表示进度,不同类型的其长度的整数位的是不一样的.INT8要么char靠8个月2 位表示,INT16或者short是由1 ...
- HBaseConvetorUtil 实体转换工具
HBaseConvetorUtil 实体转换工具类 public class HBaseConvetorUtil { /** * @Title: convetor * @De ...
- 使用NaturalDuration获取音频的时长
#region customizeTime ) sec = " + mediaElement.Position.Seconds.ToString(); else sec = mediaEle ...
- 优化数据页面(18)——标注keyword
优化数据页面(18)--标注keyword 设计要点:优化数据页面.界面设计.美化exce 秀秀:事实上俺认为,相同是数据项,它们的重要程度也不同. 阿金:嗯? 秀秀:每一行数据时描写叙述一条信息的, ...
- Unity--关于优化方面的那些事儿(一)
近期做一个小项目,要求包的大小不能超过30M. 晚上做了个小实验,方法的确非常本,只是曾经非常多没懂的地方如今清晰了很多,我是菜鸟!希望本文章对大家有帮助,谢谢! 实验结果: 实验结果: 1.场景中仅 ...
- 故障排查:是什么 导致了服务器端口telnet失败?(转)
telnet命令的主要作用是与目标端口进行TCP连接(即完成TCP三次握手).当服务端启动后,但是telnet其监听的端口,却失败了.或者,当服务端运行了一段时间后,突然其监听的端口telnet不通了 ...