I'm making a Spring MVC web app. The problem is that on single method is called twice and I don't know why.
@RequestMapping(value="/profile/{id}", method = RequestMethod.GET)
public String displayUserProfile( @PathVariable String id) {
System.out.println("asdasddsasd");
return "account/userProfile";
}
I commented many line from this method, but is still not working. Also tried to return other view..no good luck.
In console(ulr requests are written):
/demo/account/profile/f91b3a38-6921-41e0-98b7-58dff5cb1152
asdasddsasd
/demo/account/profile/0
asdasddsasd
After the second call of tihs method, it's going to my view
Any other method work fine. Does anyone know what's the problem here?
*I also read similar question from here..nothing helped
LE: what I also said in the comments. What is funny is that, if I set o model to the view, on the second call of the method, my view get's the model from the first call. (on the second call, with id 0, the model is null)
In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation.
Spring controllers are singletons (there is just one instance of each controller per web application) just like servlets.
2 Answers. Show activity on this post. @Controller @RequestMapping("user") public class UserController { @RequestMapping("edit") public ModelAndView edit(@RequestParam(value = "id", required = false) Long id, Map<String, Object> model) { ... } }
I have also observed one GET request causing the controller method to execute twice. The problem occurred when requesting the service using a Chrome browser (the problem did not occur when using Postman). In my case the culprit was the JSONView Chrome extension.
I determined the cause by using the Network tab of the Chrome developer tools. It showed my GET service being requested two times. The second request was initiated by content.js, which is a JavaScript file bundled with JSONView.
After I disabled the JSONView extension, a GET request through Chrome would cause the controller method to execute only once.
I experienced this called-twice phenomenon because BrowserSync replayed HTTP requests in each open BrowserSync browser window.
I finally got some time to find a solution here. Tried many things, but it didn't worked.
I replaced @PathVariable with @RequestParam and the URL is not accessed twice :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With