I'd like to find the absolute URL of the webapp in Spring, from the Controller. I'm aware of JSTL c:url, but I need this info from inside the Controller.
@Controller
public class AuthorizeController {
@Autowired
private Authorizer auth;
@RequestMapping("/auth")
public String sendToAuthorization() {
String baseUrl = "http://localhost:8080/tasks/";
return "redirect:" + auth.getAuthorizationUrl(baseUrl);
}
}
As you can see the baseUrl is hardcoded, and I could provide it to the Authorizer class via Spring configuration, but I am sure that it's possible to get this information from Spring within the Controller. I tried to google "spring mvc url" and could not find a way to solve this problem.
There's nothing in the Android URI class that gives you the base URL directly- As gnuf suggests, you'd have to construct it as the protocol + getHost(). The string parsing way might be easier and let you avoid stuffing everything in a try/catch block.
Run the Spring Boot Web application and open the URL http://localhost:8080/getBaseUrl on your browser to receive the base URL result as the following screenshot.
By default, Spring boot applications are accessed by context path “/” which is default for embedded servers i.e. we can access the application directly at http://localhost:PORT/ .
What Does Base URL Mean? In web development, design applications can define a base URL or base location, which helps in converting relative web URLs on the specific page to absolute web URLs. The HTML element <base> allows a base URL for use for all relative URLs within a specific document.
In this tutorial, we discuss 2 ways for retrieving the context path in a Spring Web application. The typical way of getting the context path is through the HttpServletRequest class. Simply you can add a HttpServletRequest parameter to your controller method and then get the context path using getContextPath () method.
If you want to get the base URL from a WebRequest you can do the following: ServletUriComponentsBuilder.fromRequestUri (HttpServletRequest request); This will give you the scheme ("http" or "https"), host ("example.com"), port ("8080") and the path ("/some/path"), while fromRequest (request) would give you the query parameters as well.
How TO - Get Current URL With JavaScript ❮ PreviousNext ❯ Learn how to get the current URL with JavaScript. Current URL Use window.location.hrefto get the current URL address: Example document.getElementById("demo").innerHTML = "The full URL of this page is:<br>" + window.location.href; Try it Yourself »
For example the correct URL is: Do you need the absolute URL? Or you just want to build a internal link to another page in your application? I need the absolute URL. This works perfectly. It returns the current action URL without the base URL and query strings. This does not work.
I think that getting absolute url is only possible while processing the request as your server may have many IP addresses and domain names.
@RequestMapping("/auth")
public String sendToAuthorization(HttpServletRequest request) {
String baseUrl = String.format("%s://%s:%d/tasks/",request.getScheme(), request.getServerName(), request.getServerPort());
return "redirect:" + auth.getAuthorizationUrl(baseUrl);
}
As for the servlet, it may also have several mappings in web.xml.
similar question
P.S. Anyway, url parsing in runtime does not look like a good idea to me.
Very late to this answer, but a variant to Boris's answer, if you don't want to push servlet objects into method signatures, is to use RequestContextHolder
from a utility class/method. This would also give the ability to abstract fallback logic (e.g., pulling from a property file). Cheesy example:
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if(null != requestAttributes && requestAttributes instanceof ServletRequestAttributes) {
HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
// build URL from request
}
else {
// fallback logic if request won't work...
}
This presumes you have org.springframework.web.context.request.RequestContextListener
registered as a listener in web.xml
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