Spring IBatis Struts2 集成之二:Spring与Struts2集成

30 篇文章 0 订阅
11 篇文章 0 订阅
接首上一篇《Spring IBatis Struts2 集成之一:Spring与IBatis集成 》,本篇写Spring与Struts2的集成。
集成Spring  Struts2 的整个过程如下:
  1. 引入所有需要的*.jar文件。
  2. 配置web.xml。
  3. 配置struts2的配置文件struts.xml。
  4. 编写Action类及jsp页面。
  5. 测试。

一、所需类库 | 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集成插件类库


二、配置web.xml | How to configure web.xml


在web.xml中增加如下内容:
   <!-- 字符编码过滤器 -->
   <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>
    
     <!-- Struts2过滤器 -->
    <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>

三、配置Struts2 | How to configure struts2


在src/main/resouces 目录下建立struts.xml,其内容如下:
<!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" />
   
     <!-- 将Struts2对象管理交给Spring -->
    <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


在src/main/java 目录下建立 cn.aofeng.sis.action.HelloWorldAction类,其内容如下:
/**
 * 建立时间: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文件,其内容如下:
<?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、项目代码完整结构如下图:
Spring IBatis Struts2 集成之二:Spring与Struts2集成 | Spring IBatis Struts2 Integration II: Spring Integrates with Struts2 - 傲风 - 宝剑锋从磨砺出 梅花香自苦寒来


五、测试 | Test


将项目部署至Tomcat并启动,在浏览器中输入URL:http://localhost:8080/sis/hello!input.do(执行input()方法),结果如下图所示:
Spring IBatis Struts2 集成之二:Spring与Struts2集成 | Spring IBatis Struts2 Integration II: Spring Integrates with Struts2 - 傲风 - 宝剑锋从磨砺出 梅花香自苦寒来
 
2、输入用户ID,提交查询( 执行execute()方法),结果如下图所示:
Spring IBatis Struts2 集成之二:Spring与Struts2集成 | Spring IBatis Struts2 Integration II: Spring Integrates with Struts2 - 傲风 - 宝剑锋从磨砺出 梅花香自苦寒来
 
 至此,完成Spring与Struts2的集成。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值