What is the right syntax for jsp:include?

57 Views Asked by At

My application is a hybrid of Servlets and Spring.

Most pages are divided in subsections, included in the main jsp using jsp:include. I'm having some issues when the target of the import is a REST call instead of an actual JSP page.

I had to change the paths from "../rest/xyz" to "${contextPath}/rest/xyz" because sometimes the first version would result in "appName/rest/rest/xyz".

However, I am still facing issues from time to time when there are nested includes: the resulting path is "appName/appName/rest...". I am struggling to find a syntax that works for all the cases.

Is there a better way to do it?

EXAMPLE:

in books.jsp I have

<jsp:include page="/jsp/library/chapters.jsp"/>

then in chapters.jsp I have

<jsp:include page="/rest/paragraphs?chapter=x"/>

But I cannot get the path to paragraphs right:

  • ${contextPath}/rest/paragraphs results in appName/appName/rest/paragraphs
  • ../rest/paragraphs results in appName/jsp/library/../rest/paragraphs
1

There are 1 best solutions below

1
lance-java On

It's best practice to declare your paths relative to the root (eg start your paths with /)

<jsp:include page="/WEB-INF/jsp/foo.jsp" />
<jsp:include page="/WEB-INF/jsp/bar.jsp" />
<jsp:include page="/${contextPath}/rest/xyz" />