您的位置:首页 > 编程语言 > Java开发

02-Spring之環境配置(Spring+Struts2,Eclipse)

2011-07-27 11:30 369 查看
說明:
      1、代碼顏色為#ff6600
一、軟件要求
        Spring: Srping 2.5.6版本    下載地址:http://www.springsource.com/download/community?sid=1243024  下載先註冊下,隨便填寫就可以了。
       注,我下載的是spring-framework-2.5.6-with-docs.zip ,不是spring-framework-2.5.6.SEC02-with-docs.zip

二、環境配置
        此環境配置是結合Struts2來完成的,即Spring+Struts的整合,單獨配置將在以後描述。Struts的設置是接上篇文章的結果來繼續的。下麵是Spring的環境配置。
        1、解壓spring-framework-2.5.6-with-docs.zip文件
        2、打開解壓后的文件夾,選擇 spring.jar   將其放置/WEB-INF/lib目錄下。
 三、改進第一篇文章,以Spring+Struts2實現。
        1、文件介紹
           applicationContext.xml  Spring的默認配置文件。

       步驟:
             a、打開/WEB-INF/web.xml文件,在<web-app></web-app>中追加以下內容。
                                                                        <listener>
                                                                          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                                                                       </listener>
             b、新建文件applicationContext.xml文件,其位置在/WEB-INF/applicationContext.xml,位置與web.xml相同。注:這與struts.xml的位置是不同的。在applicationContext.xml文件添加如下內容:
                                                       <?xml version="1.0" encoding="UTF-8"?>
                                                       <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-2.0.xsd">
                                                    <bean name="loginSpring" class="com.UserAction">
                                                   </bean>
                                             </beans>
               關於applicationContext.xml內容配置將在後續文章中講到。
               c、打開/src/struts.xml文件,將action標籤做了一點修改,比較內容如下:
               原來的內容:
                       <action name="login" class="com.test.UserAction">
                       <result>/success.jsp</result>
                       </action>
                 修改后的內容:
                     <action name="login" class="loginSpring">
                      <result>/success.jsp</result>
                       </action>
                  說明:修改后的標籤action的屬性class值是loginSpring 其是與applicationContext.xml中的bean標籤的name屬性值相一致。

  四、測試
        Sping的簡單配置已經完成,現在可以向第一篇文章的測試方式一樣,在此不再贅述。

            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息