dante
dante

Reputation: 403

Spring MVC - request method "POST" not supported

I got problem with my web application, when I click "add to cart" button in for example page called Phone1.jsp, then error displays:

WARNING: Request method 'POST' not supported

What's wrong with this code? I want in order to web app redirect me to /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>

Upvotes: 2

Views: 1084

Answers (1)

Abdelhak
Abdelhak

Reputation: 8387

Try to change this:

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

With this you should write /phones:

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

The reason for this is that you are setting a base path with @RequestMapping("/phones") on class level and all the other RequestMappings just add to this.

Upvotes: 4

Related Questions