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
-----------------------------------------------------