S2SH web.xml 同时配置Struts2与Servlet的解决方法

一、两种网上的方法,本人是想在说s2sh项目中加入xheditor(在线编辑器)功能,通过下面的两种方法发现不是action有问题,就是servlet有问题,因此不行。

但是毕竟这两种方法简单好理解,不放先试一下,说不定你的项目能行。

解决Could notfind action or result  There is noAction mapped for namespace / and action nameChk有两种偏门的办法:1、修改servlet的配置文件的<url-pattern>,struts2的filter中<url-pattern>不变2、修改struts2的filter中<url-pattern>,servlet中的<url-pattern>不变

原配置文件:

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

 <servlet>
   <servlet-name>Chk</servlet-name>
   <servlet-class>com.thdx.action.Chk</servlet-class>
  </servlet>

 <servlet-mapping>
   <servlet-name>Chk</servlet-name>
   <url-pattern>/Chk</url-pattern>
 </servlet-mapping>

修改后

.........

   <filter-name>struts2</filter-name>
  <url-pattern>*.action</url-pattern>
 </filter-mapping>  //servlet中的<url-pattern>不变

.....

<servlet-mapping>
   <servlet-name>Chk</servlet-name>
   <url-pattern>/Chk.servlet</url-pattern>//点后面随便怎么写
 </servlet-mapping> //struts2的filter中<url-pattern>不变


二、一种保证有效的方法

package com.xheditor.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
* 该类是定义了一个过滤器(随便你放在摸个包下面),当既用到struts2有用到servlet时,
* 必须用该过滤器对所有的servlet做一次过滤,否则servlet将无法正常运行。
* 同时在web.xml中,在定义struts的核心控制器前面添加该过滤器,添加代码如下:
* <filter>
*   <filter-name>redisp</filter-name>
*   <filter-class>filters.ReDispatcherFilter(该过滤器的类名)</filter-class>
*   <init-param>
*    <param-name>includeServlets</param-name>
*    <param-value> 所配置的servlet,注意是那url -pattern</param-value>
*   </init-param>
* </filter>
* <filter-mapping>
*   <filter-name>redisp</filter-name>
*   <url-pattern>/*</url-pattern>
* </filter-mapping>
* @author xsl
*
*/
public class ReDispatcherFilter implements Filter {

public void destroy() {
   // TODO Auto-generated method stub

}

public void doFilter(ServletRequest req, ServletResponse resp,
    FilterChain chain) throws IOException, ServletException
{
 
   HttpServletRequest request = (HttpServletRequest)req;
   String target = request.getRequestURI();
   target = target.lastIndexOf("?")>0   
      ?target.substring(target.lastIndexOf("/")+1,target.lastIndexOf("?")-target.lastIndexOf("/"))
      :target.substring(target.lastIndexOf("/")+1);
 
   System.out.println(target);
   if(this.includes.contains(target))
   {
    RequestDispatcher rdsp = request.getRequestDispatcher(target);
   
    System.out.println("go..............."+rdsp);
    rdsp.forward(req, resp);
   }
   else
    chain.doFilter(req, resp);
}
private ArrayList<String> includes = new ArrayList<String>();

public void init(FilterConfig config) throws ServletException {
 
   this.includes.addAll( Arrays.asList(config.getInitParameter("includeServlets").split(",")));

}

}


怕大家还是配置错误,给一个web.xml的配置实例。

<filter>
        <filter-name>redisp</filter-name>
        <filter-class>com.xheditor.servlet.ReDispatcherFilter</filter-class>
        <init-param>
            <param-name>includeServlets</param-name>
            <param-value>UploadFileServlet</param-value>
           </init-param>
     </filter>
    <filter-mapping>
        <filter-name>redisp</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


     <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>
    
    <servlet>
        <servlet-name>XhEditor</servlet-name>
        <servlet-class>com.xheditor.servlet.UploadFileServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>XhEditor</servlet-name>
        <url-pattern>/UploadFileServlet</url-pattern>
    </servlet-mapping>

你可能感兴趣的:(servlet,struts,String,filter,action,XSL)