Java, JSP, JSTL, EL, Tomcat notes and tips - mhulse/mhulse.github.io GitHub Wiki
- Create a file called
global.jsp
(for example). - Add your
taglib
calls and other important code:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="page" scope="session" value="${fn:split(fn:split(pageContext.request.servletPath, '.')[0], '/')[0]}" />
- Put this at the top of every include file you need the
taglib
s and/or variables:
<%@ include file="/includes/global.jsp" %>
Now you can use EL (Expression Language) and/or <c:xxx>
tags on your templates.
<%-- … something here … --%>
Static includes (compile time):
<%@ include file="path/to/file.jsp" %>
Dynamic includes (execution time):
<jsp:include page="path/to/file.jsp" />
<c:import url="http://www.example.com/foo/bar.html" />
Passing params to jsp:include
:
<jsp:include page="includes/top.jsp">
<jsp:param name="title" value="Detective Activity" />
</jsp:include>
<%--
<c:set var="s" value="${request.getHeader('referer')}" />
<%out.println(request.getHeader("referer"));%>
(${s})
${fn:contains(s, 'support-team.jsp')}
if(request.getHeader("referer").indexOf("support-team.jsp") != -1)
--%>
${header.referer}, ${header['referer']}, ${param.from}, ${param['from']}
<c:if test="${param.from == 'support-team'}">
<a href="#" class="close-ish" onclick="history.go(-1);return false;">Close</a>
</c:if>
${header.referer}
<c:if test="${fn:contains(header.referer, 'support-team.jsp')}">
<a href="#" class="close-ish" onclick="history.go(-1);return false;">Close</a>
</c:if>
This looks like lots of great tips: http://stackoverflow.com/questions/1296235/jsp-tricks-to-make-templating-easier