您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關Struts2的配置文件web.xml是怎樣的,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
任何MVC框架都需要與Web應用整合,這就不得不借助于web.xml文件,只有配置在web.xml文件中Servlet才會被應用加載。
通常,所有的MVC框架都需要Web應用加載一個核心控制器,對于Struts2框架而言,需要加載FilterDispatcher,只要Web應用負責加載FilterDispatcher,FilterDispatcher將會加載Struts2框架。
因為Struts2將核心控制器設計成Filter,而不是一個普通Servlet。故為了讓Web應用加載FilterDispatcher,只需要在web.xml文件中配置FilterDispatcher即可。
標準配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <filter> <!-- 配置Struts2核心Filter的名字 --> <filter-name>struts2</filter-name> <!-- 配置Struts2核心Filter的實現類 --> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</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> <param-name>configProviders</param-name> <param-value>lee.MyConfigurationProvider</param-value> </init-param> </filter> <!-- 配置Filter攔截的URL --> <filter-mapping> <!-- 配置Struts2的核心FilterDispatcher攔截所有用戶請求 --> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
全部配置如下
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Showcase Application</display-name> <filter> <filter-name>struts-prepare</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class> </filter> <filter> <filter-name>struts-execute</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class> </filter> <filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class> </filter> <filter-mapping> <filter-name>struts-prepare</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts-execute</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class> org.apache.struts2.tiles.StrutsTilesListener </listener-class> </listener> <!-- Chat Example in Showcase --> <listener> <listener-class> org.apache.struts2.showcase.chat.ChatSessionListener </listener-class> </listener> <listener> <listener-class>org.apache.struts2.dispatcher.listener.StrutsListener</listener-class> </listener> <!-- SNIPPET START: dwr --> <servlet> <servlet-name>dwr</servlet-name> <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet> <servlet-name>JspSupportServlet</servlet-name> <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Sitemesh Freemarker and Velocity Decorator Servlets. Shares configuration with Struts.--> <servlet> <servlet-name>sitemesh-freemarker</servlet-name> <servlet-class>org.apache.struts2.sitemesh.FreemarkerDecoratorServlet</servlet-class> <init-param> <param-name>default_encoding</param-name> <param-value>UTF-8</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>sitemesh-velocity</servlet-name> <servlet-class>org.apache.struts2.sitemesh.VelocityDecoratorServlet</servlet-class> <init-param> <param-name>default_encoding</param-name> <param-value>UTF-8</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>sitemesh-freemarker</servlet-name> <url-pattern>*.ftl</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>sitemesh-velocity</servlet-name> <url-pattern>*.vm</url-pattern> </servlet-mapping> <!-- END SNIPPET: dwr --> <!-- SNIPPET START: example.velocity.filter.chain <filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter> <filter-name>sitemesh</filter-name> <filter-class>org.apache.struts2.sitemesh.VelocityPageFilter</filter-class> </filter> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> END SNIPPET: example.velocity.filter.chain --> <!-- SNIPPET START: example.freemarker.filter.chain <filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter> <filter-name>sitemesh</filter-name> <filter-class>org.apache.struts2.sitemesh.FreeMarkerPageFilter</filter-class> </filter> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> END SNIPPET: example.freemarker.filter.chain --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>default.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- Restricts access to pure JSP files - access available only via Struts action --> <security-constraint> <display-name>No direct JSP access</display-name> <web-resource-collection> <web-resource-name>No-JSP</web-resource-name> <url-pattern>*.jsp</url-pattern> </web-resource-collection> <auth-constraint> <role-name>no-users</role-name> </auth-constraint> </security-constraint> <security-role> <description>Don't assign users to this role</description> <role-name>no-users</role-name> </security-role> </web-app>
*如上,當配置Struts2的FilterDispatcher類時,可以指定一系列的初始化參數,為該Filter配置初始化參數時,其中有3個初始化參數有特殊意義:
config:使用逗號隔開的字符串,每個字符串都是一個XML配置文件的位置。Struts框架將自動加載該屬性指定的系列配置文件。
actionPackages:使用逗號隔開的字符串,每個字符串都是一個包空間,Struts2框架將掃描這些包空間下的Action。
configProviders:配置自己的ConfigurationProvider類。
還可以在此處配置Struts2常量,每個<init-param>元素配置一個Struts2常量,其中<param-name>子元素指定了常量name,而<param-value>子元素指定了常量value。
*在web.xml中配置了<Filter>元素,還需要配置該Filter攔截的URL。通常,我們讓該Filter攔截所有的用戶請求。可以能過<filter-mapping>元素配置。
以上就是Struts2的配置文件web.xml是怎樣的,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。