一、问题的提出
先让我们看下web.xml中struts2和servlet的配置:
<!-- struts2拦截页面请求的配置
-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- servlet的配置
-->
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>com.servlets.ImageServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/myImageServlet/*</url-pattern>
</servlet-mapping>
当我们在页面在请求应用时,struts2将会截获所有请求,对于servlet请求将不能够正常响应的
(报错信息为:HTTP Status 404 - There is no Action
mapped for namespace / and action name
myImageServlet.),
这是因为struts2把servlet当成action了,因为servlet和action都是没有后缀的.
二、解决方案
解决方法目前有四种:
方法1:
统一在servlet后面加上.servlet结尾(包括web.xml配置文件中和页面上使用servlet的地方)
方法2(比较通用奥):
在struts.xml中的<struts>的节点下面添加struts2处理的请求后缀
常量:
<!--
该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理。
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。
-->
<constant name="struts.action.extension"
value="action" />
方法3:
在web.xml中修改struts2拦截页面请求的配置:
原:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
现:
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/user/*</url-pattern>
</filter-mapping>
servlet的请求路径不必改变
方法4:
自定义filter对servlet进行过滤:
##############################################
#
该类是定义了一个过滤器(随便你放在摸个包下面),当既用到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>
*该类也可以直接继承StrutsPrepareAndExecuteFilter的
###########################################################
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>
<!--
struts2拦截页面请求的配置
-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- servlet的配置
-->
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>com.servlets.ImageServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/myImageServlet/*</url-pattern>
</servlet-mapping>