Absolute url in jsp

2019-01-26 14:43发布

问题:

While including css, js and images in my jsps I am facing a problem resolving relative urls. The urls get changed on refreshing the page or clicking the back button. I suppose one solution to the problem would be to include external files by using absolute urls. But I can't find out how to use a reference to the relative url and use it. Can anyone please help me on this?

回答1:

Is this what you're looking for?

${pageContext.servletContext.contextPath}

and then in your jsp:

<link rel="stylesheet" href="${pageContext.servletContext.contextPath}/css/page.css"   media="all"    type="text/css" />


回答2:

This would also work and may be easier to read and has some extra benefits such as proper escaping and optional inclusion of parameters

<link href="<c:url value="/style/style.css"/>" rel="stylesheet" type="text/css" />



标签: jsp url jsf