RequestMethod POST and GET in the same controller?

First of all, here is my Controller :

 @RequestMapping(value = "/esta", method = RequestMethod.POST) public String handleRequest(HttpServletRequest request) { Esta estaobject = new Esta(); // To test, if the parameters are set String user = request.getParameter("user"); String name = request.getParameter("name"); String shortname = request.getParameter("shortname"); String was_admin_string = request.getParameter("was_admin"); String sap_nr = request.getParameter("sap_nr"); String etl_string = request.getParameter("etl"); if (user != null && name != null && shortname != null && was_admin_string != null && sap_nr != null && etl_string != null) { some code... } request.getSession().setAttribute("esta", estaobject); return "esta"; } 

When I visit a site, it checks with if -statement if there are some parameters.
If not, then it should just display my form. Then, when I fill out the form, it submits it using POST , and now there are some options, and it goes through if -statement.

My problem: when I first visit the site, this is not a POST -request, so I get the error message Request method 'GET' not supported .
But changing the form to GET -request is not an option for me. This should be a POST .

So, is there a solution for processing the same controller in POST and GET requests?

+8
java spring model-view-controller request
source share
3 answers

Make an array of the values โ€‹โ€‹of the methods to which it is attached, for example:

 @RequestMapping(value = "/esta", method = {RequestMethod.POST, RequestMethod.GET}) 
+19
source share

Or you can write separate methods

 @RequestMapping(value = {#some_vale}, method = RequestMethod.GET) public random_method #1{ } @RequestMapping(value = { #some_value }, method = RequestMethod.POST) public random_method #2{ } 

Now you can realize your visit to a specific page, and another - fill out the form. Hope this helps you.

+2
source share

In spring, a developer can use both RequestMethod.POST and RequestMethod.GET on one controller, simply by creating an array of such methods:

 @RequestMapping(value = "/esta", method = {RequestMethod.POST, RequestMethod.GET}) public String handleRequest(HttpServletRequest request) { //Implementation of your code. } 
0
source share

All Articles