今天在做一个项目的时候遇到了一个很奇怪的问题,这个项目的一部分是之前用servlet写的,但是后边打算用struts写,再加入struts框架配置后发现原先的那部分不能正常访问了,之前没这么做过所以很纳闷,struts2配置自认为写的很熟了,应该没什么问题啊,struts.xml文件改了又改还是找不到错误,后来在看到web.xml时突然眼前一亮。。
- <img src="https://img-blog.csdn.net/20140124152934046?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hhb2hvbmd4dWFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
- <span style="font-size:24px;">原来是所有的请求都被struts2拦截了,<span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;">struts2把servlet当成action了,因为servlet和action都是没有后缀的。</span><span style="color: rgb(73, 73, 73); font-family: simsun; line-height: 21px; background-color: rgb(226, 226, 226);"><span style="word-wrap: normal; word-break: normal; line-height: 28px; font-family: 宋体; color: red;"></span></span></span>
- <span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;"><span style="font-size:24px;"> 很是悲剧,要想使servlet和struts各司其职只能是限制struts的拦截范围了,</span></span>
- <span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;">
- </span>
- <span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;"><img src="https://img-blog.csdn.net/20140124153004875?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hhb2hvbmd4dWFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
- </span>
- <span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;"><span style="font-size:24px;">这里在struts.xml的配置里边加一个这个就可以限制struts只拦截action类型的请求了,当然在发起请求时也是要写清楚http://XXX/XX.action了这个action就不能向之前那样省掉了。</span></span>
- <span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;"><span style="font-size:24px;">下面给出网上的几种方法:</span></span>
- <span style="color: rgb(51, 51, 51); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19px;"></span><p style="color: rgb(51, 51, 51); margin: 10px auto; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px;"><strong>方式一:修改servlet的相关配置,统一在servlet后面加上“.servlet”</strong>
- 1.修改web.xml配置文件形如:</p><p style="color: rgb(51, 51, 51); margin: 10px auto; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px;"></p><pre code_snippet_id="169749" snippet_file_name="blog_20140124_7_3832600" name="code" class="html"> <servlet>
- <servlet-name>jqueryAjaxServlet</servlet-name>
- <servlet-class>com.clzhang.sample.struts2.servlet.jQueryAjaxServlet</servlet-class>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>jqueryAjaxServlet</servlet-name>
- <url-pattern>/servlet/jqueryAjax.servlet</url-pattern>
- </servlet-mapping>
- </pre><br><br><p></p><p></p><div class="cnblogs_code" style="color:rgb(51,51,51); line-height:26px; font-family:'Courier New'; border:1px solid rgb(204,204,204); padding:5px; overflow:auto; margin:5px 0px"><div style="background-color:rgb(245,245,245); font-size:14px"></div><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><span style="font-size:24px">2.修改调用servlet的地方,形如:</span></p><p style="background-color:rgb(245,245,245); font-size:13px; color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"></p><pre code_snippet_id="169749" snippet_file_name="blog_20140124_8_6168129" name="code" class="html" style="background-color: rgb(245, 245, 245); font-size: 14px;"> <%
- String path = request.getContextPath();
- %>
- ......
-
- $.ajax({
- url:'<%=path%>/servlet/jqueryAjax.servlet',
- </pre><span style="background-color:rgb(245,245,245); font-size:14px"><br><br></span><p style="background-color:rgb(245,245,245); font-size:14px"></p><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><span style="font-size:18px">这样就可以正常处理servlet的请求了。此种情况适合于小范围使用servlet的情况。</span></p><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><strong><span style="font-size:18px">方式二:修改拦截页面配置,就是将struts的相关拦截配置一下</span></strong></p><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><span style="font-size:18px">修改web.xml文件如下内容:</span></p><p style="background-color:rgb(245,245,245); font-size:13px; color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"></p><pre code_snippet_id="169749" snippet_file_name="blog_20140124_9_1358580" name="code" class="html" style="background-color: rgb(245, 245, 245); font-size: 14px;"> <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>*.do</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></pre><span style="background-color:rgb(245,245,245); font-size:14px"><br><br></span><p style="background-color:rgb(245,245,245); font-size:14px"></p><p style="background-color:rgb(245,245,245); font-size:13px; color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><br></p><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><strong><span style="font-size:24px">方式三:自定义Filter实现过滤</span></strong></p><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><span style="font-size:24px">1.创建实现类,代码参考如下:</span></p><p style="background-color:rgb(245,245,245); font-size:13px; color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"></p><pre code_snippet_id="169749" snippet_file_name="blog_20140124_10_9613692" name="code" class="html" style="background-color: rgb(245, 245, 245); font-size: 14px;">package com.clzhang.sample.struts2;
-
- 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;
-
- /**
- * 这是一个过滤servlet的filter;就是拦截servlet的处理请求,自行转向处理,而不是由struts处理。
- * 有二种过滤方式,一种是判断uri中是否包含“/servlet/”;
- * 另一种是所有servlet在初始化参数中指定。
- * 需要在web.xml中配置:
- <filter>
- <filter-name>redisp</filter-name>
- <filter-class>com.clzhang.sample.struts2.FilterServlet</filter-class>
- <!--如果使用第二种方式过滤,则需要下面的代码
- <init-param>
- <param-name>includeServlets</param-name>
- <param-value>jqueryAjax,jsonView</param-value>
- </init-param>
- -->
- </filter>
-
- <filter-mapping>
- <filter-name>redisp</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- 并且此过滤器必须在struts2的过滤器上面。
- * @author Administrator
- *
- */
- public class FilterServlet implements Filter {
- public void destroy() {
- }
-
- 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);
- if (request.getRequestURI().indexOf("/servlet/") > 0) {
- // request.getRequestURI()格式应该形如:/st/servlet/jqueryAjax,
- // 其中st是项目名,servlet是所有servlet都增加的前缀,用于能够判断出是servlet。
- // if只判断请求uri是否包含/servlet/,如果包含则处理;
- RequestDispatcher rdsp = request.getRequestDispatcher(target);
- rdsp.forward(req, resp);
- } else {
- chain.doFilter(req, resp);
- }
-
- // 第二种方式 (二选 一)
- /**
- if (this.includes.contains(target)) {
- // target取出的值则直接是jqueryAjax,在web.xml中配置即可。
- // if判断请求uri最后的那部分是否包含在配置文件中,如果包含,则处理
- RequestDispatcher rdsp = request.getRequestDispatcher(target);
- 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(",")));
- }
- }</pre><span style="background-color:rgb(245,245,245); font-size:14px"><br><br></span><p style="background-color:rgb(245,245,245); font-size:14px"></p><p style="background-color:rgb(245,245,245); color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><span style="font-size:24px">2.修改web.xml,类似如下格式:</span></p><p style="background-color:rgb(245,245,245); font-size:13px; color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"></p><pre code_snippet_id="169749" snippet_file_name="blog_20140124_11_2852296" name="code" class="html" style="background-color: rgb(245, 245, 245); font-size: 14px;"><?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <filter>
- <filter-name>redisp</filter-name>
- <filter-class>com.clzhang.sample.struts2.FilterServlet</filter-class>
- <!--如何使用第二种方式过滤,则需要下面的代码
- <init-param>
- <param-name>includeServlets</param-name>
- <param-value>jqueryAjax,jsonView</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>jqueryAjaxServlet</servlet-name>
- <servlet-class>com.clzhang.sample.struts2.servlet.jQueryAjaxServlet</servlet-class>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>jqueryAjaxServlet</servlet-name>
- <url-pattern>/servlet/jqueryAjax</url-pattern>
- </servlet-mapping>
-
- <servlet>
- <servlet-name>jsonViewServlet</servlet-name>
- <servlet-class>com.clzhang.sample.struts2.servlet.JsonViewServlet</servlet-class>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>jsonViewServlet</servlet-name>
- <url-pattern>/servlet/jsonView</url-pattern>
- </servlet-mapping>
-
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- </web-app></pre><span style="background-color:rgb(245,245,245); font-size:14px"><br><br></span><p style="background-color:rgb(245,245,245); font-size:14px"></p><p style="color:rgb(51,51,51); margin:10px auto; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; line-height:19px"><span style="font-size:18px; background-color:rgb(255,0,0)">注意<span style="color:rgb(0,0,255)">,<filter-name>redisp</filter-name></span>必须放置于<span style="color:rgb(0,0,255)"><filter-name>struts2</filter-name></span>之前,这样才能够保证它们能够按照正常的顺序处理,否则会出错。</span></p><span style="background-color:rgb(245,245,245); font-size:14px"><br></span></div>
- <span style="color: rgb(73, 73, 73); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(226, 226, 226);"><span style="word-wrap: normal; word-break: normal; line-height: 28px; font-size: 14pt; font-family: 宋体; color: red;"> </span></span>