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 ...
随机推荐
- python的按位运算
#coding=utf-8#"&"按位与运算,是指一个数字转化为二进制,然后这些二进制的数按位来进行与运算a=7&18print a'''首先将7转化为二进制,得到 ...
- 设计模式之—简单工厂模式<Simple Factory Pattern >
简单工厂模式结构图: 简单工厂模式以简单的加减乘除运算为例: 运算符类(Operation): namespace CalcTest.Simple_Factory_patterns { class O ...
- div 固定宽高 水平垂直居中方法
div固定宽高,水平垂直居中,根据所用单位不同,分成两种情况,分别是"px"和"%"情况. 例:将三层div做出三个边框,要求水平垂直居中.效果如图 情况一(单 ...
- Hbase常用操作
下面我们看看HBase Shell的一些基本操作命令,我列出了几个常用的HBase Shell命令,如 名称 命令表达式 创建表 create '表名称', '列名称1','列名称2','列名称N' ...
- 迭代器(iterator) 与 traits 编程技法
看了候哥的<STL源码剖析>的迭代器那一章,在这里将思路稍微疏理一下 迭代器 迭代器模式的定义:提供一种方法,在不需要暴露某个容器的内部表现形式情况下,使之能依次访问该容器中的各个元素. ...
- Ubuntu 安装之后的配置博文总结
由于频繁地在各种机器上给别人安装ubuntu,每次安装之后都需要进行一些配置,现在以ubuntu12.04为例,就他人的一些配置博文总结如下: 1. Ubuntu安装中文输入法 http://www. ...
- linux thread 互斥锁
#include <stdio.h> #include <stdlib.h> #include <pthread.h> void *threadhandle(voi ...
- JS中typeof与instanceof的区别 (2010-05-26 10:47:40
JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: typeof typeof 是一个一元运算,放在一个运算数之前 ...
- 文成小盆友python-num15 - JavaScript基础
一.JavaScript简介 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的 ...
- JavaScript常用
打印日志 console.log 类型判断 第一种方式var type = Object.prototype.toString.call(list);console.log(type);第二种方式ty ...