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 ...
随机推荐
- hdu 2190
//hdu2190 水题 题意是给一个n*3的教室,用1*1,2*2的砖去铺满,有多少种铺法,一开始没发现这个规律,想了一下,应该是递归. #include <iostream> usi ...
- 《Java编程思想》之重点笔记——多态性理解
Java中除了static方法和final方法(private方法本质上属于final方法,因为不能被子类访问)之外,其它所有的方法都是动态绑定,这意味着通常情况下,我们不必判定是否应该进行动态绑定— ...
- php的一些基本概念梳理
楼主是个刚参加工作的菜鸟,这几天刚开通博客园微博,想通过这个平台与大家共同学习与分享一些技术知识. 但是楼主犹豫的好久,不知道第一篇该写点什么.最后我决定先从php的一些基本概念开始,以便加深对各个概 ...
- dropdownlist 二级联动
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GradeBind(); } } //绑定 ...
- Lesson 7: Responsive Typography In Action
Lesson 7: Responsive Typography In Action 排版一直都是设计和传达的基础.虽然现在的设计和印刷品设计差别很大,但核心原则还是不变的. Article 1: Bo ...
- Linux下安装Oracle 10g(redhat 4)
--注:本篇文章只装Oracle,并没有建库 一:在虚拟机里装个readhat 4系统 二:配IP 配好之后的IP如下: 三:建用户组,用户 注意:oracle用户应具有相同的uid. groupad ...
- void *memmove( void* dest, const void* src, size_t count );数据拷贝,不需要CPU帮助
分享到 腾讯微博 QQ空间 新浪微博 人人网 朋友网 memmove 编辑词条 编辑词条 --> memmove用于从src拷贝count个字符到dest,如果目标区域和源区域有重叠的话,m ...
- Android自定义扁平化对话框
平时我们开发的大多数的Android.iOS的APP,它们的风格都是拟物化设计.如今Android 4.X.iOS 7.WP8采用的是扁平化设计,可以看出扁平化设计是未来UI设计的趋势.其实扁平化设计 ...
- [每日一题] OCP1z0-047 :2013-07-24 子查询――外查询与内查询的执行顺序
一.Oracle的子查询分为两类分别是嵌套子查询和非嵌套子查询.所谓嵌套子查询是指,子查询是一个独立的查询不与外部查询相关,子查询将被先执行,而且只被执行一次,子查询执行完成后,再执行外部的查询,外部 ...
- idea intellij 快捷键(ubuntu版本)
S + C + T 创建测试类 A + F12 开启终端 C + F12 查看类中的方法属性 ----随时更新,记录快捷方式