3

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.

AlanBE
  • 123
  • 2
  • 11

5 Answers5

2

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:

  1. The request will be further processed on the server side

  2. The client isn’t impacted by forward, URL in a browser stays the same

  3. Request and response objects will remain the same object after forwarding.

  4. Request-scope objects will be still available

Redirect:

  1. The request is redirected to a different resource
  2. The client will see the URL change after the redirect
  3. A new request is created
  4. 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);
    }
Lahiru Wijesekara
  • 623
  • 1
  • 10
  • 22
0

Redirect as follows:

@RequestMapping(value = "/uploadImage", method = {RequestMethod.POST})
    public void addUser (@RequestParam(value="file") MultipartFile file,
            HttpServletRequest request,HttpServletResponse response
            ModelMap model) {
    ...
response.sendRedirect("/");
Phil
  • 157,677
  • 23
  • 242
  • 245
DEBENDRA DHINDA
  • 1,163
  • 5
  • 14
0

Try this.

@RequestMapping(value = "/uploadImage", method = {RequestMethod.POST})
    public ModelAndView addUser (@RequestParam(value="file") MultipartFile file,
            HttpServletRequest request,
            ModelMap model) {
    ...

    return "redirect:/ http://yoururl/";
Vipin CP
  • 3,642
  • 3
  • 33
  • 55
0

I suspect if you want to forward or redirect. Redirection is for external domains or servers while forwarding is within the same server to another resource for further processing.

There are 2 options for redirect:

 return new ModelAndView("redirect:/redirectedUrl", model);

Here, redirectedUrl is relative to the server context. In your case it seems to be "/".

But it can be a full/complete HTTP URL such as : http://localhost:8080/newDomain/s...

If forward is what you really intend, try:

 return new ModelAndView("redirect:/redirectedUrl", model);
TechFree
  • 2,600
  • 1
  • 17
  • 18
0

There are different ways to redirect here is simple to try this

@RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
public ModelAndView addUser (@RequestParam(value="file")MultipartFile file, HttpServletRequest request,
        ModelMap model) {
       ...
return new ModelAndView( "redirect:/index",model);

}

and receiving page like

  @RequestMapping(value = "/index", method=RequestMethod.GET)
public String root(Model model, HttpServletRequest request) {
...
return "index";
}
MangduYogii
  • 935
  • 10
  • 24