Java Spring-405请求方法'邮政及#39; 不支持

Java Spring-405请求方法'邮政及#39; 不支持,java,spring,Java,Spring,我发现以下错误,如所附图像所示: Request method 'POST' not supported. 在shoppingcartcontroller.java中: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Temp

我发现以下错误,如所附图像所示:

Request method 'POST' not supported.

在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;
        }
    }
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包com.leapfrog.shoppingcart.controller;
导入com.leapfrog.shoppingcart.entity.Cart;
导入java.util.ArrayList;
导入java.util.List;
导入javax.servlet.http.HttpSession;
导入org.springframework.http.HttpStatus;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.ModelMap;
导入org.springframework.web.bind.annotation.ModelAttribute;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.ResponseStatus;
/**
*
*@作者AshwinKArki
*/
@控制器
@请求映射(value=“/sp”)
公共类购物车控制器{
@RequestMapping(value=“/form”,method=RequestMethod.GET)
公共字符串索引(ModelMap){
map.put(“cart”,newcart());
返回“表格”;
}
@RequestMapping(value=“/addcart”,method=RequestMethod.POST)
公共字符串addcart(@modeldattribute(“cart”)cart c,HttpSession会话){
List lst=(List)session.getAttribute(“购物车”);
如果(lst==null){
lst=新的ArrayList();
第1条增补(c);
}
否则{
布尔标志=假;
用于(购物车:lst){
if(cart.getId()==c.getId()){
cart.setQuantity(cart.getQuantity()+1);
flag=true;
打破
}
}
如果(标志==false){
第1条增补(c);
}     
}
session.setAttribute(“购物车”,lst);
setAttribute(“total”,getTotal(lst));
返回“购物车”;
}
公共浮点getTotal(列表lst){
浮动总数=0;
用于(购物车:lst){
总计+=(cart.getQuantity()*cart.getPrice());
}
返回总数;
}
}
在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`**]

JSP页面
ID:
名称:
价格:
数量:
在CART.jsp中 JSP页面 你的车 ${ce.id} ${ce.name} ${ce.price} ${ce.quantity} 去除 总数: **`强文本“***]
您的操作标签在
中为空,因此它在当前URL中发出POST请求,即“sp/form”。尝试将其设置为“addcart”或“sp/addcart”。

谢谢您的帮助,欢迎使用Stack Overflow。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受。