Struts+Spring搭建
前言
本文以Tomcat为j2ee容器,数据库为Sqlserver2005进行说明。Struts版本为2.3.15.3,Spring版本为3.2.5
Spring简介
Spring也是appache下面的一个开源项目,强大的基于 JavaBeans 的采用控制反转(Inversion of Control,IoC)原则的配置管理,使得应用程序的组件更加快捷简易。当然它的用途不仅这些,还包括:面向切面编程、JDBC支持、事务管理等。
获得Spring
Spring官网 http://www.springsource.org/ ,由于官网改版,找起来可能会比较麻烦,大家可以从这个网站进行下载所需的包:http://repo.springsource.org/release/org/springframework/spring/
新建StrutsSpringDemo
引入lib包
解压从官网下载的spring-framework-3.2.5.RELEASE-dist.zip包,在lib中挑选出所需的jar包,并引入struts的jar包(关于Struts的搭建请参考“StrutsDemo搭建”文档)。所需包如下图所示:
web.xml配置
在web.xml中新增如下内容:
<!-- spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml/WEB-INF/spring-service.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
新建applicationContext.xml
在项目中WEB-INF下新建applicationContext.xml文件,注意此处JDBC配置使用的JNDI配置,大家可以根据具体情况进行更改,里面内容如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"default-autowire="byName">
<beanid="dataSource"class="org.springframework.jndi.JndiObjectFactoryBean">
<propertyname="jndiName">
<value>java:comp/env/jdbc/ehrdb</value>
</property>
</bean>
<beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate">
<propertyname="dataSource">
<refbean="dataSource"/>
</property>
</bean>
<beanid="springDao"class="org.apache.struts.helloworld.dao.SpringDao">
<propertyname="jdbcTemplate">
<refbean="jdbcTemplate"/>
</property>
</bean>
</beans>
新建spring-service.xml
在项目中WEB-INF下新建spring-service.xml文件,里面内容如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"default-autowire="byName">
<!--配置service -->
<beanid="springService"class="org.apache.struts.helloworld.service.SpringService"/>
</beans>
新建struts.xml
在SRC目录下新建struts.xml文件,注意:要想实现Spring托管Struts必须在此配置文件中加入<constant name="struts.objectFactory" value="spring"/>这句代码,具体内容如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPE strutsPUBLIC
"-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constantname="struts.objectFactory"value="spring"/>
<constantname="struts.devMode"value="false"/>
<packagename="basicstruts2"extends="struts-default">
<actionname="index">
<result>/index.jsp</result>
</action>
<actionname="hello"
class="org.apache.struts.helloworld.action.HelloWorldAction"method="execute">
<resultname="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
新建SpringDao
package org.apache.struts.helloworld.dao;
import org.springframework.jdbc.core.JdbcTemplate;
publicclass SpringDao {
private JdbcTemplate jdbcTemplate;
publicvoid query(){
String value = jdbcTemplate.queryForObject("select password from tb_manager where id=1", String.class);
System.out.println(value);
}
publicvoid setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
}
新建SpringService
package org.apache.struts.helloworld.service;
import org.apache.struts.helloworld.dao.SpringDao;
publicclass SpringService {
private SpringDao springDao;
publicvoid doSomething(){
System.out.println("SpringService doSomething...");
springDao.query();
}
publicvoid setSpringDao(SpringDao springDao){
this.springDao = springDao;
}
}
使用Spring
在action中定义private SpringService springService;并增加set方法,这样就可以在action中直接使用service了。
部署运行
将程序部署至tomcat,访问http://localhost:8080/StrutsSpringDemo运行
Demo下载地址
以上代码均为部分核心配置,完整demo下载地址如下:http://download.csdn.net/detail/zfz1214/6679927
Struts+Spring搭建的更多相关文章
- shh(struts+spring+Hibernate)的搭建
一.Struts 一.struts的流程 (1)首先,用户在地址栏中输入你的项目访问路径,然后这个请求会发送到服务器,服务器先找到要web.xml的,给web.xml中配置了一个filter过滤器,过 ...
- 用eclipse搭建SSH(struts+spring+hibernate)框架
声明: 本文是个人对ssh框架的学习.理解而编辑出来的,可能有不足之处,请大家谅解,但希望能帮助到大家,一起探讨,一起学习! Struts + Spring + Hibernate三者各自的特点都是什 ...
- Struts+Spring+Hibernate整合入门详解
Java 5.0 Struts 2.0.9 Spring 2.0.6 Hibernate 3.2.4 作者: Liu Liu 转载请注明出处 基本概念和典型实用例子. 一.基本概念 St ...
- Struts+Spring+Hibernate项目的启动线程
在Java Web项目中,经常要在项目开始运行时启动一个线程,每隔一定的时间就运行一定的代码,比如扫描数据库的变化等等.要实现这个功能,可以现在web.xml文件中定义一个Listener,然后在这个 ...
- Struts,spring,hibernate三大框架的面试
Struts,spring,hibernate三大框架的面试 1.Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3 ...
- 使用struts+spring+hibernate组装web应用
这篇文章将讨论怎样组合几个着名的框架去做到松耦合的目的,怎样建立你的构架,怎样让你的各个应用层保持一致.富于挑战的是:组合这些框架使得每一层都以一种松耦合的方式彼此沟通,而与底层的技术无关.这篇文章将 ...
- Struts+Spring+Hibernate进阶开端(一)
入行就听说SSH,起初还以为是一个东西,具体内容就更加不详细了,总觉得高端大气上档次,经过学习之后才发现,不仅仅是高大上,更是低调奢华有内涵,经过一段时间的研究和学习SSH框架的基本原理与思想,总算接 ...
- Oracle11g R2学习系列 之四Maven+Struts+Spring实验
今天试一下Java调用Oracle来看一下.会不会也如昨天实验的一样坑呢?由于我对于Java也接触的不多,所以不打算直接使用该收提供的实验文档,而是自己利用Maven+Struts+Spring来自己 ...
- Struts+Tomcat搭建
Struts+Tomcat搭建 tomcat使用(服务器端开发): 如果要安装Tomcat需要进行的配置:tomcat安装在c: \Tomcat CATALINA_HOME变量值设为: H:\Prog ...
随机推荐
- 【转】AFNetworking之于https认证
转自:http://www.cocoachina.com/ios/20161220/18393.html 写在开头: 本来这篇内容准备写在AFNetworking到底做了什么?(三)中的,但是因为我想 ...
- OD: DEP & Ret2Libc
Data Execution Prevention,数据执行保护,专门用来弥补计算机对数据和代码混淆这一天然缺陷. DEP 的原理是将数据所在的内存页(默认的堆.各种堆栈页.内存池页)标记为不可执行, ...
- javascript获取CSS3浏览器前缀
var prefix = (function () { var styles = window.getComputedStyle(document.documentElement, ''), pre ...
- HTML5触摸屏touch事件使用介绍1
市面上手机种类繁多,在触屏手机上运行的网页跟传统PC网页相比还是有很大差别的.由于设备的不同浏览器的事件的设计也不同.传统PC站的 click 和 onmouseover 等事件在一般触屏的手机上也可 ...
- 怎样取得数组对象和arralist 的长度
数组用length属性 ArrayList用size()方法
- Swift中的延迟加载(懒加载)
Swift方式的延迟加载 而在Swift中,你只需一行代码即可实现此机制: lazy var players = String[]() 简单.简洁,直入主题. 但你得记住,你必须使用var关键字来定义 ...
- PHP 初学者的学习线路和建议【1】
先来看下PHP初学者的学习线路: (1) 熟悉HTML/CSS/JS等网页基本元素,完成阶段可自行制作简单的网页,对元素属性相对熟悉. (2) 理解动态语言的概念和运做机制,熟悉基本的PHP语法. ( ...
- 单电机板机模型,f22
视频连接 http://v.youku.com/v_show/id_XMTI5MDEzMzIxMg==.html?from=y1.7-1.2 http://v.youku.com/v_show/id_ ...
- JAVA classpath, 纠正我一直以来错误的认知
如何调在CLI中使用java tool(JDK中的java命令)调用一个打包在jar中的类,我想大多数人都能给出笼统的方案: java -classpath xxxxx com.test.classA ...
- 了解JVM
---恢复内容开始--- Java对象在运行环境中,对于内存而言,存在三种状态:年轻代.年老代.永生代: 下图是JVM内存模型 1. 年轻代被分为3个部分:Enden区和两个Survivor区,垃圾回 ...