Struts 2 環境



完整類庫列表

antlr-2.7.2.jar
commons-beanutils-1.6.jar
commons-chain-1.1.jar
commons-logging-1.0.4.jar
commons-logging-api-1.1.jar
commons-validator-1.3.0.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
oro-2.0.8.jar
struts-core-1.3.5.jar
struts2-codebehind-plugin-2.0.11.1.jar
struts2-config-browser-plugin-2.0.11.1.jar
struts2-core-2.0.11.1.jar
struts2-jasperreports-plugin-2.0.11.1.jar
struts2-jfreechart-plugin-2.0.11.1.jar
struts2-jsf-plugin-2.0.11.1.jar
struts2-pell-multipart-plugin-2.0.11.1.jar
struts2-plexus-plugin-2.0.11.1.jar
struts2-sitegraph-plugin-2.0.11.1.jar
struts2-sitemesh-plugin-2.0.11.1.jar
struts2-spring-plugin-2.0.11.1.jar
struts2-struts1-plugin-2.0.11.1.jar
struts2-tiles-plugin-2.0.11.1.jar
tiles-api-2.0.4.jar
tiles-core-2.0.4.jar
tiles-jsp-2.0.4.jar
xwork-2.0.4.jar

基礎類庫列表

commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.jar
xwork-2.0.4.jar

基礎配置

WEB.XML
<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
Struts.xml
<?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>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <include file="example.xml"/>
    <!-- Add packages here -->
</struts>

配置文件

struts.xml 位置:classes下 配置 Action

package
struts對配置的組織結構
         類似於java package
         屬性:
           *  name  :   包的名字,唯一標示
              extends:  繼承
              namespace:命名空間
              abstract:  抽象申明
namespace
1 default namespace   ""
    2 root namespace "/"
    3 action 搜索的順序
            當前namespace ---一級一級向上搜索
            Web應用的請求路徑被視爲namespace
    4 訪問 /aaa.action的時候的搜索的順序
          "/"    --->   ""
    5  namespace 沒有目錄信息
    6  package 包含0 - N 個 action           
<pre>
;Action:<pre>
    name: 唯一標示   name.action ---> action name
    class:實現類    繼承actionsupport的類
    method:調用方法  動態調用的方法名稱
Result
name: 用來匹配action的返回值
    type: 是result-type 進行預定義的/自定義的 常用dispatcher,轉發
          <result-types>
              <result-type name="dispatcher" class =""  default="true" ></result-type>
          </result-types>
特殊位置:
<global-results>
   <result name=""></result>
           ....
</global-results>

特殊參數:
<result name="next_opertor" >${var_name}</result>
      var_name 必須是action裏面的一個私有屬性,並且有get/set方法
               必須符合bean的命名規則: 小寫開頭
                                       set/get大寫開頭的屬性
include
只能放在struts 標籤的下一級
  用來引入其他的struts配置文件
   file = " "
   被他引入xml文件是一個標準的struts配置

interceptors = > 攔截器

<interceptors>
     <interceptor name="" class=""/>
     <intercptor-stack name="">
      <interceptor-ref name=""/>
      <interceptor-ref name=""/>
     </interceptor-stack>
<interceptors>
<default-interceptor-ref name="">

<action。。。。。。>
    <interceptor-ref name="" />
constants
<constant name="" value="" />
可以完成
struts.properties 所完成的工作
可以完成
    constant 所完成的工作

struts.properties

配置struts特性(動態方法調用,虛擬機)

struts-default.xml struts...jar裏面,----〉零配置 struts-default.vm 模板

Action

I18N/I10N

