`
周一Monday
  • 浏览: 342949 次
  • 来自: 北京
社区版块
存档分类
最新评论

Spring之Spring2.5集成Struts2.2

阅读更多

1.搭建开发环境

 

Spring2.5需要的JAR文件:

spring-framework-2.5.6\dist\spring.jar

spring-framework-2.5.6\lib\jakarta-commons\commons-logging.jar

 

Struts2.2需要的JAR文件

struts-2.2.1.1\lib\commons-fileupload-1.2.1.jar

struts-2.2.1.1\lib\commons-io-1.3.2.jar

struts-2.2.1.1\lib\freemarker-2.3.16.jar

struts-2.2.1.1\lib\javassist-3.7.ga.jar

struts-2.2.1.1\lib\ognl-3.0.jar

struts-2.2.1.1\lib\struts2-core-2.2.1.1.jar

struts-2.2.1.1\lib\xwork-core-2.2.1.1.jar

 

struts-2.2.1.1\lib\struts2-spring-plugin-2.2.1.1.jar Spring整合Struts2需要的

 

在web.xml中做如下配:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<!-- 配置Spring监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>

	<!-- 配置Struts2 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

 

JSP页面如下:

 

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Source City</title>
</head>
<body>
	<s:form action="distanceAction" method="post">
		<table>
			<tr>
				<td>Source City</td>
				<td><s:textfield name="srcCity" /></td>
			</tr>
			<tr>
				<td>Destination City</td>
				<td><s:textfield name="destCity" /></td>
			</tr>
			<tr>
				<td>Distance</td>
				<td><font color="red">${distance }</font></td>
			</tr>
			<tr>
				<td colspan="2"><s:submit value="Find" /></td>
			</tr>
		</table>
	</s:form>
</body>
</html>

 

 

 

 

 

2.开发Service

 

a.Service接口

package com.apress.springrecipes.city;

public interface CityService {

	/**
	 * 计算两个城市的距离
	 * @param srcCity 出发地
	 * @param destCity 目的地
	 * @return 两个城市之间的距离
	 */
	public double findDistance(String srcCity,String destCity);
}

 

b.开发Service的实现类

package com.apress.springrecipes.city;

import java.util.Map;

public class CityServiceImpl implements CityService {

	private Map<String, Map<String, Double>> distanceMap;

	public void setDistanceMap(Map<String, Map<String, Double>> distanceMap) {
		this.distanceMap = distanceMap;
	}

	/**
	 * 计算两个城市的距离
	 * @param srcCity 出发地
	 * @param destCity 目的地
	 * @return 两个城市之间的距离
	 */
	@Override
	public double findDistance(String srcCity, String destCity) {
		Map<String, Double> destinationMap = distanceMap.get(srcCity);
		if (destinationMap == null) {
			throw new IllegalArgumentException("Source city not found");
		}
		Double distance = destinationMap.get(destCity);
		if (distance == null) {
			throw new IllegalArgumentException("Destination city not found");
		}
		return distance;
	}

}

 

在Spring的配置文件beans.xml中配置service

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">

	<bean id="cityService" class="com.apress.springrecipes.city.CityServiceImpl">
		<property name="distanceMap">
			<map>
				<entry key="New York">
					<map>
						<entry key="London" value="5574" />
						<entry key="BeiJing" value="10974" />
					</map>
				</entry>
			</map>
		</property>
	</bean>
</beans>

 

3.开发Action整合Spring

 

有两种方法:

 

第一种:

package com.apress.springrecipes.city.struts2;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.apress.springrecipes.city.CityService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DistanceAction extends ActionSupport {

	private static final long serialVersionUID = 6770573934287194873L;

	private String srcCity;
	private String destCity;

	public String getSrcCity() {
		return srcCity;
	}

	public void setSrcCity(String srcCity) {
		this.srcCity = srcCity;
	}

	public String getDestCity() {
		return destCity;
	}

	public void setDestCity(String destCity) {
		this.destCity = destCity;
	}

	@Override
	public String execute() throws Exception {
		//获取ServletContext对象
		ServletContext servletContext = ServletActionContext.getServletContext();
		/**
		 * 在Web容器中获取Spring容器
		 * 此方法需要一个ServletContext对象
		 */
		WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		//取得Service对象
		CityService cityService = (CityService) ctx.getBean("cityService");
		//调用Service
		double distance = cityService.findDistance(srcCity, destCity);
		//存值
		ActionContext.getContext().put("distance", distance);
		return SUCCESS;
	}

}

 

struts.xml

<struts>
	<constant name="struts.devMode" value="true" />
	<constant name="struts.ui.theme" value="simple" />
	<package name="default" namespace="" extends="struts-default">
		<action name="distanceAction" class="com.apress.springrecipes.city.struts2.DistanceAction">
			<result name="success">/distance.jsp</result>
		</action>
	</package>
</struts>

 

这种方法Struts2并没有托管给Spring管理,只是在Action中通过Spring的容器获取Service而已。两者还是相对独立的。觉得还不算Struts与Spring集成吧,或者集成的不彻底吧。(哈,一家之言,欢迎拍转)。

 

第二种方法:

package com.apress.springrecipes.city.struts2;

import com.apress.springrecipes.city.CityService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DistanceAction2 extends ActionSupport {

	private static final long serialVersionUID = 6770573934287194873L;

	//注入Service
	private CityService cityService;

	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}

	private String srcCity;
	private String destCity;

	public String getSrcCity() {
		return srcCity;
	}

	public void setSrcCity(String srcCity) {
		this.srcCity = srcCity;
	}

	public String getDestCity() {
		return destCity;
	}

	public void setDestCity(String destCity) {
		this.destCity = destCity;
	}

	@Override
	public String execute() throws Exception {
		double distance = cityService.findDistance(srcCity, destCity);
		// 存值
		ActionContext.getContext().put("distance", distance);
		return SUCCESS;
	}

}

 

 struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
	<constant name="struts.devMode" value="true" />
	<constant name="struts.ui.theme" value="simple" />
	<package name="default" namespace="" extends="struts-default">
		<!-- 
			这里的class="distanceAction2"只是一个“伪类”
			真正的实现类在Spring的beans.xml中定义
		 -->
		<action name="distanceAction2" class="distanceAction2">
			<result name="success">/distance2.jsp</result>
		</action>
	</package>
</struts>

 

然后,beans.xml中要加入如下配置:

<!-- 第二种方法 -->
	<!-- 
		配置scope="prototype" (推荐) 或者 scope="request" 
		以保证每次请求都创建一个新的Action实例
	 -->
	<bean id="distanceAction2" class="com.apress.springrecipes.city.struts2.DistanceAction2" scope="prototype">
		<property name="cityService" ref="cityService" />
	</bean>

 这次Action的实例由Spring创建管理,而需要的Service组件,则需要提供Service的Setter方法来注入进去来得到Service,即:所有的组件都在Spring中注册了,通通交由Spring管理了。

分享到:
评论

相关推荐

    SHH整合(mysql,struts2 2.2+spring2.5+hibernate3.0,log,jquery,标签等)DEMO

    1、SHH整合 详细清晰的标准配置,主流的应用配置,struts2.2+spring2.5+hibernate3.0 2、结合MYSQL轻量级数据库,有写好的库表sql 3、整合日志管理配置,及Spring代理日志管理的配置及应用 4、应用WEB前段主流技术,...

    Struts2 入门培训

    4 1.3.2. 受控目录 5 2. 入门例子 5 2.1. 项目 5 2.2. 在WEB.XML中,配置FILTERDISPATCHER过滤器 6 2.3. 配置STRUTS.PROPERTIES 6 2.4. 编写一个简单的ACTION类 7 2.5. STRUTS.XML配置文件 8...

    Spring-Reference_zh_CN(Spring中文参考手册)

    15.4. Struts 15.4.1. ContextLoaderPlugin 15.4.1.1. DelegatingRequestProcessor 15.4.1.2. DelegatingActionProxy 15.4.2. ActionSupport 类 15.5. Tapestry 15.5.1. 注入 Spring 托管的 beans 15.5.1.1. 将 ...

    Spring in Action(第二版 中文高清版).part2

    第1章 开始Spring之旅 1.1 Spring是什么 1.2 开始Spring之旅 1.3 理解依赖注入 1.3.1 依赖注入 1.3.2 DI应用 1.3.3 企业级应用中的依赖注入 1.4 应用AOP 1.4.1 AOP介绍 1.4.2 AOP使用 1.5 小结 第2章 ...

    Spring in Action(第二版 中文高清版).part1

    第1章 开始Spring之旅 1.1 Spring是什么 1.2 开始Spring之旅 1.3 理解依赖注入 1.3.1 依赖注入 1.3.2 DI应用 1.3.3 企业级应用中的依赖注入 1.4 应用AOP 1.4.1 AOP介绍 1.4.2 AOP使用 1.5 小结 第2章 ...

    Spring in Action(第2版)中文版

    第1章开始spring之旅 1.1spring是什么 1.2开始spring之旅 1.3理解依赖注入 1.3.1依赖注入 1.3.2di应用 1.3.3企业级应用中的依赖注入 1.4应用aop 1.4.1aop介绍 1.4.2aop使用 1.5小结 第2章基本bean装配 ...

    Spring 2.0 开发参考手册

    2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.4. 中间层 2.4.1. ...

    Spring攻略(第二版 中文高清版).part1

    6.3 将Spring与Struts 1.x集成 220 6.3.1 问题 220 6.3.2 解决方案 220 6.3.3 工作原理 220 6.4 将Spring与JSF集成 226 6.4.1 问题 226 6.4.2 解决方案 226 6.4.3 工作原理 227 6.5 将Spring与DWR...

    spring in action英文版

     第1章 开始Spring之旅  1.1 为什么使用Spring  1.1.1 J2EE开发者的一天  1.1.2 Spring的承诺  1.2 Spring是什么  1.3 开始Spring之旅  1.4 理解反向控制  1.4.1 依赖注入  1.4.2 IoC应用...

    Spring中文帮助文档

    2.7. 移植到Spring 2.5 2.7.1. 改变 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. IoC(控制反转)容器 3.1. 简介 3.2. 基本原理 - 容器和bean 3.2.1. 容器 3.2.2. 实例化容器 3.2.3. 多种bean ...

    低清版 大型门户网站是这样炼成的.pdf

    6.4.2 spring 2.5集成mvc框架struts 2 411 6.4.3 ssh 2组合框架的基本开发步骤—eportal启程 414 6.5 小结 423 第2篇 实践篇 第7章 ssh 2热身——构建新闻发布系统 427 7.1 门户网站新闻资讯基本分类 427 7.2 ...

    Spring攻略(第二版 中文高清版).part2

    6.3 将Spring与Struts 1.x集成 220 6.3.1 问题 220 6.3.2 解决方案 220 6.3.3 工作原理 220 6.4 将Spring与JSF集成 226 6.4.1 问题 226 6.4.2 解决方案 226 6.4.3 工作原理 227 6.5 将Spring与DWR...

    Spring API

    2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件...

    spring chm文档

    2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.4. 中间层 2.4.1. ...

    spring security 参考手册中文版

    15.1 Servlet 2.5+集成 127 15.1.1 HttpServletRequest.getRemoteUser() 127 15.1.2 HttpServletRequest.getUserPrincipal() 127 15.1.3 HttpServletRequest.isUserInRole(String) 128 15.2 Servlet 3+集成 ...

    进销存信息管理系统毕业设计论文

    2.2 Struts框架 6 2.2.1 Struts1.x框架的概述 6 2.2.2 Struts1.x框架的体系结构 7 2.3 Hibernate框架 8 2.3.1 ORM技术的概述 8 2.3.2 Hibernate框架的概述 9 2.3.3 Hibernate的体系结构 9 2.4 Spring框架 11 2.4.1 ...

    JAVA WEB典型模块与项目实战大全

    3.4 实现spring、struts2.x和hibernate框架集成  3.5 小结  第2篇 典型模块开发  第4章 在线文本编辑器(fckeditor)  4.1 分析fckeditor在线文本编辑器  4.2 fckeditor在线文本编辑器初级应用  4.3 ...

    JAVA程序开发大全---上半部分

    以及基于这些技术的商业化应用程序的开发技巧,在讲解过程中以目前最为流行的开发工具MyEclipse为载体,全面系统地介绍了如何在MyEclipse中开发基于Struts、Hibernate、Spring等主流框架的各种Java应用程序。...

    基于Java的邮件系统的设计与实现-初稿.doc

    2.2 Struts2简介 4 2.3 Spring简介 6 2.4 Hibernate简介 7 2.5 MyEclipse开发工具简介 8 2.6 MySQL简介 8 2.7 Tomcat服务器简介 9 2.8 James邮件服务器简介 9 2.9 Jquery简介 10 2.10 电子邮件简介 11 3 系统需求...

Global site tag (gtag.js) - Google Analytics