How can i redirect to different url eg: yahoo.com,hotmail.com from my controller i am using Spring 3.0 and using config and not annotations.
one thing i forgot to mention is the url will be user input so cannot add it in the config
Thanks Gauls
We can use a name such as a redirect: http://localhost:8080/spring-redirect-and-forward/redirectedUrl if we need to redirect to an absolute URL.
The sendRedirect() method of HttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file. It accepts relative as well as absolute URL. It works at client side because it uses the url bar of the browser to make another request.
@RestController public class RedirectController { @RequestMapping("/redirect") public String redirect() { return "redirect:/other/controller/"; } } and if we will try to access that url curl localhost:8080/redirect we will simply see redirect:/other/controller/ string as result.
public class RedirectView extends AbstractUrlBasedView implements SmartView. View that redirects to an absolute, context relative, or current request relative URL. The URL may be a URI template in which case the URI template variables will be replaced with values available in the model.
You can use redirect: prefix in the controller method.
public String method(){
return "redirect:http://yahoo.com"
}
More info in the section 13.5.3.2 of the spring documentation
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