代码改变世界

web.xml配置 struts2

2012-07-30 11:38  Patrick.Lee  阅读(4199)  评论(0编辑  收藏  举报

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>