`
yarafa
  • 浏览: 86940 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Struts2 - Spring 整合简单样例

阅读更多

初学 Struts2,在整合 Spring 时遇到一些麻烦,下面以一个简单样例说明操作过程,以备不时只需。如有不当之处,望不吝赐教。

开发环境:

MyEclipse 6.5

Struts 2.1.8.1

Spring 2.5

Tomcat 6.0

 

1. 新建 web 项目

新建 Java EE 5.0 的 web project

 

2. 项目配置

2.1 配置文件目录

在 src 下新建 config 文件夹,并在该文件夹下新建两个文件夹,分别是:spring 和 struts2,这两个文件夹分别用于放置 spring 和 struts2 的配置文件,以便统一管理。

 

2.2 JAR 包目录

在项目根目录下新建文件夹 lib,并在该文件夹下新建两个文件夹,分别是:spring 和 struts2,这两个文件夹分别用于放置spring 和 struts2 的 JAR 文件,以便统一管理。

 

说明:上边的配置纯属个人偏好或者习惯。

 

3. 添加 Struts2 的支持

将 struts2 所需的基础包以及 struts-spring-plugin 包复制到 lib/struts2 文件夹下,并将这些 JAR 包添加到项目的 classpath。如下图所示。

 

4. 添加 Spring 支持

右键项目 --> MyEclipse --> Add Spring Capabilities...,选中 Spring 2.5 Core Libraries,并将 JAR 文件复制到 lib/spring 目录下,同时选中创建 applicationContext.xml 文件,并将其存放至 src/config/spring 目录下。如下两图所示。

注意:在添加了 Spring 的 JAR 包后,需要检查所有的 JAR 包是否有冲突。比如在样例中,添加 struts2 JAR 包时有 commons-logging-1.0.4,而添加 spring JAR 包时有 commons-logging.jar,这两个 JAR 包由可能会有冲突,需要将其中一个从 classpath 中删除。

 

 

 

5. Java 类及接口编辑

5.1 Service 接口定义

/***********************************************************************
 * <p>Project Name: struts2spring</p>
 * <p>File Name: com.thu.afa.struts2spring.service.LoginService.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.struts2spring.service;

/**
 * <p>Class Name: LoginService</p>
 * <p>Description: </p>
 * @author Afa
 * @date Aug 2, 2010
 * @version 1.0
 */
public interface LoginService
{
	public boolean doLogin(String username, String password);
}

 

5.2 Service 接口的实现

public class LoginServiceImpl implements LoginService
{
	/* (non-Javadoc)
	 * <p>Title: </p>
	 * <p>Method Name: doLogin</p>
	 * <p>Description: </p>
	 * @author: Afa
	 * @date: Aug 2, 2010
	 * @see com.thu.afa.struts2spring.service.LoginService#doLogin(java.lang.String, java.lang.String)
	 *
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean doLogin(String username, String password)
	{
		if("llu".equals(username) && "llu".equals(password)) return true;
		else return false;
	}
}

 

5.3 Action 的定义

/***********************************************************************
 * <p>Project Name: struts2spring</p>
 * <p>File Name: com.thu.afa.struts2spring.action.LoginAction.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.struts2spring.action;

import com.opensymphony.xwork2.ActionSupport;
import com.thu.afa.struts2spring.service.LoginService;

/**
 * <p>Class Name: LoginAction</p>
 * <p>Description: </p>
 * @author Afa
 * @date Aug 2, 2010
 * @version 1.0
 */
public class LoginAction extends ActionSupport
{
	private static final long serialVersionUID = -231294389344546625L;

	private String username;
	private String password;
	private LoginService loginService;
	
	public String getUsername()
	{
		return username;
	}
	public void setUsername(String username)
	{
		this.username = username;
	}
	public String getPassword()
	{
		return password;
	}
	public void setPassword(String password)
	{
		this.password = password;
	}
	public LoginService getLoginService()
	{
		return loginService;
	}
	public void setLoginService(LoginService loginService)
	{
		this.loginService = loginService;
	}
	
	@Override
	public String execute() throws Exception
	{
		if(loginService.doLogin(username, password)) return SUCCESS;
		else return INPUT;
	}
}

说明:在 LoginAction 中引用了 LoginService 接口的实例,该实例是通过 spring 注入的。

 

6. 配置文件的编辑

6.1 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.objectFactory" value="spring" />
	<package name="com.thu.afa.struts" extends="struts-default">
		<action name="login" class="loginAction">
			<result name="success">/result.jsp</result>
			<result name="input">/index.jsp</result>
		</action>
	</package>
</struts>

 

6.2 applicationContext.xml

<?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-2.5.xsd">

	<bean id="loginService"
		class="com.thu.afa.struts2spring.service.impl.LoginServiceImpl" />
	<bean id="loginAction"
		class="com.thu.afa.struts2spring.action.LoginAction">
		<property name="loginService">
			<ref bean="loginService" />
		</property>
	</bean>
</beans>

 

6.3 配置说明

首先,在 Struts 的配置文件中需要添加如下配置,表明 Struts 对象交由 Spring 进行管理

<constant name="struts.objectFactory" value="spring" />

其次,在 action 配置节点的 class 属性,这里不再是真正的类的映射,由于是交由 Spring 进行管理的缘故,所以需要与 applicationContext.xml 中对于 action 的配置保持一致,而真正的 action 类定义是在 applicationContext.xml 中进行说明。

 

6.4 web.xml

在该文件中需要添加如下配置信息。

<listener>
	<listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
</listener>

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/classes/config/spring/applicationContext.xml</param-value>
</context-param>

<filter>
	<filter-name>struts2</filter-name>
	<filter-class>
		org.apache.struts2.dispatcher.FilterDispatcher
	</filter-class>
</filter>
<filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

说明:按下 Ctrl 键,将鼠标移至 org.springframework.web.context.ContextLoaderListener,如果找不到对应的类,那么还需要手动添加 spring-web.jar 文件到 lib/spring 目录,并将该 JAR 包添加到项目的 classpath。

 

7. JSP 页面

7.1 index.jsp

<body>
  <s:form action="login.action" method="post">
  	<s:textfield name="username" label="UserName"></s:textfield>
  	<s:password name="password" label="Password"></s:password>
  	<s:submit value="Submit"></s:submit>
  </s:form>
</body>

 

7.2 result.jsp

<body>
  Welcome,
</body>

 

8 项目部署

到此,已完成了 Struts2 和 Spring 的整合,可以部署运行了。如下图所示是项目的整体文件结构。

 

 

-----------------------------------------------------
Stay Hungry, Stay Foolish!
Afa
Aug 2nd, 2010
-----------------------------------------------------

分享到:
评论

相关推荐

    hibernate3.3+spring2+struts2的整合源代码

    整合SSH2的经典样例,来自传智播客,有注解,适合初学者

    开发者突击:Java Web 主流框架整合开发 blank目录

    (3)struts2_blank:构建Struts2环境的jar、tld、xml文件; (4)log4j_blank:构建Log4j环境的jar、properties文件; (5)sitemesh_blank:构建Sitemesh环境的jar、tld、xml及装饰文件样例; (6)dbcp_blank:...

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

    2. Spring 2.0 的新特性 2.1. 简介 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 切面的...

    ssh+cxf整合及单个cxf测试样例

    spring+struts+hibernate+cxf整合及单个cxf测试样例,详细介绍ssh+cxf配置过程,及执行顺序。

    WEB项目集成Flex3功能

    跃跃欲试在项目中加入Flex的功能需求者与日俱增,所谓万事开头难,共同期待好的向导,故在《FRAME-INTEGERATION》专栏,逐步整理并给出日常开发框架整合此类向导,而此类整合主要关注于Struts2、Spring2、iBatis2及...

    spring chm文档

    2. Spring 2.0 的新特性 2.1. 简介 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. 对@...

    SSH架构样例

    本次搭建的过程是先搭建Struts2.3.12,测试通过之后,整合Spring3.2.2,最后再整合Hibernate4.2.0,最后的整合内容有点多,因为这也是居于MVC思想搭建的环境,希望大家耐心学习。 说明 本文档是个人总结的经验,仅供...

    Spring 2.0 开发参考手册

    2. Spring 2.0 的新特性 2.1. 简介 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. 对@...

    Spring中文帮助文档

    2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. IoC(控制反转)容器 3.1. 简介 3.2. 基本原理 - 容器和bean 3.2.1. 容器 3.2.2. 实例化容器 3.2.3. 多种bean 3.2.4. 使用容器 3.3. 依赖 3.3.1. 注入...

    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中自动搜索组件...

Global site tag (gtag.js) - Google Analytics