如何让Spring MVC DispatchServlet拦截所有的.do请求,比如/system/*.do!

本文解答了如何配置SpringMVC的DispatcherServlet以拦截所有以.do结尾的URL请求,并提供了正确的<servlet-mapping>配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何让Spring MVC DispatchServlet拦截所有的.do请求,比如/system/*.do!

2011-09-22 09:27 wangxu198709  |  浏览 5317 次
我现在要使用SpringMVC来编写框架,但是我只是想拦截特定的.do请求,如果配置成/system/*.do会出错,配置如下
 <servlet>
  <servlet-name>basedata</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/spring/basedata-servlet.xml</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>basedata</servlet-name>
  <url-pattern>/basedata/*.do</url-pattern>
 </servlet-mapping>
请问要怎么处理?谢谢
2011-09-22 09:35 提问者采纳
<servlet-mapping>
  <servlet-name>basedata</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
提问者评价
没最佳答案,给最及时的答案吧
评论  |  4  1

★鲲背浮雪★ | 来自团队:Java软件 | 九级 采纳率23%

擅长: 诛仙 JAVA相关 百度产品 数学

为您推荐:

按默认排序 | 按时间排序

其他4条回答

2011-09-22 10:03 baungham  | 四级
<servlet-mapping>
<servlet-name>basedata</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
拦截*.do,例如:/system/add.do,弊端:所有的url都要以.do结尾。不会影响访问静态文件。
参考:http://blog.youkuaiyun.com/sunitjy/article/details/6782431
评论  |  1  0
2011-09-22 10:23 outman_linfeng  | 四级
<servlet-mapping>
<servlet-name>basedata</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> 
*前面的路径可以不要了。
评论  |  0  0
2011-10-05 22:43 匿名_热心网友  | 十六级
if(xmlhttp.responseText.trim()=="true")
你用responseText要注意 可能返回来的是一个页面代码 你可以alert(xmlhttp.responseText)看下返回来的是什么 估计是这个返回值有问题
评论  |  0  0
举报| 2011-09-22 10:05 623661034  | 二级
basedata/*.do把*号前的删了试一试
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 明确指定只扫描Controller --> <context:component-scan base-package="com.itheihei" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:resources mapping="/images/**" location="/images/"/> <mvc:resources mapping="/css/**" location="/css/"/> <mvc:annotation-driven/> </beans><?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 排除Controller,只扫描Service/Repository等 --> <context:component-scan base-package="com.itheihei"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>@Controller public class AccountController { @Autowired AccountService accountService; @RequestMapping("findAllAccounts") public String findAllAccounts() { System.out.println("方法成功执行"); return "list"; } } <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <a href="${pageContext.request.contextPath}/findAllAccounts">查看所有用户信息</a> </body> </html><?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> <!-- 将所有请求映射到 dispatcherServlet --> </servlet-mapping> </web-app>为什么会404
最新发布
05-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值