Struts 2(二):使用Struts2

本文簡單描述如何在Eclipse中使用使用Struts2,並介紹一下Struts2的配置文件html

注:Struts2默認須要Java 5.0及其以上版本的運行環境支持,Web容器須要支持Servlet 2.4和JSP 2.0java

1、Eclipse+Struts

1.新建項目

在Eclipse中新建Dynamic Web Project,項目名爲StrutsPro,在WEB-INF/lib目錄下添加Struts 2框架的jar包,項目結構以下圖所示:web

 

2.配置web.xml

編輯項目中的web.xml文件,在該文件中配置Struts 2的核心Filter,編輯後的web.xml文件以下:apache

 1 <?xml version="1.0" encoding="UTF-8"?>
 2  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3    <display-name>StrutsPro</display-name>
 4   
 5    <filter>
 6        <filter-name>struts2</filter-name>
 7        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 8    </filter>
 9    <filter-mapping>
10        <filter-name>struts2</filter-name>
11        <url-pattern>/*</url-pattern>
12    </filter-mapping>
13 </web-app>

解說:瀏覽器

代碼第6行定義了核心過濾器的名稱爲struts2緩存

代碼第7行配置核心Filter的實現類爲org.apache.struts2.dispatcher.FilterDispatcherapp

代碼第11行用來配置核心過濾器過濾全部的Web請求框架

配置完web.xml文件後,項目就已經添加好Struts 2框架了jsp

 

3.配置struts.xml

struts.xml是Struts 2框架的核心配置文件,主要負責管理Struts 2框架下的業務控制器Action編碼

struts.xml放置在項目的WEB-INF/classes路徑下,須要在struts.xml文件中添加XML規範、DTD以及根目錄信息,編輯後的struts.xml文件以下:

1 <?xml version="1.0" encoding="UTF-8" ?><!-- XML聲明 -->
2 <!DOCTYPE struts PUBLIC
3   "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
4   "http://struts.apache.org/dtds/struts-2.1.dtd"><!-- 指定Struts 2配置文件的DTD信息 -->
5 <struts><!-- 根節點 -->    
6 </struts>

解說:

代碼第1行爲XML聲明

代碼第2行至第4行用來指定Struts 2配置文件的DTD信息

XML的根節點爲struts,全部的配置信息都放置在該節點之下。

 

2、Maven+Struts

暫略

 

3、配置文件

3.1 struts-default.xml文件

struts-default.xml文件包含在Struts2-core-2.x.x.jar中,是struts2框架默認加載的配置文件,它定義了一些bean和攔截器,爲框架提供默認設置

此外,Struts2有兩個核心配置文件:struts.xml文件、struts.properties文件

3.2 struts.xml文件

struts.xml文件主要負責管理應用中的Action映射,以及該Action包含的Result定義等,訪問路徑 = 命名空間 + 動做名稱

include  引入其它配置文件
package

屬性:name

  包的名稱,保持惟一。

屬性:extends

  通常狀況下須要繼承struts-default包,但不是必須的,若是不繼承將沒法使用struts2提供的核心功能

  struts-default在struts-default.xml中定義

屬性:namespace

  命名空間

action

 屬性:name

  動做名稱

屬性:class

  類的路徑

屬性:method

  方法名稱

result  
global  
constant

設置請求URL的擴展名:<constant name="struts.action.extension" value="do"></constant>

設置開發者模式:<constant name="struts.devMode" value="true"></constant>

   
   
   

案例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!-- 全部匹配*.action的請求都由struts2處理 -->
    <constant name="struts.action.extension" value="action" />
    <!-- 是否啓用開發模式 -->
    <constant name="struts.devMode" value="true" />
    <!-- struts配置文件改動後,是否從新加載 -->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 設置瀏覽器是否緩存靜態內容 -->
    <constant name="struts.serve.static.browserCache" value="false" />
    <!-- 請求參數的編碼方式 -->
    <constant name="struts.i18n.encoding" value="utf-8" />
    <!-- 每次HTTP請求系統都從新加載資源文件,有助於開發 -->
    <constant name="struts.i18n.reload" value="true" />
    <!-- 文件上傳最大值 -->
    <constant name="struts.multipart.maxSize" value="104857600" />
    <!-- 讓struts2支持動態方法調用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <!-- Action名稱中是否仍是用斜線 -->
    <constant name="struts.enable.SlashesInActionNames" value="false" />
    <!-- 容許標籤中使用表達式語法 -->
    <constant name="struts.tag.altSyntax" value="true" />
    <!-- 對於WebLogic,Orion,OC4J此屬性應該設置成true -->
    <constant name="struts.dispatcher.parametersWorkaround" value="false" />

    <include file="new-define.xml"></include>

    <package name="studentMgr" extends="struts-default" namespace="csu">
        <interceptors>
            <interceptor name="filter" class="com.csu.filter.CommitFilter"></interceptor>

            <interceptor-stack name="mystack">
                <interceptor-ref name="filter"></interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <action name="register" class="com.csu.action.LoginAction">

            <interceptor-ref name="mystack"></interceptor-ref>

            <result name="success" type="dispatcher">/studenInfo.jsp</result>
            <!--
                參數設置 
                name:對應Action中的get/set方法 
            -->
            <param name="url">http://www.csu.com</param>

        </action>
    </package>

</struts>

 

 

3.3 struts.properties文件

struts.properties文件一般放在Web應用的WEB-INF/classes路徑下,是一個標準的Properties文件,該文件定義了Struts 2框架的大量屬性,每一個key就是一個Struts 2屬性,該key對應的value就是一個Struts 2屬性值,開發者能夠經過改變這些key-value來知足應用的需求。

屬性 含義
struts.configuration 該屬性指定Struts2的配置文件管理器,該屬性的默認值是org.apache.Struts2.config.DefaultConfiguration;開發者能夠自行實現Configuration接口來加載Struts2的配置文件
   
   
   
   
   

 

參考資料:

Eclipse怎樣配置struts2_百度經驗

http://jingyan.baidu.com/article/fd8044fafdf0a25030137a7c.html

相關文章
相關標籤/搜索