I would like to redirect the user to "/" page.
In my controller, I have:
@RequestMapping(value = "/uploadImage", method = {RequestMethod.POST})
public ModelAndView addUser (@RequestParam(value="file") MultipartFile file,
HttpServletRequest request,
ModelMap model) {
...
if (...) {
model.addAttribute("uploadFileError", true);
return new ModelAndView("/", model);
}
return new ModelAndView("/", model);
and in my receiving page:
@RequestMapping(value = "/")
public String root(Model model, HttpServletRequest request) {
...
return "index";
}
but Spring returns "Error resolving template [/], template might not exist or might not be accessible by any of the configured Template Resolvers"
Please help. Thanks.
As per the explanation given it seems like you need to redirect to some other controller in the same server. Below code will suits you to manage this.
@RequestMapping(value = "/uploadImage", method = { RequestMethod.POST })
public ModelAndView addUser(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request,
ModelMap model) {
return new ModelAndView("redirect:/", model);
}
For further reading you can refer to below differences of redirect and forward
Forward:
The request will be further processed on the server side
The client isn’t impacted by forward, URL in a browser stays the same
Request and response objects will remain the same object after forwarding.
- Request-scope objects will be still available
Redirect:
- The request is redirected to a different resource
- The client will see the URL change after the redirect
- A new request is created
- Redirect is normally used within Post/Redirect/Get web development pattern
If you just want to test the redirection run below without MultipartFile @AlanBE
@RequestMapping(value = "/uploadImage", method = { RequestMethod.POST })
public ModelAndView addUser(/* @RequestParam(value = "file") MultipartFile file, */ HttpServletRequest request,
ModelMap model) {
return new ModelAndView("redirect:/", model);
}