1

I´ve read a lot but I did not find any good answers regarding to my problem. Indeed, this my class :

@Controller
public class ServletController {

    @RequestMapping(value = {"", "/testControlP"}, method = RequestMethod.POST)
    @ResponseBody
    public void test(@RequestParam("visitor_name") String name,
            @RequestParam("visitor_email") String email, @RequestParam("visitor_phone") String phone,
            @RequestParam("total_adults") String total_adults, @RequestParam("total_children") String total_children,
            @RequestParam("checkin") String checkin, @RequestParam("checkout") String checkout,
            @RequestParam("visitor_message") String visitor_message) {
        
        System.out.println("Name : " + name);
        System.out.println("Email : " + email);
        System.out.println("Phone : " + phone);
        System.out.println("Total_adults : " + total_adults);
        System.out.println("Total_children : " + total_children);
        System.out.println("Checkin : " + checkin);
        System.out.println("Checkout : " + checkout);
        System.out.println("Visitor_message : " + visitor_message);
    }
}

After launching the application, I´ve got this error : La méthode HTTP POST n''est pas supportée par cette URL ( HTTP POST method is not supported by this URL).

This is a part of my jsp page :

<form method="post">
<button type="submit" name="camper" formaction="testControlP">Test ServletSpring</button>
<hr>

Could you please help me ?

** I´ve already tried with no params and I got the same errors. (Server WildFly 21)

Nick DeFazio
  • 2,412
  • 27
  • 31
RaphaelB
  • 21
  • 1
  • What URL is being submitted when you click the button? – Nick DeFazio Jan 03 '22 at 15:26
  • This is http://localhost:8080/HotelCancun/testControlP – RaphaelB Jan 03 '22 at 15:32
  • Your controller is weird, why `@ResponseBody` on a `void` method? That doesn't make sense, nor are you submitting anything in your form leading to errors for the `@RequestParam`. Finally your form should post to `/testControlP` not `testControlP`. – M. Deinum Jan 03 '22 at 15:32
  • You´re right. I tried other ways. I couldn´t be able to find a solution. that´s why I am posting. With "@ResponseBody" or without that does not work. I tried with or without "@RequestParam". I tried with "@RestController" as well. I tried with "@PostMapping" too. And so on, I have tried too many ways. I don´t know anymore. – RaphaelB Jan 03 '22 at 15:46

1 Answers1

0

We can't give requests to "/displayForm" URL directly from the browser because by default browser supports only GET mode requests. For this, we can use the form. The index.html is placed in the resources folder.

<h1>Form</h1>
<form action="/testControlP" method="post">
    <!-- Input fields -->
    <input type="submit" value="Submit">
</form>

By default @RequestParam contains required = true, so if the value is not passed in that case it will throw an exception. To resolve this we can use required = false.

@Controller
public class ServletController {

    @GetMapping("/displayForm")
    public String displayForm(){
        return "form";
    }

    @PostMapping(value = {"", "/testControlP"})
    public String test(@RequestParam(value = "visitor_name", required = false) String name,
                     @RequestParam(value = "visitor_email", required = false) String email,
                     @RequestParam(value = "visitor_phone", required = false) String phone,
                     @RequestParam(value = "total_adults", required = false) String totalAdults,
                     @RequestParam(value = "total_children", required = false) String totalChildren,
                     @RequestParam(value = "checkin", required = false) String checkin,
                     @RequestParam(value = "checkout", required = false) String checkout,
                     @RequestParam(value = "visitor_message", required = false) String visitorMessage) {
        System.out.println("Name : " + name);
        System.out.println("Email : " + email);
        System.out.println("Phone : " + phone);
        System.out.println("Total_adults : " + totalAdults);
        System.out.println("Total_children : " + totalChildren);
        System.out.println("Checkin : " + checkin);
        System.out.println("Checkout : " + checkout);
        System.out.println("Visitor_message : " + visitorMessage);
        return "redirect:/displayForm";
    }
}