您现在的位置是:首页 >

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

火烧 2022-03-21 00:58:12 1034
修改Strut 2的 trut .xml配置文件位置   默认情况下 Strut 的配置文件名称为 trut xml 且该文件放在 rc根目录下 如下图所示   如果需要修改 trut xml的位置

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

  默认情况下 Struts 的配置文件名称为struts xml 且该文件放在src根目录下 如下图所示

  如果需要修改struts xml的位置 例如把struts xml放到struts 文件夹下 结构如下图所示 该怎么办呢?

  Struts 在web xml中的一般配置如下

  []

  struts

   apache struts dispatcher ng filter StrutsPrepareAndExecuteFilter

  struts

  /*

  struts

   apache struts dispatcher ng filter StrutsPrepareAndExecuteFilter

  struts

  /*

  为了弄清Stuts 是如何加载配置文件的 先查看Struts 的StrutsPrepareAndExecuteFilter类

  [java] package apache struts 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 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();

  }

  }

  package apache struts dispatcher ng filter;

  /**

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

  * 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 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();

  }

  }可以看到 有个public void init(FilterConfig filterConfig) throws ServletException { } 方法 很明显 这个方法在启动时会被调用 然后加载classes目录下的struts xml配置文件 配置文件参数名称为filterConfig 因此 我们可以指定Struts Filter 的参数 这和指定Servlet参数相同 配置即可 配置如下

  []

  struts

   apache struts dispatcher ng filter StrutsPrepareAndExecuteFilter

  filterConfig

  classpath:struts /struts xml

  struts

  /*

  struts

   apache struts dispatcher ng filter StrutsPrepareAndExecuteFilter

  filterConfig

  classpath:struts /struts xml

  struts

  /*

lishixinzhi/Article/program/Java/ky/201311/28733  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码