resin Resin 3.0 tutorial Tag Files

JSP 2.0 introduces XML-based tag files. Instead of having to write Java code for custom tags, applications can use XML or JSP syntax for their tags. Tag files do not require .tld files, making development simpler.

test.jsp
The jsp page that uses the tags The basic tag.

Tag files in JSP 2.0 look and run like normal JSP files with some extra directives. This example definine a tag attribute "a" which takes a String value. It prints a simple message.

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"> <jsp:directive.attribute name="a" type="java.lang.String" required="true"/> <jsp:text>A sample tag: ${'${'}a}</jsp:text> </jsp:root>

The extension identifies the tag as a JSP tag file using XML syntax. The example could also have used for a tag file in JSP syntax. Tag files normally belong in WEB-INF/tags, although the location is configurable.

The tag example uses the JSP 2.0 Expression Language as an example and as a way to avoid any Java in the tag file. The ${"${"}a} tells the tag to look in the pageContext for a local variable a. The value of a is assigned by the tag attribute as defined in the attribute directive.

Using tag files is similar to using normal JSP tags. This example uses the XML syntax, although normal JSP syntax would work, too. The namespace prefix tells JSP where to look for the tag. In this case it will look in WEB-INF/tags.

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:tags="urn:jsptagdir:/WEB-INF/tags"> <tags:test a="Test 1"/><br/> <tags:test a="Test 2"/> </jsp:root> A sample tag: Test 1<br> A sample tag: Test 2