How to set JSP base url with JSTL
March 13, 2012 08:46:57 Last update: March 13, 2012 08:46:57
This trick sets HTML
base to the root context path of the current webapp:
- The short version:
<!DOCTYPE html> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <html> <head> <c:set var="req" value="${pageContext.request}" /> <base href="${fn:replace(req.requestURL, req.requestURI, req.contextPath)}/" /> </head> . . . </html>
- The long version:
<!DOCTYPE html> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <html> <head> <c:set var="req" value="${pageContext.request}" /> <c:set var="http_base" value="${req.scheme}://${req.serverName}:${req.serverPort}${req.contextPath}/"/> <base href="${pageContext.request.secure ? fn:replace(http_base, ':443/', '/') : fn:replace(http_base, ':80/', '/')}"/> . . . </html>