Spring MVC添加支持Http的delete、put请求!(HiddenHttpMethodFilter)

Spring3.0之后->Spring MVC过滤器-HiddenHttpMethodFilter

SpringMVC DELETE,PUT请求报错 添加支持Http的DELETE、PUT请求

昨天看上面的博客,然后自己实践了一下,还是不行,今天早上来就用我的办法了猜

不知道为什么,虽然现在实现了但是还不知道为什么

jauery好像也支持这样写了吗  type : 'DELETE',

javascript代码

function DeleteFatherModule(moduleId, title)
{
//alert(moduleId + "" + title);
if (!confirm("确认删除"" + title + ""吗?"))
{
return false;
}
//fatherModule_
$("#fatherModule_"+moduleId).html("<td colspan='3'><span style='color:red'>删除操作中...</span></td>");
$.ajax(
{
type : 'DELETE',
dataType : 'json',
contentType : 'application/json;charset=utf-8',
url : "/sfk_BBS02/rest/fatherModule/" + moduleId,
// data:{_method:'delete'},
success : function(data)
{
// data in (true,false)
if (data)
{
$("#fatherModule_"+moduleId).html("<td colspan='3'><span style='color:red'>删除成功!</span></td>");
}
else
{
$("#fatherModule_"+moduleId).html("<td colspan='3'><span style='color:red'>删除失败!</span></td>");
}
},
error : function()
{
$("#fatherModule_"+moduleId).html("<td colspan='3'><span style='color:red'>Error!</span></td>");
}
});

后台代码

package sfk.bbs.admin.rest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import sfk.bbs.admin.action.AdminIndexAction;
import sfk.bbs.admin.service.AdminIndexService; @RestController
@RequestMapping("/rest")
public class AdminIndexRest
{
private static Logger log = Logger.getLogger(AdminIndexAction.class);
@Autowired
private AdminIndexService adminIndexService; @RequestMapping(value="/fatherModule/{id}", method=RequestMethod.DELETE)
public ResponseEntity<Boolean> delete(@PathVariable("id") Long id)
{
System.out.println("success");
return new ResponseEntity<Boolean>(adminIndexService.deleteFatherModule(id),HttpStatus.OK);
} }

配置文件 applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描的包 -->
<!-- <context:component-scan base-package="app04"/> -->
<!-- 使用转换器 -->
<!--<mvc:annotation-driven conversion-service="conversionService"/>-->
<!-- 使用格式化 -->
<!-- conversionService2 -->
<!-- <mvc:annotation-driven conversion-service="conversionService2"/> --> <!-- 使用注册器替代格式化 -->
<!-- <mvc:annotation-driven />意思是开启使用注释的功能-->
<!-- conversionService3是格式化或者转换器 -->
<!-- <mvc:annotation-driven conversion-service="conversionService3"/> --> <!-- 扫描的包 -->
<context:component-scan base-package="sfk.bbs"/> <aop:aspectj-autoproxy />
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" /> <!-- 这个的作用是让DispatcherServlet不将下列路径理解为一个request请求,
在项目中,这个是必须的,如果没有加这些就可能造成上述问题 -->
<mvc:annotation-driven />
<!-- <mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/> -->
<mvc:resources mapping="/style/**" location="/style/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/*.html" location="/"/> <!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 读取配置文件信息,在Spring的配置文件中使用EL表达式填充值 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据库连接池 -->
<bean id="dataSourceLocal" name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 指定连接数据库的驱动-->
<property name="driverClass" value="${jdbc.driverClassName}"/>
<!-- 指定连接数据库的URL-->
<property name="jdbcUrl" value="${jdbc.url}"/>
<!-- 指定连接数据库的用户名-->
<property name="user" value="${jdbc.username}"/>
<!-- 指定连接数据库的密码-->
<property name="password" value="${jdbc.password}"/>
<!-- 指定连接池中保留的最大连接数. Default:15-->
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
<!-- 指定连接池中保留的最小连接数-->
<property name="minPoolSize" value="${jdbc.minPoolSize}"/>
<!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3-->
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0-->
<property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3-->
<!-- <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/> -->
<!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。
但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0-->
<!-- <property name="maxStatements" value="${jdbc.maxStatements}"/> -->
<!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->
<!-- <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"/> -->
</bean> <!-- <bean id="dataSourceLocal" name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
制定连接数据库的驱动
<property name="driverClass" value="${jdbc.driverClassName}" />
制定连接数据库的URL
<property name="jdbcUrl" value="${jdbc.url}" />
指定连接数据库的用户名
<property name="user" value="${jdbc.username}" />
指定数据库的密码
<property name="password" value="${jdbc.password}"/>
指定连接池中保留的最大连接数 default:15
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
指定连接池中保留的最小连接数
<property name="minPoolSize" value="${jdbc.minPoolSize}" />
指定连接池的初始化连接数 取值应在minPoolSize 与MaxPoolSize之间,Default:3
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
最大空闲时间,60秒内未使用连接被丢弃,若为0则永不丢弃,Default:0
<property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>
当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. default: 3
<property name="acquireIncrement" value="${jdbc.acquireIncrement}" />
JDBC的标准,用以控制数据源内加载的prepareedStatement数量.但是由于预缓存的statements
属于单个connection而不是整个连接池所以色绘制这个参数需要考虑到多方面的因素,如果maxStatements
与maxStatementsPerConnection均为0,则缓存被关闭,Default:0
<property name="maxStatements" value="${jdbc.maxStatements}"/>
每60秒检查所有连接池中的空闲连接default:0
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" />
</bean> -->
<!-- 错误提示信息配置,用配置文件管理错误信息 -->
<!-- <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/resource/messages" />
</bean> -->
<!-- org.springframework.context.support.ConversionServiceFactoryBean.class -->
<!-- 使用转换器的bean -->
<!-- <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="app06a.converter.StringToDateConverter" >
<constructor-arg type="java.lang.String" value="MM-dd-yyyy"/>
</bean>
</list>
</property>
</bean>
-->
<!-- 使用Formatter的格式化 -->
<!-- <bean id="conversionService2" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set> /springMVC/src/app06a/formatter/DateFormatter.java
<bean class="app06a.formatter.DateFormatter">
<constructor-arg type="java.lang.String" value="MM-dd-yyyy"/>
</bean>
</set>
</property>
</bean> --> <!-- 使用注册器替代Formatter的格式化 -->
<!-- <bean id="conversionService3" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatterRegistrars">
<set>
/springMVC/src/app06a/formatter/DateFormatter.java
<bean class="app06a.formatter.MyFormatterRigistrar">
<constructor-arg type="java.lang.String" value="MM-dd-yyyy"/>
</bean>
</set>
</property>
</bean> --> </beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>sfk_BBS02</display-name> <!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> -->
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/resource/applicationContext.xml</param-value>
</context-param> --> <!-- config log4j first Part -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- /WEB-INF/classes/applicationContext-*.xml --> <!-- Srping监听器 -->
<!-- <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> --> <!-- RequestMethod.delete RequestMethod.put -->
<!-- HiddenHttpMethodFilter必须作用于dispatcher前,配置这个没有起作用就不配了 -->
<!-- <filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<servlet-name>springMVC</servlet-name>
</filter-mapping> --> <!-- config log4j second Part -->
<!-- 加载log4j配置文件 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup>
</servlet>
<!-- 有的项目中这里写的是.do,这样分发器只会分发带有.do的请求,
就可以规避将js.css,.png等文件的路径当作一个请求,当前没有写.do,就要用到
<mvc:annotation-driven />
<mvc:resources mapping="/css/**" location="/css/"/>
将.css等文件不当作一个请求 -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> </web-app>

RequestMethod.DELETE相关,如何用jquery实现RequestMethod.DELETE请求的更多相关文章

  1. 如何用jquery实现实时监控浏览器宽度

    如何用jquery实现实时监控浏览器宽度 2013-06-05 14:36匿名 | 浏览 3121 次 $(window).width();这代码只能获取浏览器刷新时的那一刻的宽度,如何才能达到实时获 ...

  2. 如何用jQuery获得select的值

    如何用jQuery获得select的值,在网上找了看了一下,下面将总结一下: 1.获取第一个option的值        $('#test option:first').val(); 2.最后一个o ...

  3. 如何用jquery获取input输入框中的值?

    如何用jquery获取<input id="test" name="test" type="text"/>中输入的值? $(&q ...

  4. jquery 拦截 post 等请求实现aop效果

    $(function(){ jQuery.extend({ put: function( url, data, callback, type ) { // shift arguments if dat ...

  5. jquery的ajax异步请求接收返回json数据

    http://www.jb51.net/article/51122.htm jquery的ajax异步请求接收返回json数据方法设置简单,一个是服务器处理程序是返回json数据,另一种就是ajax发 ...

  6. jquery ajax跨域请求详解

    本文章来给大家详细jquery中的ajax跨域请求, 在JQuery对于Ajax的跨域请求有两类解决方案,不过都是只支持get方式.分别是JQuery的jquery.ajax jsonp格式和jque ...

  7. jQuery Ajax: $.post请求示例

    jQuery Ajax: $.post请求示例 leyangjun.html页面 <html> <head> <meta http-equiv="Content ...

  8. 关于JQuery中的ajax请求或者post请求的回调方法中的操作执行或者变量修改没反映的问题

    前段时间做一个项目,而项目中所有的请求都要用jquery 中的ajax请求或者post请求,但是开始处理一些简单操作还好,但是自己写了一些验证就出现问题了,比如表单提交的时候,要验证帐号的唯一性,所以 ...

  9. jquery版 发同步请求 自定义头部信息 公共请求体

    //jquery版 发同步请求 function getData(url,param,fn){ var Authorization=localStorage.getItem("Authori ...

随机推荐

  1. 定制你的Unity编辑器

    Unity的编辑器可以通过写脚本进行界面定制,包括添加功能菜单,今天写游戏Demo用到了记录一下. 为Unity添加子菜单 示例程序 [AddComponentMenu("Defend Ho ...

  2. java 12-1 StringBuffer类

    线程安全(多线程讲解) 安全 -- 同步 -- 数据是安全的--效率低一些 不安全 -- 不同步 -- 数据不安全--效率高一些 安全和效率问题是永远困扰我们的问题. 安全:医院的网站,银行网站 效率 ...

  3. 安装grunt

    1,npm install -g grunt-cli 2,npm install grunt --save 作为项目的依赖安装 这两步必不可少

  4. UINavagationController

    如何防止navigation多次push一个页面?有时候网慢,点了一下没反应,用户可能就多点几下,这时候会打开好几个一样的页面: 写了一个navigation基类,重写了push方法:传进来要push ...

  5. 05SpringMvc_映射器SimpleUrlHanderMapping

    这篇文章讲的还是映射器,映射器类有两种,前一篇文章讲的是BeanNameUrlHanderMapping映射器类.今天讲的是SimpleUrlHanderMapping映射器类. 这两个映射器类有什么 ...

  6. sqlSQL2008如何创建定时作业(代理服务)(转)

    SQL2008如何创建定时作业?此方法也适应于Sql Server2005数据库,有兴趣的可以来看下! 1.打开[SQL Server Management Studio],在[对象资源管理器]列表中 ...

  7. 泛型类型的协变(covariant)和逆变

    官网:http://msdn.microsoft.com/zh-cn/library/dd799517.aspx 原文链接:http://book.51cto.com/art/201112/30857 ...

  8. 入门Linux

    45分钟带你入门Linux(附:笔者在工作室开讨论班录制的视频讲解)   第一部分    熟悉Linux基本操作 一.初识Linux 1.Linux特点 ◊  开放性 ◊  多用户 ◊  多任务 ◊  ...

  9. SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程'sys.sp_OACreate' 的访问

    --开启 Ole Automation Procedures sp_configure ; GO RECONFIGURE; GO sp_configure ; GO RECONFIGURE; GO E ...

  10. [转]Windows网络编程学习-面向连接的编程方式

    直接附上原文链接:windows 网络编程学习-面向连接的编程方式