Redirect to an external URL from controller action in Spring MVC
You can do it with two ways.
First:
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public void method(HttpServletResponse httpServletResponse) {
httpServletResponse.setHeader("Location", projectUrl);
httpServletResponse.setStatus(302);
}
Second:
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public ModelAndView method() {
return new ModelAndView("redirect:" + projectUrl);
}
Redirect to an external URL in Spring MVC
The protocol is required if the host is different to that of the current host
String redirectUrl = "http://www.yahoo.com";
return "redirect:" + redirectUrl;
Have a look at the redirect: prefix
section from Spring Web MVC framework
A logical view name such as redirect:/myapp/some/resource will redirect relative to the current Servlet context, while a name such as redirect:http://myhost.com/some/arbitrary/path will redirect to an absolute URL.
Url redirection from a spring boot controller
You can do it in many ways
Using RedirectView
@RequestMapping("/to-be-redirected")
public RedirectView localRedirect() {
RedirectView redirectView = new RedirectView();
redirectView.setUrl("http://www.yahoo.com");
return redirectView;
}
Using ResponseEntity
@RequestMapping("/to-be-redirected")
public ResponseEntity<Object> redirectToExternalUrl() throws URISyntaxException {
URI yahoo = new URI("http://www.yahoo.com");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setLocation(yahoo);
return new ResponseEntity<>(httpHeaders, HttpStatus.SEE_OTHER);
}
Using HttpServletResponse
@RequestMapping(value = "/", method = RequestMethod.GET)
public void redirectToTwitter(HttpServletResponse httpServletResponse) throws IOException {
httpServletResponse.sendRedirect("https://twitter.com");
}
Using ModelAndView
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public ModelAndView method() {
return new ModelAndView("redirect:" + projectUrl);
}
Redirect to External URl in Spring MVC
Prefix your url string with the protocol, i.e. http://www.google.com
.
final String redirectUrl = "redirect:http://www.google.com";
return redirectUrl;
Redirect to External URl in Spring MVC by RequestMethod.POST
RedirectView class has created GET action and it can't POST request.
I put refId in ModelMap object and redirect to jsp.
map.put("refId", refId);
So when page loaded then create post request form submit.
$(document).ready(function () {
if (${not empty refId}) {
var refId = ('${refId}');
document.getElementById('bank-card-form').action = 'https://bpm.shaparak.ir/pgwchannel/startpay.mellat?RefId=' + refId;
document.getElementById('bank-card-form').submit();
}
});
And in my jsp added bank-card-form
<form method="post" id="bank-card-form"
style="display: none" autocomplete="off">
</form>
Spring, redirect to external url using POST
Like @stepanian said, you can't redirect with POST.
But there are few workarounds:
- Do a simple HttpUrlConnection and use POST. After output the response stream. It works, but I had some problem with CSS.
- Do stuff in your controller and after redirect the result data to a fake page. This page will do automatically the POST through javascript with no user interaction (more details):
html:
<form name="myRedirectForm" action="https://processthis.com/process" method="post">
<input name="name" type="hidden" value="xyz" />
<input name="phone" type="hidden" value="9898989898" />
<noscript>
<input type="submit" value="Click here to continue" />
</noscript>
</form>
<script type="text/javascript">
$(document).ready(function() {
document.myRedirectForm.submit();
});
</script>
Related Topics
How to Use a Delimiter with Scanner.Usedelimiter in Java
Java: How to Split an Arraylist in Multiple Small Arraylists
Dealing with "Xerces Hell" in Java/Maven
How to Obtain Method Parameter Name Using Java Reflection
Calculate Size of Object in Java
Error: Selection Does Not Contain a Main Type
How to Measure Distance and Create a Bounding Box Based on Two Latitude+Longitude Points in Java
Cannot Make a Static Reference to the Non-Static Field
Java: Check the Date Format of Current String Is According to Required Format or Not
Javafx Tableview Text Alignment
Remove All Occurrences of Char from String
Fastest Way to Write Huge Data in Text File Java
What's the Difference Between Map() and Flatmap() Methods in Java 8
How to Get Parameters from the Url with Jsp
How to Remove Duplicates from a List