Spring -405不支持请求方法'POST'

时间:2017-01-23 07:13:46

标签: java spring

我收到以下错误,如附图所示:

Request method 'POST' not supported.

https://i.stack.imgur.com/SMRph.png

在shoppingcartcontroller.java中:

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.leapfrog.shoppingcart.controller;

    import com.leapfrog.shoppingcart.entity.Cart;
    import java.util.ArrayList;
    import java.util.List;
    import javax.servlet.http.HttpSession;
    import org.springframework.http.HttpStatus;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseStatus;

    /**
     *
     * @author AshwinKArki
     */
    @Controller
    @RequestMapping(value="/sp")
    public class ShoppingCartController {

        @RequestMapping(value="/form",method=RequestMethod.GET)
        public String index(ModelMap map){
           map.put("cart",new Cart());
            return "form";
        }

         @RequestMapping(value="/addcart",method=RequestMethod.POST)
         public String addcart(@ModelAttribute("cart") Cart c,HttpSession session){

            List<Cart> lst=(List<Cart>)session.getAttribute("cart");
            if(lst==null){
                lst=new ArrayList<>();
                lst.add(c);

            }

            else{
                boolean flag=false;
                for(Cart cart:lst){
                    if(cart.getId()==c.getId()){
                        cart.setQuantity(cart.getQuantity()+1);
                        flag=true;
                        break;
                    }
                }
                if(flag==false)  {
                    lst.add(c);
                }     
            }
            session.setAttribute("cart", lst);
            session.setAttribute("total",getTotal(lst));
            return "cart";
        }
        public float getTotal(List<Cart> lst){
            float total=0;
            for(Cart cart:lst){
                total+=(cart.getQuantity()*cart.getPrice());
            }
            return total;
        }
    }

在form.jsp中:

    <%-- 
        Document   : form
        Created on : 23-Jan-2017, 12:16:53
        Author     : AshwinKArki
    --%>

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://www.springframework.org/tags/form" %>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <c:form modelAttribute="cart" action="" method="POST">
                ID:<c:input path="id" /> <br/>
               Name:<c:input path="name" /> <br/>
                Price:<c:input path="price" /> <br/>
               Quantity:<c:input path="quantity" /> <br/>
               <input type="submit" value="Add to CART" />

            </c:form>
        </body>
    </html>

    In CART.jsp

    <%-- 
        Document   : cart
        Created on : 23-Jan-2017, 12:35:07
        Author     : AshwinKArki
    --%>

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1>Your cart</h1>
            <table border="1px">
                <c:forEach  var="ce" items="<%= request.getSession().getAttribute("cart")%>" >
                    <tr>
                        <td>${ce.id}</td>
                        <td>${ce.name}</td>
                        <td>${ce.price}</td>
                        <td>${ce.quantity}</td>
                        <td>
                            Remove
                        </td>
                    </tr>
                    <tr>
                        <td>
                        TOTAL:    <%= request.getSession().getAttribute("total") %>
                        </td>
                    </tr>
                </c:forEach>
                </table>
        </body>
    </html>
    **`strong text`**]

1 个答案:

答案 0 :(得分:0)

您的操作标记在<c:form>中为空,因此它在当前网址中发出POST请求,即“sp / form”。尝试将其设置为“addcart”或“sp / addcart”。