The first element we want to pull out of the file is the printing of all the error messages
above the form. This code won’t change across our wizard pages, and the only unique part is
the name of the command bean that errors might be bound to. We will create a tag file for this
snippet first.
To create a tag file, create a directory named tagsinside WEB-INF. Any file you place in
here with an extension of .tagwill be available to your JSP pages just like any other JSP tag.
The advantage here is that you do not need to write a Tag Library Descriptor (TLD), plus these
.tagfiles are recompiled on the fly just like JSP pages, making development much easier.
Listing 6-67 contains the first tag file, which takes a command bean name and prints
any errors.
Listing 6-67.Errors Tag File
<%@<%@ tag body-content="scriptless" %>
<%@ attribute name="name" required="true" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<spring:hasBindErrors name="${name}">
<ul>
<c:forEach items="${errors.allErrors}" var="error">
<li><span class="error"><spring:message message="${error}" /></span></li>
</c:forEach>
</ul>
</spring:hasBindErrors>
As you can see, the tag file looks a lot like a regular JSP page. The main differences are
found at the top of the file, with the tagand attributeelements.
If this looks so much like a JSP file, why not use a simple include instead of this tag file?
While includes are perfect for static content, this snippet requires a dynamic nameattribute for
<spring:hasBindErrors>. By using a tag file instead of an include, we can use this snippet any-
where we want to print out a list of errors from Spring’s validation framework.
Can you spot the second grossly repeated snippet from the JSP page? If you suggested that
the form input fields and labels look awfully similar, give yourself a hand. Listing 6-68 contains
the tagfile for simple <input> form elements. It has two dynamic elements: the path to the
field and the name of the field itself.
Listing 6-68.Form Field Tag File
<%@ tag body-content="scriptless" %>
<%@ attribute name="name" required="true" %>
<%@ attribute name="path" required="true" %>
<%@ attribute name="type" required="false" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:if test="${empty type}">
<c:set var="type" value="text" scope="page" />
</c:if>
186 CHAPTER 6 ■THE CONTROLLER MENAGERIE