Spring MVC - 请求方法" POST"不支持

时间:2016-01-05 13:35:35

标签: java spring jsp spring-mvc

我的网络应用程序出现问题,当我点击"添加到购物车"例如,在名为Phone1.jsp的页面中按钮,然后显示错误:

WARNING: Request method 'POST' not supported

这段代码有什么问题? 我想要网页应用程序将我重定向到/cart.html?(selecteditem)

PhoneController:

@Controller
@RequestMapping("/phones")
public class PhoneController {

@RequestMapping(value="/phone1.html", method = RequestMethod.GET)
public ModelAndView phone1Page(Model model) {

    ModelAndView phone1 = new ModelAndView("Phone1");
    return phone1;  
}

@RequestMapping(value="/phone2.html", method = RequestMethod.GET)
public ModelAndView phone2Page(Model model) {

    ModelAndView phone2 = new ModelAndView("Phone2");   
    return phone2; 

}



@RequestMapping(value="/cart.html", method = RequestMethod.POST)
public ModelAndView addToCart(@RequestParam String selectedPhone, Model model) throws ClassNotFoundException, SQLException{


    if ("Phone1".equals(selectedPhone))
    {
        something
    }

    else if ("Phone2".equals(selectedPhone))
    {   
        something
    }

    ModelAndView cart = new ModelAndView("Cart");
    return cart;

}

Phone1.jsp:

<form action="/OnlineShop/cart.html?selectedPhone=Phone1" method="post">
<div style="padding-right: 40px">
    <table border="1">
        <tr>
            <td>Name</td>
            <td>${name}</td>
        </tr>
        <tr>
            <td>Company</td>
            <td>${company}</td>
        </tr>
        <tr>
            <td>Type</td>
            <td>${type}</td>
        </tr>
        <tr>
            <td>Price</td>
            <td>${price}</td>
        </tr>
    </table>
    <p>

Phones.jsp:

<div align="center">
    <a href="http://localhost:8080/OnlineShop/phones/phone1.html"><img
        src="C:\JAVAEE_PROJECTS\workspace\OnlineShop\src\com\damian\resources\iphone.png"></a>
    <a href="http://localhost:8080/OnlineShop/phones/phone2.html"><img
        src="C:\JAVAEE_PROJECTS\workspace\OnlineShop\src\com\damian\resources\nokialumia.png"></a>
</div>

1 个答案:

答案 0 :(得分:4)

尝试改变这一点:

<form action="/OnlineShop/cart.html?selectedPhone=Phone1" method="post">

有了这个,你应该写/phones

 <form action="/phones/cart.html?selectedPhone=Phone1" method="post">

原因是您在课程级别设置了@RequestMapping("/phones")的基本路径,而其他所有RequestMapping只是添加到此。