Redirect to an external URL from controller action in Spring MVC
To redirect to an external URL from a controller action in Spring MVC, you can use the RedirectView
class and return it from the controller action.
Here is an example of how to do this:
@Controller
public class MyController {
@RequestMapping("/redirect")
public RedirectView redirectToUrl() {
RedirectView redirectView = new RedirectView();
redirectView.setUrl("http://www.example.com");
return redirectView;
}
}
This example defines a controller action that redirects to the URL "http://www.example.com".
You can also use the "redirect:"
prefix in the @RequestMapping
annotation to redirect to an external URL. For example:
@Controller
public class MyController {
@RequestMapping("/redirect")
public String redirectToUrl() {
return "redirect:http://www.example.com";
}
}
I hope this helps. Let me know if you have any questions.