amachado
amachado

Reputation: 1040

Include JSP files in a jsp page

I'm building a website using J2EE (using spring and JSP). I have mutiple pages like client.jsp and user.jsp. Each page has a header, a content and a footer.

So, What I want to do is to include in each jsp file (client or user) the header and footer, but each one has a specific header and footer. Let say, header_client.jsp and header_footer.jsp.

To include this I'm doing:

<%@ include file="/header_client.jsp" %>

But if this header does not exist I want to load a generic header.jsp.

These header/footer use vars from model like ${image}, or so!!

How can I test if exists the specific header/footer before include and import one or other?

Thank you!

Upvotes: 1

Views: 2335

Answers (2)

gene b.
gene b.

Reputation: 11974

Why not just the JSTL tags <c:if> (with a condition, like <c:if test='${someCondition}", and then <jsp:include>? You can even include <jsp:param> if you have any params to pass in to the included JSP.

Example:

<c:set var="myVar" value="someValue" />
..
<c:if test="${condition}">
  <jsp:include page="newPage.jsp" />
</c:if>

Upvotes: 0

Ankur Singhal
Ankur Singhal

Reputation: 26067

I will suggest using/creating templates and keeping most of the configuration in tiles.xml

Sample Code

<definition name="mytemplate" template="/WEB-INF/jsp/common/my_template.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/WEB-INF/jsp/common/header.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/WEB-INF/jsp/common/footer.jsp" />          
</definition>

Definition using template created above

<definition name="myPage" extends="mytemplate">
        <put-attribute name="title" value="My Page" />
        <put-attribute name="body" value="/WEB-INF/jsp/common/my.jsp" />
    </definition>

Upvotes: 1

Related Questions