免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1382 | 回复: 0
打印 上一主题 下一主题

Struts2整合Spring [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-01 20:00 |只看该作者 |倒序浏览

strut2提供了一种非常简单的方式来实现与spring的整合,记得以前用struts1还要改配置文件,struts通过一种“可插拨式”的插件,实现了与Spring框架的整合。在实现应用中,只需要把struts2-spring-plugin-x.x.x.x.jar(其中的xxxx为版本号)文件拷到应用的lib下即可。
       Struts2提供了两种基本的整合策略:
1.         将Action实例交给Spring容器来负责生成,管理,通过这种方式,可以充分利用Spring容器的IOC特性,提供最好的解耦;
2.         利用Spring插件的自动装配方式,当Spring插件创建Action实例之后,立即将Spring窗口中对应的业务逻辑组件注入Action实例。
下面看看两个实例:
首先:把spring.jar commons-logging.jar及struts2-spring-plugin-x.x.x.x.jar等相关的包拷到lib下,修改web.xml:添加linstener
org.springframework.web.context.ContextLoaderListener
这样配置之后,Spring自动查找WEB-INF路径下的applicationContext.xml配置文件。当然也可以自己指定配置文件,则需要在web.xml中元素,如下:
       contextConfigLocation
       /WEB-INF/my.xml
  
1.用第一方式实现struts2与spring的整合

           (1)一个简单的用户登录页面login.jsp:
         



@ page language="java" contentType="text/html; charset=gb2312"%>



@ taglib prefix="s" uri="/struts-tags"%>

html>

head>

meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

title>用户登录title>

head>

body>

s:form action="Login">

    s:textfield name="username" label="用户信息"/>

s:textfield name="password" label="用户信息"/>

    s:submit value="登录"/>

s:form>

body>

html>

      (2)实现控制器逻辑(LoginAction.java):

package my;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Action;

public class LoginAction implements Action



{

    private static final Logger log = Logger.getLogger(LoginAction.class);

    private MyService ms;

    private String tip;

    private String username;

    private String password;

    public void setMs(MyService ms)


   

{

        this.ms = ms;

    }

    public void setPassword(String password)


   

{

        this.password = password;

    }

    public String getPassword()


   

{

        return this.password;

    }

    public void setUsername(String username)


   

{

        this.username = username;

    }

    public String getUsername()


   

{

        return this.username;

    }


    public void setTip(String tip)


   

{

        this.tip=tip;

    }

    public String getTip()


   

{

        return this.tip;

    }

    public String execute()throws Exception


   

{   

            if (ms.valid(getUsername(),getPassword()))


            

{

                setTip("呵,整合成功!");

                return SUCCESS;

            }

            else

                return ERROR;

        

    }

}(3)struts2的配置文件(struts.xml):注意class属性不是指向一个action,而是指向spring 的一个bean

xml version="1.0" encoding="UTF-8"?>

DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

struts>

    package name="strutsqs" extends="struts-default">

        action name="Login" class="loginAction">

            result name="error">/login.jspresult>

            result name="success">/success.jspresult>

        action>

    package>

struts>
(4)业务逻辑接口(MyService.java):

package my;

public interface MyService



{

    public boolean valid(String username , String password);

}(5)实现业务逻辑(MySeviceImpl.java):

package my;

public class MyServiceImpl implements MyService



{

    public boolean valid(String username , String password)


   

{

        if (username.equals("hello") && password.equals("world"))


        

{

            return true;

        }

        else

            return false;

    }

}(6)Spring的配置文件my.xml:

xml version="1.0" encoding="GBK"?>

指定Spring配置文件的Schema信息 -->

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.xsd">

    bean id="myservice" class="my.MyServiceImpl"/>

    bean id="loginAction" class= "my.LoginAction" scope="prototype">

        property name="ms" ref="myservice"/>

    bean>

beans>ok.整合完毕。
2.使用自动装配方式整合
    (1)指定自动装配的策略,让Spring 自动管理Bean与Bean之间的依赖有关系,无需使用ref显式指定依赖Bean ,Spring容器会自动检查XML配置文件内容,为主调Bean 注入依赖Bean。Spring 提供了3种装配策略,通过修改struts.objectFactory.spring.autoWire常量指定,可接受的3个值:
     1.name:根据属性名自动装配,这个是默认值。
     2.type根据属性类型自动装配
     3.auto自动检测需要使用哪种自动装配方式
  在上面的简单应用中,只需要修改配置文件即可:
    1.修改struts.xml配置文件:class属性与没有整合时一样的。。

xml version="1.0" encoding="UTF-8"?>

DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">


struts>

   

    package name="strutsqs" extends="struts-default">


        action name="Login" class="my.LoginAction">


            result name="error">/login.jspresult>

            result name="success">/success.jspresult>

        

        action>

        

    package>


struts>2.修改spring的配置文件my.xml,因为action中需要业务逻辑组件名为ms,所以必须在配置业务逻辑组件时,指定其id属性为ms
  

xml version="1.0" encoding="GBK"?>

指定Spring配置文件的Schema信息 -->

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.xsd">

    bean id="ms" class="my.MyServiceImpl"/>


beans>ok,完成。。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/11570/showart_1674702.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP