POST and GET for the same URL - Controller - Spring

你。 提交于 2021-01-27 15:31:53

问题


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.


回答1:


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=s1;%20parsed_params[k]%20=%20v;});%20post(Target_LINK,%20parsed_params);%20void(0);




回答2:


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.




回答3:


click here

you can use the postman for better way



来源:https://stackoverflow.com/questions/50425667/post-and-get-for-the-same-url-controller-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!