<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    一葉笑天
    雄關漫道真如鐵, 而今邁步從頭越。 從頭越, 蒼山如海, 殘陽如血。
    posts - 73,comments - 7,trackbacks - 0
    原文位置:http://downpour.javaeye.com/blog/24239

    論壇中用Struts+Spring的人不少,以前的帖子也有問過Struts+Spring的整合方式。前面的帖子中ReadOnly老大曾經提到過Spring2.0新增加的一個整合方式。今天簡單把這幾種整合方式小結一下。

    在這之前,別忘了用一下Google大法,一般早有人會對類似的問題做過回答,果然,在ibm developworks上有一篇文章,一下子涵蓋了三種整合方式,有興趣的xdjm可以參考下面的鏈接:http://www-128.ibm.com/developerworks/cn/java/j-sr2.html。

    下面著重談一下Spring2.0新增的一個整個方式,我感覺挺不錯,可以完全將Struts的配置和Spring的配置分離。具體步驟如下:
    1. 編寫Spring的配置文件applicationContext.xml,簡單起見,僅僅定義一個Service對象。
    引用

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>

    <bean id="userService" class="com.bearingpoint.gdc.zero.service.impl.UserServiceImpl" />

    </beans>

    這看上去就和普通的Spring配置文件沒有任何分別。
    2. 編寫Struts的配置文件struts-config.xml,注意其中的controller的配置,用到了Spring2.0的新特性。
    引用

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config
    PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
    "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"
    >

    <struts-config>

    <action-mappings>

    <action path="/addUser"
    type="com.bearingpoint.gdc.zero.action.user.AddUser"
    scope="request"
    >
    <forward name="success" path="/index.jsp" />
    </action>

    </action-mappings>

    <controller processorClass="org.springframework.web.struts.AutowiringRequestProcessor" />

    </struts-config>


    3. 然后為你的Struts的Action注入你需要的Service
    引用

    private UserService userService;

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    User user = new User();
    userService.addUser(user);
    return mapping.findForward("success");
    }

    /**
    * @param userService
    *            The userService to set.
    */
    public void setUserService(UserService userService) {
    this.userService = userService;
    }

    看上去你好像啥都沒做,而事實上,注入工作已經由AutowiringRequestProcessor自動完成。

    4. 編寫web.xml進行測試。
    引用

    ?xml version="1.0" ?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

    <web-app>

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
        </context-param>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
    <servlet-name>struts</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
    <param-name>config</param-name>
    <param-value>/WEB-INF/classes/struts-config.xml</param-value>
    </init-param>
    <init-param>
    <param-name>detail</param-name>
    <param-value>2</param-value>
    </init-param>
    <init-param>
    <param-name>validate</param-name>
    <param-value>true</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>struts</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>

        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>


    最后,啟動Jetty進行測試,順利運行通過!

    看上去如此簡單,配置起來也沒有什么很特別的地方,只是按照常規來寫你的Spring和Struts的配置文件就好了。

    不過在這里還是說一下其中的要注意兩個問題:
    1. 這種autowire的注入支持兩種不同的方式,分別是byName和byType,默認是byType。我想這對于絕大多數開發者來說是夠了。
    2. 鑒于在http://www.javaeye.com/topic/15057中所提到的OpenSessionInView模式的失效的問題。我仔細看了一下Spring的源碼。對于這種autowire的整合方式,不推薦在struts-config.xml文件中配置ContextLoaderPlugIn,而是采用web.xml中的ContextLoaderListener來加載Spring的初始化配置。否則,你的OpenSessionInView模式可能會失效。
    posted on 2008-12-22 13:55 一葉笑天 閱讀(853) 評論(1)  編輯  收藏 所屬分類: 開源技術

    FeedBack:
    # re: 使用AutowiringRequestProcessor整合struts和Spring2.0
    2014-12-29 15:24 | AUTOCHECK
    1. 這種autowire的注入支持兩種不同的方式,分別是byName和byType,默認是byType。我想這對于絕大多數開發者來說是夠了。
    請問怎么配置方式為byName呢?  回復  更多評論
      
    主站蜘蛛池模板: 18禁免费无码无遮挡不卡网站| 青娱乐在线视频免费观看| 国产在线观a免费观看| 亚洲成a人片在线观看老师| 亚洲av综合av一区二区三区 | 亚洲国产精品无码中文字| 国产精品小视频免费无限app | 亚洲人成免费网站| 亚洲精品中文字幕乱码| xx视频在线永久免费观看| 色偷偷亚洲女人天堂观看欧| 欧美三级在线电影免费| 亚洲精品无码一区二区| 免费一级毛片女人图片| eeuss免费影院| 亚洲av日韩av激情亚洲| 222www免费视频| 亚洲国产午夜精品理论片在线播放| 国产成人3p视频免费观看| 一级做a爰片久久毛片免费陪 | mm1313亚洲国产精品无码试看| 国产网站免费观看| 黄桃AV无码免费一区二区三区| 亚洲国产精品无码AAA片| 中文字幕天天躁日日躁狠狠躁免费| 亚洲国产最大av| 亚洲欧洲久久久精品| 一级毛片**不卡免费播| 亚洲色在线无码国产精品不卡| 免费一级毛片在线播放| 无码国产精品一区二区免费3p| 在线观看日本亚洲一区| 亚洲综合伊人久久综合| 台湾一级毛片永久免费| 美女羞羞免费视频网站| 亚洲国产精品国自产电影| 国内自产拍自a免费毛片| 99re6在线视频精品免费| 国产日本亚洲一区二区三区| 亚洲伊人成无码综合网| 免费精品人在线二线三线区别|