一、两种网上的方法,本人是想在说s2sh项目中加入xheditor(在线编辑器)功能,通过下面的两种方法发现不是action有问题,就是servlet有问题,因此不行。
但是毕竟这两种方法简单好理解,不放先试一下,说不定你的项目能行。
解决Could notfind act
原配置文件:
修改后
.........
或
.....
<servlet-mapping>
二、一种保证有效的方法
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>