攔截器

  1. 攔截器在struts 2接收到請求之後,抵達Action之前 以及離開Action之後 發揮作用
    1. 抵達Action之前,可以處理參數(文件上傳,struts2有內置攔截器),或者攔截請求
    2. 離開Action之後,可以處理Action返回的Result(嵌套模板的攔截器)
  2. 配置攔截器
    1. struts自己提供了缺省的攔截器,
    2. 攔截器的使用的第一步 ,配置、可以理解爲登記
    3. 攔截器的使用的第二步 ,配置、定義缺省interceptor-stack 、或者 直接指明 某個Action使用的攔截器
    4. 使用攔截器的時候以Stack爲單位
    5. 攔截器可以被覆蓋,採取就近配置的攔截器:: namespace範圍 或者 action範圍內部
    6. 攔截器之行的先後順序:根據配置文件內容,由上至下(抵達Action之前),由下至上(離開Action之後)
      註冊Interceptors:
      
       <interceptors>
                  <interceptor name="name_1" 
                         class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
                  .......
       </interceptors>
      
      註冊Interceptor-stack:
      
       <interceptor-stack name="stack_name_1">
                      <interceptor-ref name="name_1"/>
                       .......
      </interceptor-stack>
      
      使用Interceptor-stack
      
      在package中定義 :
      <default-interceptor-ref name="stack_name_1"/>
      
      在Action中定義         
          <action .......>
               <interceptor-ref name="stack_name_1"/>
          </action>
      <pre>
      
      ##系統自帶攔截器
      ###Alias Interceptor
      ####轉換參數名,內容不變<pre>
      name="alias" 
      class="com.opensymphony.xwork2.interceptor.AliasInterceptor"
      1. 實現參數讀取的攔截器 Parameters Interceptor
        1. 把請求中的參數放置到Action中 : 參數 ---》 Action中的 Property(attribute)
        2. 放置在 default stack ,在覆蓋的情況下可以直接使用
          name="params" 
          class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"
      2. 實現國際化的攔截器 I18n Interceptor
        1. 提供讀取國際化 properties文件鍵值的方法
        2. 放置在 default stack ,在覆蓋的情況下可以直接使用
          name="i18n" 
          class="com.opensymphony.xwork2.interceptor.I18nInterceptor"
      3. 實現cookie的讀寫和配置的攔截器 cookie Interceptor
        1. 提供Action中操作Cookie的方法
        2. 放置在 default stack ,在覆蓋的情況下可以直接使用
          name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"
      4. 實現struts2校驗功能的攔截器 validation Interceptor
        1. 放置在 default stack ,在覆蓋的情況下可以直接使用
        2. 在Action執行缺省或者配置的方法之前進行調用validation方法
          name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"
      5. 實現Struts中數據(參數)前後共享的 Chainning Interceptor
        1. Action前後可以讀取上下文的參數 (比如前一個 Action的屬性)
        2. 把所有使用到的Action 的屬性放置在上下文的attribute中
      6. Create Session Interceptor 自動創建 Session
        1. 提供 其他基於Session的Interceptor/Action提供session
          name="createSession"
          class="org.apache.struts2.interceptor.CreateSessionInterceptor"
      7. 異常攔截器 Exception Interceptor
        1. 把異常定位到一個確切的頁面
          name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"
      8. 提供Action操作 Session 與 Application(Servlet context )的方法Scope Interceptor
        1. Action直接操作session/application的方法
          name="scope" 
          class="org.apache.struts2.interceptor.ScopeInterceptor"
      9. 文件上傳的攔截器 fileuploader interceptor
        1. 提供基於struts2的文件上傳功能
          name="fileUpload" 
          class="org.apache.struts2.interceptor.FileUploadInterceptor"
      10. Logger Interceptor 輸出Action的名字

要點

在Struts 2中實現文件上傳
  1. 所需要的 jar文件
    基本的 struts2的 類庫 (commons-logging-1.0.4.jar日誌記錄 ,freemarker-2.3.8.jar 模板實現 ,ognl-2.6.11.jar onnl語言支持  ,struts2-core-2.0.11.jar struts2的核心類庫,xwork-2.0.4.jar struts2的基礎類庫)
    apache upload 組件 (commons-fileupload-x.xxxx.jar)
    apache io 組件 (commons-io-x.xx.xx.jar)
  2. 所需要的配置文件
    ;struts.properties :
         在該文件中需要修改臨時文件存儲路徑,文件的大小
         struts.multipart.saveDir= /upload   保存目錄,值得是realpath
         struts.multipart.maxSize=209715200  上傳文件的大小限制
         struts.multipart.parser=jakarta     解析上傳文件信息(multipart/form-data,
    上傳文件時的form數據類型一定是這種情況)
    ;struts.xml :
          在該文件中配置文件類型,以參數的形式放置在fileUpload攔截器引用中
          <interceptor-ref name="fileUpload">
          <param name="allowedTypes">
          image/png,application/pdf
          </param>
         </interceptor-ref>
  3. 所需要的操作
    在Action 中,直接使用  MultiPartRequestWrapper 來獲取上傳的文件及參數 (文件上傳的時候 ,
    通過正常的參數獲取渠道---params攔截器無法的到我們需要的參數,因爲該攔截器無法解析multipart
    類型的請求)
       這個時候 我們通過ServletActionContext得到的Request本身就是一個MultiPartRequestWrapper的實
    例,我們通過前置類型轉換的到該對象。  
          multipartInfo.getParameter("") 得到我們需要的普通參數
          multipartInfo.getFiles("");     得到我們需要的文件對象(但我們得到該文件的時候,文件
    已經被上傳,並且保存在struts.properties中所定義的目錄中,該 Request被返回給客戶端的時候,
    臨時文件會被刪除)
