2017-01-23 74 views
0

我获得以下错误,如显示的附加的图像中:春-405请求方法' POST '不支持

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`**] 

回答

0

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

+0

因此,你有很多shakti ji –

+0

*乐于帮忙,欢迎来到Stack Overflow。如果此答案或任何其他人解决了您的问题,请将其标记为已接受。* –

相关问题