简体   繁体   中英

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!

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM