先说一下我自己的开发环境吧,我的开发环境是:JDK1.6+MyEclipse6.6+Tomcat6.0.18,如果你的开发环境与我这里的不一样,不必担心!只要你的开发环境为JDK1.5以上+MyEclipse6.0以上+Tomcat5.5以上,也同样可以达到本文最后的相同效果,包括其他的开发工具(如Jbuilder,NetBeans,JBoss,Weblogic等)也是如此。
废话少说,时间就是生命,咱们开始动手吧,:
1、收集或下载下列的所有资源:
巧妇难为无米之炊,没有下面这些资源,我们将无从下手。不过,如果你已经全部有了这些资源,可以直接跳过这一步。
(1)struts-2.1.6-all.zip:http://www.rayfile.com/zh-cn/files/fbf3c36b-c2fa-11df-be7a-0015c55db73d/
(2)spring-framework-2.5.6-with-dependencies.zip:http://www.rayfile.com/zh-cn/files/2a174778-c2fb-11df-88ab-0015c55db73d/
(3)jdk-6u12-windows-i586-p.exe:http://www.rayfile.com/files/2879e173-66f0-11de-9cd8-0014221b798a/
(4)apache-tomcat-6.0.16.exe:http://www.rayfile.com/files/918febc7-66ed-11de-ab58-0014221b798a/
(5)整合成功后完成的源代码(含jar包):http://www.rayfile.com/zh-cn/files/07b643cf-e1ae-11df-8d47-0015c55db73d/
2、新建Web项目并加入相应的jar包:
(1)在MeEclipse中新建一个web项目,输入一个项目名称,如我的是:Struts216Spring256,选中“Java EE 5.0”选项(支持JSTL),其他默认即可,直接点击完成,如图:
(2)导入相应jar外:
解压第1步中的struts-2.1.6-all.zip和spring-framework-2.5.6-with-dependencies.zip这两个资源,
复制strut2.1.6的解压目录struts-2.1.6\lib下的如下jar包到上面建好的web项目的WebRoot/WEB-INF/lib里面:
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
freemarker-2.3.13.jar
ognl-2.6.11.jar
struts2-core-2.1.6.jar
xwork-2.1.2.jar
struts2-spring-plugin-2.1.6.jar(如果struts2不与spring整合,不需要导入这个包,但在我们这里当然是需要的)
同样的,
复制spring2.5.6解压目录spring-framework-2.5.6\dist的spring.jar到web项目的WebRoot/WEB-INF/lib里面:
spring.jar
到此,所有我们需要的jar都导入完成了。
3、添加struts.xml并配置web.xml:
(1)在项目src目录中增加文件struts.xml,struts.xml文件的初始内容如下:
<?xml version="1.0" encoding="GBK" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 默认使用simple主题,其他可选的主题有xhtml,css_xhtml,ajax等 --> <constant name="struts.ui.theme" value="simple"/> <package name="struts2" extends="struts-default"> <!-- 这里添加Action的映射配置 --> </package> </struts>
(2)配置web.xml文件,以集成支持struts2框架,web.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- Spring内置的字符编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 添加Struts2支持 --> <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> <!-- 添加Spring支持 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
4、添加 applicationContext-action.xml、applicationContext-common.xml和applicationContext-service.xml文件:(这三个文件其实是applicationContext.xml的分解,目的一是模块化配置,另一方面也为了将来与hibernate集成)
在项目的src目录下添加 applicationContext-action.xml、applicationContext-common.xml和applicationContext-service.xml文件,这三个文件的初始内容完全相同,文件内容如下:
<?xml version="1.0" encoding="GBK"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 后面会在这里配置bean --> </beans>
至此,struts2与spring的最简单集成已经完成了,利用MyEclipse发布web项目到tomcat下,启动tomcat没有发现错误,说明最简单的配置已经成功。稍后,我们会在此基础上实现一个简单的用户登录示例。
5、实现一个用户登录的示例:
(1)建立页面login.jsp、welcome.jsp、error.jsp分别为登录页面、登录成功页面、出错页面:
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登录页面</title> </head> <body> <s:form action="login" method="post"> 用户名:<s:textfield name="username"/> <br> 密 码:<s:password name="password"/> <br> <s:submit value="登录"/> </s:form> </body> </html>
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>登录成功</title> </head> <body> 用户名:${username } <br> 密码:${password } <br> </body> </html>
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>登录失败</title> </head> <body> 登录失败! <br> <a href="login.jsp">请重新登录</a> <br> </body> </html>
(2)建包com.a8.service和com.a8.service.impl分别存放业务逻辑处理的接口和其实现,分别建立接口LoginService.java和其实现LoginServiceImpl.java
package com.a8.service;
public interface LoginService {
public boolean isLogin(String username, String password);
}
LoginServiceImpl .java,只是非常简单的测试登录用户名和密码,判断用户名密码是否为“myname”、“123456”,若是则登录成功,否则登录失败。在实际开发中,这里通常是一个数据库的操作实现。
package com.a8.service.impl;
import com.a8.service.LoginService;
public class LoginServiceImpl implements LoginService {
public boolean isLogin(String username, String password) {
//这里仅仅以简单的用户名和密码做测试,实现开发中应该是查询数据库用户表
//登录用户名为“myname”,密码为“123456”,则登录成功
if (null != username && null != password
&& "myname".equals(username.trim())
&& "123456".equals(password.trim())) {
return true;
}
return false;
}
}
(3)在applicationContext-service.xml中添加业务实现类的配置,让Spring来管理此bean:
需要注意的是,在配置class属性时,必须保证LoginServiceImpl所在的包位置是一致的。
<bean id="loginService" class="com.a8.service.impl.LoginServiceImpl"> </bean>
(4)创建包com.a8.web.action用于存放action,并新建LoginAction.java,继承ActionSupport类
包含从页面所接收参数username、password,以及业务逻辑处理类LoginService类型的loginService,给username和password设置get、set,给loginService设置set方法,以让Spring为我们自动注入其实现类对象。
package com.a8.web.action;
import com.a8.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = -6505213250169207817L;
private LoginService loginService;
private String username;
private String password;
@Override
public String execute() throws Exception {
if (loginService.isLogin(username, password)) {
return SUCCESS;
}
return INPUT;
}
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 void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
}
(5)在applicationContext-action.xml中进行添加LoginAction配置,也让Spring来管理LoginAction:
需要注意的是,在配置class属性时,必须保证LoginAction所在的包位置是一致的。
<bean id="loginAction" class="com.a8.web.action.LoginAction" scope="prototype"> <property name="loginService" ref="loginService"></property> </bean>
(6)在struts.xml中进行配置,处理页面提交的请求,配置action:login,login一定要和login.jsp中form的action属性名匹配。此时struts.xml文件如下:
<?xml version="1.0" encoding="GBK" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 默认使用simple主题,其他可选的主题有xhtml,css_xhtml,ajax等 --> <constant name="struts.ui.theme" value="simple"/> <package name="struts2" extends="struts-default"> <!-- 这里添加Action的映射配置 --> <action name="login" class="loginAction"> <result name="success">welcome.jsp</result> <result name="input">login.jsp</result> <result name="error">error.jsp</result> </action> </package> </struts>
(7)项目的所有目录结构如图所示:
6、测试项目示例是否成功:
(1)发布项目到tomcat服务器,进入登录页面,测试登录:
输入用户名myname和密码123456,登录成功:
哈哈,结过测试,登录成功,以上结果说明Struts2.1.6与Spring2.5.6整合成功!
附:完整的源代码(包括jar包)下载地址:
http://www.rayfile.com/zh-cn/files/07b643cf-e1ae-11df-8d47-0015c55db73d/