作者:聂勇 欢迎转载,请保留作者信息并说明文章来源!
接着上一篇《Spring IBatis Struts2 集成之一:Spring与IBatis集成》,本篇写Spring与Struts2的集成。
集成Spring Struts2 的整个过程如下:
- 引入所有需要的*.jar文件。
- 配置web.xml。
- 配置struts2的配置文件struts.xml。
- 编写Action类及jsp页面。
- 测试。
一、所需类库 | Required jar list
c3p0-0.9.1.2.jar // 连接池实现类库
commons-logging-1.0.4.jar
ibatis-2.3.4.726.jar
log4j-1.2.15.jar
ojdbc14-10g.jar // Oracle JDBC驱动类库
spring-2.5.6.jar
spring-test.jar // 单元测试需要用到的类库
junit-4.4.jar // 单元测试需要用到的类库
-—————————————————————-在原来的基础上再增加如下类库(*.jar)
commons-beanutils-1.8.3.jar
commons-collections-3.2.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
freemarker-2.3.15.jar
struts2-core-2.1.8.1.jar // Struts2核心类库
xwork-core-2.1.6.jar // Struts2核心类库
struts2-spring-plugin-2.1.8.1.jar // Spring与Struts2集成插件类库
1、在web.xml中增加如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value> cn.aofeng.sis.action </param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping>
|
1、在src/main/resouces 目录下建立struts.xml,其内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.locale" value="zh_CN" /> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <constant name="struts.action.extension" value="do" /> <constant name="struts.configuration.xml.reload" value="false" /> <constant name="struts.objectFactory" value="spring" /> <package name="default" namespace="/" extends="struts-default"> <action name="hello" class="cn.aofeng.sis.action.HelloWorldAction"> <result name="view" type="dispatcher"> <param name="location">/WEB-INF/views/helloworld.jsp</param> </result> </action> </package> </struts>
|
四、编写Action类及jsp页面 | Create Action class and jsp page
1、在src/main/java 目录下建立 cn.aofeng.sis.action.HelloWorldAction类,其内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| * 建立时间:2011-3-22 */ package cn.aofeng.sis.action; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import cn.aofeng.sis.dao.UserDAO; import cn.aofeng.sis.domain.User; import com.opensymphony.xwork2.ActionSupport; * HelloWorld. * * @author 傲风 <a href="mailto:aofengblog@163.com">aofengblog@163.com</a> */ @Controller public class HelloWorldAction extends ActionSupport { private static final long serialVersionUID = 2828670087037313586L; @Resource(name="userDAO") private UserDAO _userDao; private User _user; public String input() throws Exception { return "view"; } public String execute() throws Exception { _user = _userDao.selectByUserId(_user.getUserId()); return "view"; } public User getUser() { return _user; } public void setUser(User user) { this._user = user; } }
|
2、在/WEB-INF/views 目录下建立 helloworld.jsp文件,其内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>SpringStruts2集成</title> </head> <body> <form method="post" action="hello.do"> 用户ID:<input type="text" name="user.userId" /> <br/> <input type="submit" /> <br/> <br/> <table width="100%" border="1"> <tr> <th>账号</th> <th>建立时间</th> <th>最近登录时间</th> </tr> <tr> <td><s:property value="user.userName" /></td> <td><s:property value="user.createTime" /></td> <td><s:property value="user.lastLoginTime" /></td> </tr> </table> </form> </body> </html>
|
3、项目代码完整结构如下图:
五、测试 | Test
1、将项目部署至Tomcat并启动,在浏览器中输入URL:http://localhost:8080/sis/hello!input.do (执行input()方法),结果如下图所示:
2、输入用户ID,提交查询(执行execute()方法),结果如下图所示:
至此,完成Spring与Struts2的集成。