Month: March 2007

Comments and SPAM

I did some work on the blog code so that you can now comment on posts without registering a user before.

At first I tried allowing comments with only a user name and an email address, but faced SPAM comments very soon. Since I’m not really convinced that you are interested in ring tones, I also added some calculatory CAPTCHA test which will hopefully solve that issue ( You are allowed to cheat and use a calculator 😉 

Creating JSP Layouts with Page Tags

For some time now I’ve been following a new approach when creating the basic layout structure for JSP based web applications. The new approach is based on JSP 2.0 tag files and changes the way that the single pages are build up from reusable parts.


The most common way to do so is to use included files with common blocks:

<%@include “head.inc”%>

<b>page specific markup</b>

<%@include “tail.inc”%>

Here the individual pages contain two or more includes which define the page start and end. Often navigation or sidebars or whatever are included in their own files. Changing the output of the title in the header e.g. requires passing data through the page scope or so.

My new approach defines the basic layout as JSP 2.0 tag file. Let’s take a look at an example:

<%@tag description=”page layout” %>
<%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>
<%@attribute name=”head” fragment=”true”%>
<%@attribute name=”title” required=”true” type=”java.lang.String”%>
<%@attribute name=”script” required=”false” type=”java.lang.String”%>
<%@attribute name=”stylesheet” required=”false” type=”java.lang.String”%>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<title>${title} – Page Tag Example</title>
<style type=”text/css”>
@import url(“<c:url value=”/style/style.css”/>“);
<c:forTokens var=”item” items=”${stylesheet}” delims=”,”>
@import url(“<c:url value=”/style/”/>${item}“);
</c:forTokens>
</style>

<c:forTokens var=”item” items=”${script}” delims=”,”>
<script type=”text/javascript” src=”<c:url value=”/script/”/>${item}“>
</script>
</c:forTokens>
<jsp:invoke fragment=”head” />
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<meta http-equiv=”Pragma” content=”no-cache”>
</head>
<body>
<h1>Page Tag Example</h1>
<ul id=”nav”>
<li><a href=”index.jsp”>Index</a></li>
<li><a href=”page1.jsp”>Page 1</a></li>
<li><a href=”page2.jsp”>Page 2</a></li>
</ul>
<div id=”content”>
<jsp:doBody />
</div>
<hr>
<div id=”footer”>
<a href=”http://validator.w3.org/check?uri=referer”><img
src=”http://www.w3.org/Icons/valid-html401″ border=”0″
alt=”Valid HTML 4.01 Transitional” height=”31″ width=”8″></a>
</div>
</body>
</html>

The tag defines the example page layout with a page header, a navigation and a footer. There are several attributes to control the page tag from inside the JSP page it is used in:

  • head – Fragment attribute used to add additional HTML markup to the head section of the page
  • title – Required attribute which sets the page specific part of the page title
  • script – Comma separated lists of script files to include from the /script directory
  • stylesheet – Comma separated lists of style sheets to import from the /style directory

The head fragment attribute demonstrates how to setup a fragment attribute which makes it possible to set HTML code as an attribute. To use such a fragment attribute inside a page you need to use the <jsp:attribute> and <jsp:body> tags:

<%@taglib tagdir=”/WEB-INF/tags” prefix=”example”%>
<example:page title=”Page 1″>
<jsp:attribute name=”head”>
<meta name=”author” content=”Sven Helmberger”>
</jsp:attribute>
<jsp:body>
Page 1 – demonstrates the use of the head fragment attribute
</jsp:body>
</example:page>

If you only use normal (non-fragment) attributes you can leave out the <jsp:attribute> and <jsp:body> tags and specify the rest of the attributes like the title attribute above. Fragment attributes can also be useful to define common code blocks besides the tag body like a page-specific area in a sidebar or so.

You can see that, even with fragment attributes complicating things, the page tag leads to a simple JSP code with high reuse and clear passing of data from the special page code to the common code.

© 2024 fforw.de

Theme by Anders NorénUp ↑