Modify the Struts.xml profile location for Struts2

Source: Internet
Author: User

By default, the profile name of Struts2 is called Struts.xml, and the file is placed under the SRC root directory. As shown in the following:

If you need to modify the location of the struts.xml, for example, put Struts.xml in the Struts2 folder, as shown in the structure, what should I do?

The general configuration of Struts2 in Web. XML is as follows:

    <!--configuration Struts2 filter: Strutsprepareandexecutefilter--    <filter>        <filter-name>struts2</ Filter-name>        <filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter </filter-class>    </filter>    <filter-mapping>        <filter-name>struts2</ filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>

To find out how STUTS2 loads the configuration file, first look at the Struts2 Strutsprepareandexecutefilter class:

Package org.apache.struts2.dispatcher.ng.filter;/** * Handles Both the preparation and execution phases of the Struts disp  Atching process.  This filter was better to the use * if you don't have another filter, the needs access to action context information, such as Sitemesh.    */public class Strutsprepareandexecutefilter implements Strutsstatics, Filter {protected prepareoperations prepare;    Protected Executeoperations execute;protected list<pattern> excludedpatterns = null;        public void init (Filterconfig filterconfig) throws Servletexception {initoperations init = new Initoperations ();            try {filterhostconfig config = new Filterhostconfig (filterconfig);            init.initlogging (config);            Dispatcher Dispatcher = init.initdispatcher (config);            Init.initstaticcontentloader (config, dispatcher);            Prepare = new Prepareoperations (Filterconfig.getservletcontext (), dispatcher); Execute = new Executeoperations (FiltercoNfig.getservletcontext (), dispatcher); this.excludedpatterns = init.buildexcludedpatternslist (dispatcher);        Postinit (Dispatcher, filterconfig);        } finally {Init.cleanup ();  }}/** * Callback for post initialization */protected void Postinit (Dispatcher Dispatcher, Filterconfig Filterconfig) {} public void DoFilter (ServletRequest req, servletresponse Res, Filterchain chain) throws Ioexcepti        On, servletexception {httpservletrequest request = (httpservletrequest) req;        HttpServletResponse response = (httpservletresponse) res;            try {Prepare.setencodingandlocale (request, response);            Prepare.createactioncontext (request, response); Prepare.assigndispatchertothread (); if (excludedpatterns! = null && prepare.isurlexcluded (Request, Excludedpatterns) {Chain.dofilter (request, response);} else {request = Prepare.wraprequest (request); actionmapping mapping = prepare.findactionmapping(Request, response, true); if (mapping = = NULL) {Boolean handled = Execute.executestaticresourcerequest (Request, response if (!handled) {chain.dofilter (request, Response);}}        else {execute.executeaction (request, response, mapping);}}        } finally {prepare.cleanuprequest (request);    }} public void Destroy () {prepare.cleanupdispatcher (); }}

As you can see, there is a public void init (Filterconfig filterconfig) throws Servletexception {...} method, and it is clear that this method will be called at startup, and then Load the Struts.xml configuration file under the classes directory . The configuration file parameter name is Filterconfig. Therefore, we can specify the parameters of the Struts2 Filter, which is the same as specifying the servlet parameter, and is configured <init-param>. The configuration is as follows:

    <filter>        <filter-name>struts2</filter-name>        <filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>        <init-param >            <param-name>filterConfig</param-name>            <param-value>classpath:struts2/struts.xml </param-value>        </init-param>    </filter>    <filter-mapping>        < filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </ Filter-mapping>

With this configuration, the specified struts profile can be loaded normally.

source:http://blog.csdn.net/zht666/article/details/8980451

Modify the Struts.xml profile location for Struts2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.