在Struts 2.0中國際化(i18n)應用程序
  1. 使用struts 的 Text標籤可以讀取在struts.properties文件中定義的消息文件中的消息文本
    struts.custom.i18n.resources 定義文件名稱(包含路徑信息,包含路徑信息的時候,路徑的分隔符用.)
    struts.locale=zh-cn    定義缺省的Location
    實際讀取的文件: 文件名稱+Locale的值+.properties   
    如果該文件找不到,會讀取   文件名稱+.properties   
    如果該文件依舊找不到,顯示key的值
    在所有的struts ui tag(用於界面顯示的標籤 都有一個key屬性,可以直接讀取消息文件中key對對應的值   
    
    當出現非ASCII CODE的時候,我們需要 先使用本地語言編碼編寫文字內容,然後使用jdk提供的
    native2ascii工具轉碼,成爲iso8859-1編碼的文本文件,否則會出現亂碼,該操作與IDE(Eclipse)所設
    定的缺省字符集,文件字符集等 無關
  2. 使用struts標籤中以及Action中可以使用getText的方式的到消息文件中的消息文本
    在struts中的Text標籤也是調用該方法
             在Action中的父類ActionSupport中已經定義了該方法,可以直接使用
             在Action中(不包含jsp頁面、模板頁面),不需要定義package.properties文件,但是可以直接讀取該文件中的key-value對應信息,
             package.properties             局部消息文件
             struts.custom.i18n.resources   全局消息文件
             定義了局部消息文件後,局部消息文件會首先被讀取。
             文件名的定義不變   filename_local.properties
  3. struts2中使用資源文件的local可以通過參數傳遞來得到
    參數是 java.util.Locale的實例
             設置方式: ActionContext.getContext().setLocale(locale參數)
             可以通過其它方式來設定locale,或者自我實現,但都不是Struts2的實現方式。
  4. struts2 中的國際化有可能出現亂碼,出現亂碼後的解決方式
    引用文件的字符集 (jsp有三個:  jsp自己 兩個:contentType中的charset和
    pageEncoding,  html 一個: meta 中的contentType中的charset,模板文件根據模板實現不同,但是與jsp方式類似),預定義不符合,或者三個字符集不一致。
             服務器返回給客戶端的數據編碼與客戶端請求不符合
             消息文件本身是亂碼:不一定是編寫的亂碼,而是讀取的時候使用的字符集與文件本省的字符
    集不一直,缺省情況下讀取的時候使用操作系統自己的缺省字符集讀取,單獨xp本身就有四種(其中三種是unicode,unicode的不同類型互補兼容--ASCII例外)
             解決方式:
                 1、轉碼或過濾器,可以註冊使用apache提供的字符集過濾器()
                 2、更新本地消息文件字符集、編碼內容,(另存爲新的字符集文件 轉成新的字符集編碼的文件 或者使用 java提供的 native2ascii轉換成iso8859-1的字符集)
                 3、修改jsp/模板文件中的有關字符集定義,如果jsp自己定義的兩個charset不一直,編譯本身就會出錯,只需要修改一致,如果修改一致後依舊是亂碼,可以一起替換成新的字符集
    事實上,會涉及字符集只有:utf-8 / iso8859-1 / gbk 
           CJK字符集 : 中日韓 字符集 JPN-xxxx;外包項目開發的時候纔會遇到
在Struts 2.0中實現表單數據校驗(Validation)
  1. 通過攔截器來實現,在Action中的方法被調用之前,首先調用validation方法或者其他驗證方法
  2. 驗證結束後,如果驗證失敗,返回結果(result)爲固定的input,返回內容中會包含錯誤信息。
    返回的錯誤集合以 key爲 "fieldErrors"的一個數據對象保存在request中,如果使用模板技術,
    該錯誤信息會顯示在該控件的下部(具體位置由模板控制),如果不使用模板技術,我們可以使用Struts2 Tag
    讀取"fieldErrors"的值進行顯示.
  3. Struts的校驗功能爲服務器端校驗
    校驗代碼: 
        配置文件struts.xml中的Action必須要有<result name="input"> </result>
        校驗代碼中需要有 addfieldError方法(儘量在該方法中使用I18N代碼,以方便國際化顯示,因爲這段代碼是硬編碼,不便於修改)
         需要添加
           @Override
     public void validate() {
               addfieldError(....);
            }
           方法到Action中,所有的驗證操作在該方法中實現.
  4. 如果驗證成功,調用Action定義的方法,返回邏輯上應該得到的結果。
在Struts 2中數據類型轉換
Struts
    field user.name
          user.password
    ----> 在action中尋找一個屬性,名稱爲user
                    if find
                       set user.name to user.name's value
                       set user.passwordto user.name's password
                    else
                       DEBUG:打印消息 
    -----> After Find it:
           數據類型轉換:
                    得到屬性 user的實例 (如果爲null,通過class創建實例,注意field對應的類、bean需要包含無參構造方法)
                    調用set方法,給該對象賦值   【在這個地方完成數據類型轉換】
                    
    【在這個地方完成數據類型轉換】                
  基本數據類型      <---->   String    
  引用數據類型      <---->   String
  比如:
      在數據類型轉換中,Date需要單獨處理,原因是格式的問題:
      Date 請求的 Locale 對應的 DateFormat 的 short 格式 
手動轉換類型(日期):
     轉換方式,
          1    重寫Struts的 StrutsTypeConverter                            
          2    註冊在Struts中
                  ActionName-conversion.properties  [針對Action]
                  xwork-conversion.properties       [針對Struts App]
          3   非 Struts 類型自定義轉換 @interface,就是jdk5之後的註釋
在程序中註冊過的日期類型可以自動被調用,無需干涉。
在Struts 2中實現IoC
  1. 在Struts 2中實現IoC
IoC
Inversion Of Control 反轉of控制,我們自己定義的類交給容器(運行上下文環境)去控制,並非在類的內部進行控制,依賴注入 (Dependency Injection) 與Ioc實際是一回事
class A   implement x
   class B   implement x
   interface x
   xxxFactory   
   xxxFactory.get(parameter);
通過這種方式可以 動態的調用 某一個已知接口的實現類、或者是某一個抽象類的子類。

在調用之前,被調用的類是未知的。

這種方式被稱爲接口驅動(Interface Driver Design)

在這種方式的基礎上 使用接口實現的形式對已經實現的類進行非浸入方式的注入,從而達到不修改實現類的情況下完成比原有代碼更多的工作,直接後果就是延緩接口的實現,在接口實現之前可以添加代碼。

這種行爲被稱爲依賴注入(Dependency Injection) === Inversion Of Control
配置
把誰注入給誰,如果沒有定義的話,依舊是一個典型的factory模式的代碼不能完成真正的Ioc操作 ,通過 framework 進行配置和管理
Struts2中使用 Ioc;
加入spring-xxx.jar 【提供spring支持,spring是一個輕量級的Ioc容器,如果沒有完整的jar文件 ,可以使用必須的四個包:spring-context-2.5.4.jar,spring-web-2.5.4.jar,spring-core-2.5.4.jar,spring-beans-2.5.4.jar】
加入struts2-spring-plugin-2.0.11.1.jar【用於提供在Struts中使用spring Ioc控制的鏈接】
加入配置文件
ApplicationContext.xml 【配置Ioc中的Java Bean ( POJO,非 Ejb 的 JavaBean ) 】
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
    "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
      <bean id="" class=""> 
                <property name ="Field_Name" >
              <ref local ="another_bean_id" />
         </property >
                <property name ="Field_Name" >
                 </propertie>
      </bean> 
</beans>
  1. find action
  2. search action with classname in applicationcontext.xml
  3. use a action class's instance defined by applicationcontext.xml
完成一個Struts2 with Spring的IOC程序的步驟
  1. 定義 Struts2中的Action 【使用 applicationContext.xml中的bean_id作為我們的Action的class】
    <action name="CatchIocDataMethod"
       class="com.baoding.Action.CatchIocDataMethod">
          成為:
          <action name="CatchIocDataMethod"
       class="ActionClassId in ApplicationContext">
  2. 定義Spring
    1. 拷貝jar:spring-context-2.5.4.jar,spring-web-2.5.4.jar,spring-core-2.5.4.jar,spring-beans-2.5.4.jar,struts2-spring-plugin-2.0.11.1.jar 5個文件到/WEB-INFO/lib
    2. 建立文件 applicationContext.xml 到 /WEB-INFO/目錄 【如果在web.xml中定義了spring配置文件的路徑,另當別論】
WEB.xml中路進的配置方式
<context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/classes/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
     </context-param>
    1. 修改struts.properties文件或者struts.xml文件,定義struts中ObjectFactory使用的容器為Spring
      【struts.properties】
              struts.objectFactory = spring //去掉改行注釋
              【struts.xml】
              <constant name="struts.objectFactory" value="spring" /> 
              所有的在struts.properties文件中出現的常量都可以使用這種方式在struts.xml中進行覆蓋
  1. 提示信息
    Initializing Struts-Spring integration  **  struts2-spring-plugin 被使用後的提示信息
    Bean factory for application context .....  Spring Ioc容器成功初始化後的信
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章