Spring3.0以後->Spring MVC過濾器-HiddenHttpMethodFilterjavascript
昨天看上面的博客,而後本身實踐了一下,仍是不行,今天早上來就用個人辦法了猜css
不知道爲何,雖然如今實現了可是還不知道爲何html
jauery好像也支持這樣寫了嗎 type : 'DELETE',
javascript代碼java
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>"); } });
後臺代碼web
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.xmlajax
<?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.xmlspring
<?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>