By default, the configuration file name of struts2 is called struts. XML, and the file is placed in the SRC root directory. As shown in:
To modify the location of Struts. XML, for example, put struts. xml in the struts2 folder, the structure is shown in. What should I do?
The general configuration of struts2 in Web. XML is as follows:
<! -- Configure the struts2 filter: strutsprepareandexecutefilter --> <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 understand how stuts2 loads the configuration file, first view the strutsprepareandexecutefilter class of struts2:
package org.apache.struts2.dispatcher.ng.filter;/** * Handles both the preparation and execution phases of the Struts dispatching process. This filter is better to use * when you don't have another filter that 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 IOException, 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(); }}
We can see that there is a public void Init (filterconfig) throws servletexception {...} method. Obviously, this method will be called at startup, and thenLoad the Struts. xml configuration file under the classes directory. The Parameter Name of the configuration file is filterconfig. Therefore, you can specify the struts2 filter parameter, which is the same as the servlet parameter. Configure <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>
After this configuration, you can load the specified struts configuration file.