u010359004
u010359004
2015-04-15 05:10

SpringMVC提交put,delete请求405

  • 请求

请求删除操作报:HTTP Status 405 - Request method 'POST' not supported
已经在web.xml配置,HiddenHttpMethodFilter过滤器
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


主页面


<table border="1">
    <tr>
        <th>编号</th>
        <th>用户名</th>
        <th>密码</th>
        <th>邮箱</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${requestScope.list}" var="list">
        <tr>
            <td>${list.userId}</td>
            <td>${list.userName}</td>
            <td>${list.password}</td>
            <td>${list.email}</td>
            <td>
                <a href="update" class="update">修改</a>
                <a href="user/${list.userId}" class="delete">刪除</a>
            </td>
        </tr>
    </c:forEach>
</table>
<br>
<br>


$(function(){ $(".delete").click(function(){ var href=$(this).attr("href"); $("form").attr("action",href).submit(); return false; }); });

SpringMVC-servlet.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
" default-autowire="byName">
<!-- 启用spring mvc 注解 -->

<!-- 处理静态资源 -->
<mvc:default-servlet-handler/>


<!--对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
<context:component-scan base-package="com.tang" />

<!-- 获取数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/springmvc"></property>
    <property name="username" value="root"></property>
    <property name="password" value="123">
    </property>
</bean>

<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>


<!-- 给jdbc模板注入数据源 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>


controller代码:
package com.tang.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tang.dao.UserDao;
import com.tang.pojo.User;
@Controller
public class LoginController {
@Autowired
private UserDao userDao;

public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
}
@ModelAttribute
public void test(){
    System.out.println("LoginController.test()");
}

@RequestMapping("/login")
public String doLogin(String username,String password,ModelMap map){
    User user=userDao.doLogin(username, password);
    if(user!=null){
        List<User> list=userDao.findAll();
        map.put("list", list);
        return "index";
    }else{
        return "login";
    }
}

@RequestMapping(value="/update",method=RequestMethod.PUT)
public String doUpdate(User user,ModelMap map){
    userDao.doUpdate(user);
    List<User> list=userDao.findAll();
    map.put("list", list);
    return "index";
}
@RequestMapping(value="/user/{id}",method=RequestMethod.DELETE)
public String doDelete(@PathVariable("id")Integer id,ModelMap map){
    userDao.doDelete(id);
    List<User> list=userDao.findAll();
    map.put("list", list);
    return "index";
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答