简体   繁体   中英

POST and GET for the same URL - Controller - Spring

I have this Controller :

@Controller
public class HelloWorldController {

    @RequestMapping("/hello.html")
    public ModelAndView helloWorld() {

        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message);
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String viewLogin(Map<String, Object> model) {
        User user = new User();
        model.put("userForm", user);
        return "LoginForm";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String doLogin(@Valid  @ModelAttribute("userForm")  User userForm,
            BindingResult result, Map<String, Object> model) {

        if (result.hasErrors()) {
            return "login";
        }

        return "LoginSuccess";
    }
}

I have 2 methods having different http methods for the same url /login so when clicking on the first url ' localhost:8080/project_name/login ' the first method with GET will be handled and will redirect me to the /LoginForm

So from my understanding the second method handler will not be executed as the request for /login is always with GET.

If my understanding is wrong please explain to me how can the second method will be exectued and thanks.

the request for /login is always with GET

I think this is where you are getting confused. It's GET because the browser makes a GET call by default. If you want to evoke the second method, you may have to write a custom form / JSP page or check out postman to set the HTTP method to POST.

You can also use curl from command line:

$ curl -d "param1=value1&param2=value2" -X POST http://localhost:3000/data

If you want to be able to make POST calls from your browser itself, checkout these browser add-ons : firefox , chrome ,

If you do not like extensions in your browser, create a bookmark with the following text and use it :

javascript:var%20my_params=prompt("Enter%20your%20parameters","var1=aaaa&var2=bbbbb");%20var%20Target_LINK=prompt("Enter%20destination",%20location.href);%20function%20post(path,%20params)%20{%20%20%20var%20xForm=%20document.createElement("form");%20%20%20xForm.setAttribute("method",%20"post");%20%20%20xForm.setAttribute("action",%20path);%20xForm.setAttribute("target",%20"_blank");%20%20%20for(var%20key%20in%20params)%20{%20%20%20if(params.hasOwnProperty(key))%20{%20%20%20%20%20%20%20%20var%20hiddenField%20=%20document.createElement("input");%20%20%20%20%20%20hiddenField.setAttribute("name",%20key);%20%20%20%20%20%20hiddenField.setAttribute("value",%20params[key]);%20%20%20%20%20%20%20%20%20xForm.appendChild(hiddenField);%20%20%20%20%20}%20%20%20}%20%20%20document.body.appendChild(xForm);%20%20xForm.submit();%20}%20%20%20parsed_params={};%20my_params.split("&").forEach(function(item)%20{var%20s%20=%20item.split("="),%20k=s[0],%20v=s 1 ;%20parsed_params[k]%20=%20v;});%20post(Target_LIN K,%20parsed_params);%20void(0);

A hit from the browser for the url localhost:8080/project_name/login will be a GET call; so your first /login GET method will be executed.

To have the second method executed, make a POST call from any Rest client like PostMan or ARC etc.

click here

you can use the postman for better way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM