八、 配置Struts2
Struts的配置文件都會(huì)在web.xml中注冊(cè)的。
a) Struts的配置文件如下:
Src/struts.xml
b) struts_book.xml配置文件如下:
文件中的<interceptor-ref name="params"/>,使用了struts2自己的攔截器,攔截器在AOP(Aspect-Oriented Programming)中用于在某個(gè)方法或字段被訪問之前,進(jìn)行攔截然后在之前或之后加入某些操作。攔截是AOP的一種實(shí)現(xiàn)策略。
Struts 2已經(jīng)提供了豐富多樣的,功能齊全的攔截器實(shí)現(xiàn)。大家可以到struts2-all-2.0.6.jar或struts2-core-2.0.6.jar包的struts-default.xml查看關(guān)于默認(rèn)的攔截器與攔截器鏈的配置。
在struts-default.xml中已經(jīng)配置了大量的攔截器。如果您想要使用這些已有的攔截器,只需要在應(yīng)用程序struts.xml文件中通過“<include file="struts-default.xml" />”將struts-default.xml文件包含進(jìn)來,并繼承其中的struts-default包(package),最后在定義Action時(shí),使用“<interceptor-ref name="xx" />”引用攔截器或攔截器棧(interceptor stack)。一旦您繼承了struts-default包(package),所有Action都會(huì)調(diào)用攔截器棧 ——defaultStack。當(dāng)然,在Action配置中加入“<interceptor-ref name="xx" />”可以覆蓋defaultStack。
作為“框架(framework)”,可擴(kuò)展性是不可或缺的,因?yàn)槭郎蠜]有放之四海而皆準(zhǔn)的東西。雖然,Struts 2為我們提供如此豐富的攔截器實(shí)現(xiàn),但是這并不意味我們失去創(chuàng)建自定義攔截器的能力,恰恰相反,在Struts 2自定義攔截器是相當(dāng)容易的一件事。所有的Struts 2的攔截器都直接或間接實(shí)現(xiàn)接口com.opensymphony.xwork2.interceptor.Interceptor。除此之外,大家可能更喜歡繼承類com.opensymphony.xwork2.interceptor.AbstractInterceptor。
九、 配置Spring
1、Spring的配置文件如下:
2、Struts.properties.xml
本來此文件應(yīng)該寫在struts 配置一節(jié),但主要是考慮這體現(xiàn)了集成spring的配置,所以放在spring的配置這里來講。
struts.objectFactory:ObjectFactory 實(shí)現(xiàn)了 com.opensymphony.xwork2.ObjectFactory接口(spring)。struts.objectFactory=spring,主要是告知Struts 2運(yùn)行時(shí)使用Spring來創(chuàng)建對(duì)象(如Action等)。當(dāng)然,Spring的ContextLoaderListener監(jiān)聽器,會(huì)在web.xml文件中編寫,負(fù)責(zé)Spring與Web容器交互。
struts.locale:The default locale for the Struts application。 默認(rèn)的國際化地區(qū)信息。
struts.i18n.encoding:國際化信息內(nèi)碼。
十、 Web.xml配置
在Struts 2中,配置有少許改變,最明顯的是分發(fā)器(dispatcher)已由Servlet轉(zhuǎn)為Servlet Filter, 其配置和Servlet一樣簡單。和Servlet配置一樣,Filter配置中定義了Filter的名稱(作為引用)和類名。Filter Mapping通過URI和名稱匹配來調(diào)用相應(yīng)的Filter。默認(rèn)情況下,擴(kuò)展名為“.action”,這是在default.properties文件(在Struts 2 JAR文件里)的“struts.action.extension”屬性定義的。
待續(xù).....
Powered by: BlogJava Copyright © 詩特林