web.xml配置 struts2
(2012-03-25 02:30:41)(摘自網絡)
配置FilterDispatcher的代碼如下:
<!–STRUTS2框架的核心Filter–>
<filter>
<!–配置STRUTS2核心Filter的名字–>
<filter-name>struts</filter-name>
<!–配置STRUTS2核心Filter的名字–>
<filter-class>org.apache.struts2.dispatcher.FilterDispacher</filter-class>
<init-param>
<!–配置STRUTS2框架默認加載的ACTION包結構–>
<param-name>actionPackages</param-name>
<param-value>org.apache.struts2.showcase.person</param-value>
</init-param>
<!–配置STRUTS2框架的配置提供者類–>
<init-param>configProviders</init-param>
<param-value>com.pjwqh.MyConfigurationProvider</param-value>
</filter>
正如上面看到的,當配置STRUTS2的FilterDispatcher類時,可以一系統初始化的參數,為該Filter配置初始化參數,其中有3個初始化參數有特殊意義:
1.config:該參數的值是一個以英文逗號(,)隔開的字符串,每個字符串都是一個XML配置文件的位置。STRUTS2框架將自動加載該屬性指定的配置文件。
2.actionPackages:該參數的值也是一個以英文逗號(,)隔開的字符串,每個字符串都是一個包空間,STRUTS2框架將掃描這些包空間下的Action類。
3.configProviders:如果用戶需要實現自己的ConfigurationProvider類,用戶可以提供一個或者多個實現了ConfigurationProvider接口的類,然后將這些類的類名設置成該屬性的值,多個類名之間以英文逗號隔開。
除此之外,還可在此處配置STRUTS2常量,每個<init-param></init-param>元素配置一個STRUTS2常量,其中<param-name>子元素指定了常量name,而<init-value>子元素指定了常量value。
在web.xml文件中配置了該Filter,還需要配置該Filter攔截的URL。通常,我們讓該Filter攔截所有的用戶請求,因此使用通配符來配置該Filter攔截的URL。
下面是配置該Filter攔截URL的配置片段:
<!–置該Filter攔截URL–>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置了STRUTS2的核心FilterDispatcher后基本是完成了STRUTS2在web.xml文件中的配置。
如果WEB應用使用了SERVLET2.3以前的規范,因為不會自動加載STRUTS2框架的標簽文件,因此必須在web.xml中配置加載STRUTS2標簽庫。
配置STRUTS2的標簽庫配置片段如下:
<!–手動配置STRUTS2的標簽庫–>
<taglib>
<!–配置STRUTS2標簽庫的URL–>
<taglib-url>/s</taglib-url>
<!–指定STRUTS2標簽庫定義文件的位置–>
<taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
</taglib>
在上面配置片段中,指定了STRUTS2標簽庫配置文件的物理位置:/WEB-INF/struts-tags.tld,因此我們必須手動復制STRUTS2的標簽庫定義文件(包含在struts2-core-2.06.jar文件的META-INF路徑下,文件名為struts-tag.tld),將該文件放置在WEB應用的WEB-INF路徑下。
如果使用servlet2.4以上的規范,則無需在web.xml文件中配置標簽庫定義,因為servlet2.4規范會自動加載標簽庫定義文件。、
-----------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<!-- 過濾器名字 -->
<filter-name>struts2</filter-name>
<!-- 過濾器支持的struts2類 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2 </filter-name>
<!-- /*代表過濾器攔截所有的請求,也就是說不管你訪問的后綴名是什么如jsp,action,do他都攔截
而*.action代表過濾器只攔截以action結尾的請求,其他的如jsp,do結尾的都不管 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<jsp-config>
<!--
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-been</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>-->
<taglib>
<taglib-uri>/s</taglib-uri>
<taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>