Struts2.1.6与Spring2.5.6框架整合222

来源:百度文库 编辑:神马文学网 时间:2024/04/26 07:51:38
1、在MyEclipse中新建项目(test)
2、Struts包导入
   暂时导入所必需的包,其他包将在用到时导入:
   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
 3、复制在Struts目录的例子程序中WEB-INF\classes\struts.xml文件,粘贴到项目的src目录下,主要保留其文件头:
 1
 2 3    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4    "http://struts.apache.org/dtds/struts-2.0.dtd">
 5
 6
 7    
 8
 9    
10

4、配置web.xml
 1
 2 3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 5    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 6
 7    
 8        struts2
 9        
10            org.apache.struts2.dispatcher.FilterDispatcher
11        

12    

13    
14        struts2
15        /*
16    

17
18    
19        index.jsp
20    

215、引入Spring包,在dist目录下
   spring.jar
6、在src目录下建立三个文件
   applicationContext-actions.xml
   applicationContext-beans.xml
   applicationContext-common.xml
   这三个文件其实是applicationContext.xml的分解,为的是避免所有配置放在同一文件,造成混乱。
   结构均如下:
 1
 2
 3 4    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5    xmlns:context="http://www.springframework.org/schema/context"
 6    xmlns:tx="http://www.springframework.org/schema/tx"
 7    xmlns:aop="http://www.springframework.org/schema/aop"
 8    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 9                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
10                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
11                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
12
13
7、需要在web.xml进行配置
    
        contextConfigLocation
        classpath*:applicationContext-*.xml
    

    
        
            org.springframework.web.context.ContextLoaderListener
        

    
   前一段代码的配置是因为我们配置了后一段代码的配置后它默认是到WEB-INF下查找applicationContext.xml文件,我们现在改到src目录下并进行文件分解。

8、需要引入Struts2中的另一个包
   struts2-spring-plugin-2.1.6.jar
9、测试是否整合成功
(1)建立页面login.jsp、welcome.jsp、error.jsp分别为登录页面、登录成功页面、出错页面
login.jsp
 1<%@ page language="java" contentType="text/html; charset=GB18030"
 2    pageEncoding="GB18030"%>
 3<%@ taglib prefix="s" uri="/struts-tags"%>
 4
 5
 6    
 7        
 8        登录页面
 9    
10    
11        
12            
13            
14            
15        
16    
17welcome.jsp
 1<%@ page language="java" contentType="text/html; charset=GB18030"
 2    pageEncoding="GB18030"%>
 3
 4
 5    
 6        
 7        登录成功
 8    
 9    
10        用户名:${username }
11        

12        密码:${password }
13        

14    
15
(2)建包com.test.manager和com.test.manager.impl分别存放业务逻辑处理的接口和其实现,分别建立接口LoginManager.java和其实现LoginManagerImpl.java
LoginManager.java
1package com.test.manager;
2
3public interface LoginManager {
4    public boolean isLogin(String username, String password);
5}LoginManagerImpl.java,只是测试用,判断用户名密码是否为intrl、intrl,若是则登录成功
 1package com.test.manager.impl;
 2
 3import com.test.manager.LoginManager;
 4
 5public class LoginManagerImpl implements LoginManager{
 6    public boolean isLogin(String username, String password)
 7    {
 8        if(null!=username&&null!=password&&"intrl".equals(username.trim())&&"intrl".equals(password.trim()))
 9        {
10            return true;
11        }
12        return false;
13    }
14}(3)在applicationContext-beans.xml把实现类配置上,以让Spring进行管理
            class="com.test.manager.impl.LoginManagerImpl">
    (4)创建包com.test.action用于存放action,并新建LoginAction.java,继承ActionSupport类
包含从页面所接收参数username、password,以及业务逻辑处理类LoginManager类型的loginManager,给username和password设置get、set,给loginManager设置set方法,以让Spring为我们自动注入;overwrite父类中的
 1package com.test.action;
 2
 3import com.opensymphony.xwork2.ActionSupport;
 4import com.test.manager.LoginManager;
 5
 6@SuppressWarnings("serial")
 7public class LoginAction extends ActionSupport {
 8    private LoginManager loginManager;
 9    private String username;
10    private String password;
11
12    public String getUsername() {
13        return username;
14    }
15
16    public void setUsername(String username) {
17        this.username = username;
18    }
19
20    public String getPassword() {
21        return password;
22    }
23
24    public void setPassword(String password) {
25        this.password = password;
26    }
27
28    public void setLoginManager(LoginManager loginManager) {
29        this.loginManager = loginManager;
30    }
31    
32    @Override
33    public String execute() throws Exception {
34
35        if(loginManager.isLogin(username, password))
36        {
37            return SUCCESS;
38        }
39        return INPUT;
40    }
41}(5)在applicationContext-actions.xml中进行配置,也让Spring来管理LoginAction
            scope="prototype">
        
    (6)在struts.xml中进行配置,处理页面提交的请求,配置action:login,login一定要和login.jsp中form的action属性名匹配。此时struts.xml文件如下:
 1
 2 3    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4    "http://struts.apache.org/dtds/struts-2.0.dtd">
 5
 6
 7    
 8        
 9            welcome.jsp
10            login.jsp
11            error.jsp
12        
13    
14

(7)此时目录结构如下:

(8)部署项目Tomcat服务器,启动服务器,进入登录页面,进行登录测试:
若用户名密码不为intrl/intrl,则登录失败,返回登录页面,注意地址栏的变化

若输入intrl/intrl则登录成功,跳至welcome.jsp页面,显示用户名和密码

10、以上结果证明,Struts2.1.6与Spring2.5.6框架整合成功

11、源代码下载
http://www.rayfile.com/files/b82cd5e3-66b2-11de-836f-0014221b798a/

12、其他资源
struts-2.1.6-all.zip:http://www.rayfile.com/files/d71417ae-66dd-11de-9d35-0014221b798a/
spring-framework-2.5.6-with-dependencies.zip:http://www.rayfile.com/files/6819bd23-66e2-11de-ad79-0014221b798a/
jdk-6u12-windows-i586-p.exe:http://www.rayfile.com/files/2879e173-66f0-11de-9cd8-0014221b798a/
apache-tomcat-6.0.16.exe:http://www.rayfile.com/files/918febc7-66ed-11de-ab58-0014221b798a/
mysql\MYSQL123 5 5.0.zip:http://www.rayfile.com/files/dee8bc17-66f1-11de-a255-0014221b798a/

posted on 2009-04-12 16:38 intrl 阅读(1059) 评论(20)  编辑 收藏 引用 所属分类: Java 、Struts2 、Spring