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" />