struts2的完整類庫清單
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
基礎類庫清單(struts五個必須的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配置)
- 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
1、package struts對配置的組織結構類似于java package屬性:
1.1 name:包的名字,唯一标示
1.2 extends: 繼承
1.3 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:調用方法 動态調用的方法名稱
1.4 abstract: 抽象申明
- 2、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大寫開頭的屬性
3、include -
隻能放在struts 标簽的下一級 用來引入其他的struts配置檔案 file = " " 被他引入xml檔案是一個标準的struts配置
4、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="" />
- 5、constants
-
<constant name="" value="" /> 可以完成 struts.properties 所完成的工作 可以完成 constant 所完成的工作
struts.properties
配置struts特性(動态方法調用,虛拟機)
struts-default.xml struts...jar裡面,----〉零配置 struts-default.vm 模闆
Action
I18N/I10N
攔截器
- 攔截器在struts 2接收到請求之後,抵達Action之前 以及離開Action之後 發揮作用
- 抵達Action之前,可以處理參數(檔案上傳,struts2有内置攔截器),或者攔截請求
- 離開Action之後,可以處理Action傳回的Result(嵌套模闆的攔截器)
- 配置攔截器
- struts自己提供了預設的攔截器,
- 攔截器的使用的第一步 ,配置、可以了解為登記
- 攔截器的使用的第二步 ,配置、定義預設interceptor-stack 、或者 直接指明 某個Action使用的攔截器
- 使用攔截器的時候以Stack為機關
- 攔截器可以被覆寫,采取就近配置的攔截器:: namespace範圍 或者 action範圍内部
- 攔截器之行的先後順序:根據配置檔案内容,由上至下(抵達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"
- 實作參數讀取的攔截器 Parameters Interceptor
- 把請求中的參數放置到Action中 : 參數 ---》 Action中的 Property(attribute)
- 放置在 default stack ,在覆寫的情況下可以直接使用
name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"
- 實作國際化的攔截器 I18n Interceptor
- 提供讀取國際化 properties檔案鍵值的方法
- 放置在 default stack ,在覆寫的情況下可以直接使用
name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"
- 實作cookie的讀寫和配置的攔截器 cookie Interceptor
- 提供Action中操作Cookie的方法
- 放置在 default stack ,在覆寫的情況下可以直接使用
name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"
- 實作struts2校驗功能的攔截器 validation Interceptor
- 放置在 default stack ,在覆寫的情況下可以直接使用
- 在Action執行預設或者配置的方法之前進行調用validation方法
name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"
- 實作Struts中資料(參數)前後共享的 Chainning Interceptor
- Action前後可以讀取上下文的參數 (比如前一個 Action的屬性)
- 把所有使用到的Action 的屬性放置在上下文的attribute中
- Create Session Interceptor 自動建立 Session
- 提供 其他基于Session的Interceptor/Action提供session
name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor"
- 提供 其他基于Session的Interceptor/Action提供session
- 異常攔截器 Exception Interceptor
- 把異常定位到一個确切的頁面
name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"
- 把異常定位到一個确切的頁面
- 提供Action操作 Session 與 Application(Servlet context )的方法Scope Interceptor
- Action直接操作session/application的方法
name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"
- Action直接操作session/application的方法
- 檔案上傳的攔截器 fileuploader interceptor
- 提供基于struts2的檔案上傳功能
name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"
- 提供基于struts2的檔案上傳功能
- Logger Interceptor 輸出Action的名字
- 實作參數讀取的攔截器 Parameters Interceptor
要點
在Struts 2中實作檔案上傳
- 所需要的 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)
- 所需要的配置檔案
;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>
- 所需要的操作
在Action 中,直接使用 MultiPartRequestWrapper 來擷取上傳的檔案及參數 (檔案上傳的時候 , 通過正常的參數擷取管道---params攔截器無法的到我們需要的參數,因為該攔截器無法解析multipart 類型的請求) 這個時候 我們通過ServletActionContext得到的Request本身就是一個MultiPartRequestWrapper的實 例,我們通過前置類型轉換的到該對象。 multipartInfo.getParameter("") 得到我們需要的普通參數 multipartInfo.getFiles(""); 得到我們需要的檔案對象(但我們得到該檔案的時候,檔案 已經被上傳,并且儲存在struts.properties中所定義的目錄中,該 Request被傳回給用戶端的時候, 臨時檔案會被删除)
在Struts 2.0中國際化(i18n)應用程式
- 使用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)所設 定的預設字元集,檔案字元集等 無關
- 使用struts标簽中以及Action中可以使用getText的方式的到消息檔案中的消息文本
在struts中的Text标簽也是調用該方法 在Action中的父類ActionSupport中已經定義了該方法,可以直接使用 在Action中(不包含jsp頁面、模闆頁面),不需要定義package.properties檔案,但是可以直接讀取該檔案中的key-value對應資訊, package.properties 局部消息檔案 struts.custom.i18n.resources 全局消息檔案 定義了局部消息檔案後,局部消息檔案會首先被讀取。 檔案名的定義不變 filename_local.properties
- struts2中使用資源檔案的local可以通過參數傳遞來得到
參數是 java.util.Locale的執行個體 設定方式: ActionContext.getContext().setLocale(locale參數) 可以通過其它方式來設定locale,或者自我實作,但都不是Struts2的實作方式。
- 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)
- 通過攔截器來實作,在Action中的方法被調用之前,首先調用validation方法或者其他驗證方法
- 驗證結束後,如果驗證失敗,傳回結果(result)為固定的input,傳回内容中會包含錯誤資訊。
傳回的錯誤集合以 key為 "fieldErrors"的一個資料對象儲存在request中,如果使用模闆技術, 該錯誤資訊會顯示在該控件的下部(具體位置由模闆控制),如果不使用模闆技術,我們可以使用Struts2 Tag 讀取"fieldErrors"的值進行顯示.
- Struts的校驗功能為伺服器端校驗
校驗代碼: 配置檔案struts.xml中的Action必須要有<result name="input"> </result> 校驗代碼中需要有 addfieldError方法(盡量在該方法中使用I18N代碼,以友善國際化顯示,因為這段代碼是寫死,不便于修改) 需要添加 @Override public void validate() { addfieldError(....); } 方法到Action中,所有的驗證操作在該方法中實作.
- 如果驗證成功,調用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
- 在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>
- find action
- search action with classname in applicationcontext.xml
- use a action class's instance defined by applicationcontext.xml
完成一個Struts2 with Spring的IOC程式的步驟
- 定義 Struts2中的Action 【使用 applicationContext.xml中的bean_id作為我們的Action的class】
<action name="CatchIocDataMethod" class="com.baoding.Action.CatchIocDataMethod"> 成為: <action name="CatchIocDataMethod" class="ActionClassId in ApplicationContext">
- 定義Spring
- 拷貝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
- 建立檔案 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>
-
- 修改struts.properties檔案或者struts.xml檔案,定義struts中ObjectFactory使用的容器為Spring
【struts.properties】 struts.objectFactory = spring //去掉改行注釋 【struts.xml】 <constant name="struts.objectFactory" value="spring" /> 所有的在struts.properties檔案中出現的常量都可以使用這種方式在struts.xml中進行覆蓋
- 修改struts.properties檔案或者struts.xml檔案,定義struts中ObjectFactory使用的容器為Spring
- 提示資訊
Initializing Struts-Spring integration ** struts2-spring-plugin 被使用後的提示資訊 Bean factory for application context ..... Spring Ioc容器成功初始化後的資訊