Friday, November 21, 2014

Making a "master page" in a Java web site!

At first I thought I might have to make classic includes as described here around page content like so:

<jsp:include page="whatever.jsp" />

 
 

But this revealed a better way. It suggests building a file at web/WEB-INF/tags/genericpage.tag like so:

<%@tag description="Overall Page template" pageEncoding="UTF-8"%>
<%@attribute name="header" fragment="true" %>
<%@attribute name="footer" fragment="true" %>
<html>
   <head>
      <jsp:invoke fragment="header"/>
   </head>
   <body>
      <div id="whatever">
         <jsp:doBody/>
      </div>
   </body>
</html>

 
 

...and using it like so:

<t:genericpage>
      <jsp:attribute name="header">
         <title>Foo</title>
      </jsp:attribute>
      <jsp:body>
         <p>Baz</p>
      </jsp:body>
</t:genericpage>

 
 

This mostly worked great, but I started seeing an error to do with breaking into Java inside of the jsp:body tag which looked like this:

Scripting elements (<%!, <jsp:declaration, <%=, jsp:expression, <%, <jsp:scriptlet ) are disallowed here.

 
 

To fix things implementations like this:

<script src="<%=Signature.Foo %>" type="text/javascript" language="javascript">
</script>

 
 

...were made like look this:

<script src="${bar}" type="text/javascript" language="javascript">
</script>

 
 

...begging the question: What is ${bar}? Per this, that is an example of something which would need to be defined as an attribute upstream of the opening t:genericpage tag like so:

<% request.setAttribute("bar", Signature.Foo); %>

No comments:

Post a Comment