熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

修改Struts2的struts.xml配置文件位置

2022-06-13   來源: Java開源技術 

  默認情況下Struts的配置文件名稱為strutsxml且該文件放在src根目錄下如下圖所示

\

  如果需要修改strutsxml的位置例如把strutsxml放到struts文件夾下結構如下圖所示該怎麼辦呢?

\

  Struts在webxml中的一般配置如下

  [html]

  struts

  orgapachestrutsdispatcherngfilterStrutsPrepareAndExecuteFilter

  struts

  /*

  struts

  orgapachestrutsdispatcherngfilterStrutsPrepareAndExecuteFilter

  struts

  /*

  為了弄清Stuts是如何加載配置文件的先查看Struts的StrutsPrepareAndExecuteFilter類

  [java] package orgapachestrutsdispatcherngfilter;

  /**

  * Handles both the preparation and execution phases of the Struts dispatching process This filter is better to use

  * when you dont 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 excludedPatterns = null;

  public void init(FilterConfig filterConfig) throws ServletException {

  InitOperations init = new InitOperations();

  try {

  FilterHostConfig config = new FilterHostConfig(filterConfig);

  initinitLogging(config);

  Dispatcher dispatcher = initinitDispatcher(config);

  initinitStaticContentLoader(config dispatcher);

  prepare = new PrepareOperations(filterConfiggetServletContext() dispatcher);

  execute = new ExecuteOperations(filterConfiggetServletContext() dispatcher);

  thisexcludedPatterns = initbuildExcludedPatternsList(dispatcher);

  postInit(dispatcher filterConfig);

  } finally {

  initcleanup();

  }

  }

  /**

  * 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 {

  preparesetEncodingAndLocale(request response);

  preparecreateActionContext(request response);

  prepareassignDispatcherToThread();

  if ( excludedPatterns != null && prepareisUrlExcluded(request excludedPatterns)) {

  chaindoFilter(request response);

  } else {

  request = preparewrapRequest(request);

  ActionMapping mapping = preparefindActionMapping(request response true);

  if (mapping == null) {

  boolean handled = executeexecuteStaticResourceRequest(request response);

  if (!handled) {

  chaindoFilter(request response);

  }

  } else {

  executeexecuteAction(request response mapping);

  }

  }

  } finally {

  preparecleanupRequest(request);

  }

  }

  public void destroy() {

  preparecleanupDispatcher();

  }

  }

  package orgapachestrutsdispatcherngfilter;

  /**

  * Handles both the preparation and execution phases of the Struts dispatching process This filter is better to use

  * when you dont 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 excludedPatterns = null;

  public void init(FilterConfig filterConfig) throws ServletException {

  InitOperations init = new InitOperations();

  try {

  FilterHostConfig config = new FilterHostConfig(filterConfig);

  initinitLogging(config);

  Dispatcher dispatcher = initinitDispatcher(config);

  initinitStaticContentLoader(config dispatcher);

  prepare = new PrepareOperations(filterConfiggetServletContext() dispatcher);

  execute = new ExecuteOperations(filterConfiggetServletContext() dispatcher);

  thisexcludedPatterns = initbuildExcludedPatternsList(dispatcher);

  postInit(dispatcher filterConfig);

  } finally {

  initcleanup();

  }

  }

  /**

  * 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 {

  preparesetEncodingAndLocale(request response);

  preparecreateActionContext(request response);

  prepareassignDispatcherToThread();

  if ( excludedPatterns != null && prepareisUrlExcluded(request excludedPatterns)) {

  chaindoFilter(request response);

  } else {

  request = preparewrapRequest(request);

  ActionMapping mapping = preparefindActionMapping(request response true);

  if (mapping == null) {

  boolean handled = executeexecuteStaticResourceRequest(request response);

  if (!handled) {

  chaindoFilter(request response);

  }

  } else {

  executeexecuteAction(request response mapping);

  }

  }

  } finally {

  preparecleanupRequest(request);

  }

  }

  public void destroy() {

  preparecleanupDispatcher();

  }

  }可以看到有個public void init(FilterConfig filterConfig) throws ServletException { } 方法很明顯這個方法在啟動時會被調用然後加載classes目錄下的strutsxml配置文件配置文件參數名稱為filterConfig因此我們可以指定Struts Filter 的參數這和指定Servlet參數相同配置即可配置如下

  [html]

  struts

  orgapachestrutsdispatcherngfilterStrutsPrepareAndExecuteFilter

  filterConfig

  classpath:struts/strutsxml

  struts

  /*

  struts

  orgapachestrutsdispatcherngfilterStrutsPrepareAndExecuteFilter

  filterConfig

  classpath:struts/strutsxml

  struts

  /*

  這樣配置後就可以正常加載指定的Struts配置文件了


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28733.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.