Redirecting to an external URL in view file

I have the following string in a Spring MVC Controller action. I wanted the controller action to render a view page that takes this following string and then does the redirection.

String redirectUrl = "http://www.yahoo.com"

My controller action looks like the following:

@RequestMapping(method = RequestMethod.GET)
    public String showForm(HttpServletRequest request, ModelMap model) 
    {
          String redirectUrl = "http://www.yahoo.com";
          model.addAttribute("redirectUrl", redirectUrl);
          return "loginSuccess"; //this is my view JSP file
    }

Is there a way in JSP view to do this redirect without using JSTL? I want a clean redirect and not send any query string parameters.

Thanks,

Answer

Maybe I misunderstood but if you want a redirect, you’ll have to use a RedirectView.

String redirectUrl = "http://www.yahoo.com";
return "redirect:" + redirectUrl;

Or use a RedirectView instance

@RequestMapping(method = RequestMethod.GET)
public View showForm(HttpServletRequest request, ModelMap model) 
{
      String redirectUrl = "http://www.yahoo.com";
      RedirectView view = new RedirectView(redirectUrl );
      return view;
}

Leave a Reply

Your email address will not be published. Required fields are marked *