RequestMethod.DELETE相关,如何用jquery实现RequestMethod.DELETE请求 SpringMVC DELETE,PUT请求报错 添加支持Http的DELETE、PUT请求?

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

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

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

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

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

javascript代码

function DeleteFatherModule(moduleId, title)
{
    //alert(moduleId + "" + title);
    if (!confirm("确认删除"" + title + ""吗?"))
    {
        return false;
    }
    //fatherModule_
    $("#fatherModule_"+moduleId).html("<td colspan='3'><span >删除操作中...</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 >删除成功!</span></td>");
            }
            else
            {
                $("#fatherModule_"+moduleId).html("<td colspan='3'><span >删除失败!</span></td>");
            }
        },
        error : function()
        {
            $("#fatherModule_"+moduleId).html("<td colspan='3'><span >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  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- jdbcTemplate -->
    <bean  class="org.springframework.jdbc.core.JdbcTemplate" >
         <property name="dataSource" ref="dataSource"/>  
    </bean>
    
    <!-- 读取配置文件信息,在Spring的配置文件中使用EL表达式填充值 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置数据库连接池 -->
    <bean  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 >
        制定连接数据库的驱动
        <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 >
        <property name="basename" value="/WEB-INF/resource/messages" />
    </bean> -->
    <!-- org.springframework.context.support.ConversionServiceFactoryBean.class -->
    <!-- 使用转换器的bean -->
<!--     <bean >
        <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 >
        <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 >
        <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"  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>