Describe the semantics of the Tag File model; describe the web application structure for tag files; write a tag file; and explain the constraints on the JSP content in the body of the tag.

Writing a traditional custom tag requires two steps:

Tag files simplify the process. First, tag files don't need to be compiled. They are compiled as they are invoked. Also, tag files allow tag extensions to be written using only JSP syntax. This means someone who does not know Java can also write tag extensions. Secondly, a tag element in a tag library descriptor describes the name to be used in a JSP page to reference the custom action. Using tag files, the name of a custom action is the same as the tag file representing the action. Therefore, you don't need a tag library descriptor at all. A tag file looks like a JSP page. It can have directives, scripts, EL expressions, and standard and custom tags. A tag file has the .tag or .tagx extension and can also include other files that contain a common resource. An include file for a tag file has a .tagf extension.

To work, tag files must be placed in the WEB-INF/tags directory under your application directory OR a subdirectory under it. The container converts each tag file found in (or uder) WEB-INF/tags into a tag handler.

A number of implicit objects are available from inside of a tag file. You can access these objects from a script or an EL expression:

Table 10.2. The Tag Files implicit objects

ObjectType
requestjavax.servlet.http.HttpServletRequest
responsejavax.servlet.http.HttpServletResponse
outjavax.servlet.jsp.JspWriter
sessionjavax.servlet.http.HttpSession
application javax.servlet.ServletContext
configjavax.servlet.ServletConfig
jspContextjavax.servlet.jsp.JspContext

This is the example of a tag library in which the tag file simply writes a String to the implicit out object:


<%—  WEB-INF/tags/myExample.tld —%>

<%
	out.println("Hello, World !");
%>
					
					
In JSP you need the taglib directive as usual, with the prefix attribute to identify your tag library throughout the page. NOTE, instead of a uri attribute, you have a tagdir attribute. The tagdir attribute refers to the WEB-INF/tags directory OR ANY subdirectory below WEB-INF/tags:
					
<%@ taglib prefix="simpleTag" tagdir="/WEB-INF/tags" %>

<simpleTag:myExample />
					
					

Combined with the expression language (EL), you can really build a scriptless JSP page very rapidly. The following example accepts an attribute from a calling JSP page and converts it to the upper case:

					
<%— WEB-INF/tags/myExample2.tld —%>

<%@ attribute name="someAttribute" %>
<%
	someAttribute = someAttribute.toUpperCase();
	out.println(someAttribute);
%>
					
					
The following JSP page that uses the tag file:

<%@ taglib prefix="simpleTag" tagdir="/WEB-INF/tags" %>

<simpleTag:myExample2 someAttribute="hello" />
					
					

example, which is Java-code-free:


<%— WEB-INF/tags/myExample3.tag —%>
<%@ variable name-given="myVar" scope="AT_BEGIN" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:set var="myVar" value="3"/>

After: ${myVar}

<jsp:doBody/>
					
					
To use the tag file, use the following JSP page:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="sampleTag" tagdir="/WEB-INF/tags" %>

<c:set var="myVar" value="1"/> 

Before: ${myVar} <br>

<simpleTag:myExample3/>
					
					

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide