How to create a var attribute on scriptless tags
JavaServer Pages 2.0 have a feature called Scriptless tags. Scriptless tags are in a way traditional taglibs but written i JSP and they do provide a good feature to JSP. I must have written tons of JSP during the years and I am not gonna discuss JSP in general here but I've always found the JSP documentation kind of unintuitive and lacking good simple examples on some issues.
It might be my search phrases which are poor, but one such issue I've newer found a simple example on is the simple task of defining a var attribute on a scriptless tag. So here is my note to self example on creating a scriptless tag with a var attribute.
From traditional taglibs we have the familiar "var" attribute which lets us define a variable to an user defined variable name.
<c:set var="foo" value="bar" />
${foo}
This is also possible with Scriptless tags but its not, at least not in my head, very intuitive to define and not well documented.
The tag file:
This is what goes into the tag file, here a hello.tag file:
<%@ tag body-content="empty" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="var"
rtexprvalue="false"
required="true"
type="java.lang.String" %>
<%@ variable name-from-attribute="var"
alias="varAlias"
variable-class="java.lang.Object"
scope="AT_END" %>
<c:set var="varAlias">Hello World!</c:set>
Using the tag file:
A jsp using the hello.tag file:
<%@ taglib tagdir="/WEB-INF/tags" prefix="tags" %>
<tags:hello var="myCustomVariable" />
The scriptless say: ${myCustomVariable}
How it works:
The trick is to define an attribute with the name var and then make a variable use the value provided on that var attribute as name for the variable which the tag stores its result on.
Let's run trough the directives:
<%@ attribute name="var"
rtexprvalue="false"
required="true"
type="java.lang.String" %>
The attribute directive declares what becomes the attributes on the scriptless tags. Here we define an attribute with the name "var". This attribute is set to be of type “java.lang.String” since variable names are strings. We do also set it to required since our variable directive will be using it and we must set the named attribute not to accept values on request time by setting rtexprvalue to "false".
<%@ variable name-from-attribute="var"
alias="varAlias"
variable-class="java.lang.Object"
scope="AT_END" %>
The variable directive declares what variables which should be exposed to the caller of the scriptless tag. On the variable directive we define the variable to get its name from the attribute directive by setting name-from-attribute to the same as name in the attribute directive. To be able to store the result of the tag on the variable, we must define an alias which we store its result on.
We might not be 100% sure what type of data the variable will store so we set it to the generic “java.lang.Object” by defining the variable-class attribute. If we are sure on what data type the variable will be holding, the value for variable-class should be that type. The scope is set to "AT_END" to make sure the result of the tag is copied to the variable before exiting the tag.
<c:set var="varAlias">Hello World!</c:set>
In the body of the tag we can do whatever we want and store the result on the alias of the variable directive. In the example we store the string “Hello World!” on the variables alias.
When calling the scriptless tag, the value we used on the var attribute are now used as a return name for a variable of the tag. This return variable has an alias which are used in the tag to store the tags result which then returns the result of the tag on the var variable we defined.
<%@ tag body-content="empty" pageEncoding="UTF-8" %>
Do also note that body-content on the tag directive in the scriptless tag is set to "empty" since the tag sets its value on the defined variable and is not supposed to return a body content.
26.08.2009 01:44 - Posted by Trygve - Comments: 1 - Technical
About:
My name is Trygve Lie. I live in Oslo, Norway where I deal with web technology. You can read a bit more about me here.
Search:
Categories:
Links:
Feeds:
Tags
- atom
- blog
- components
- css
- dom
- entitys
- esi
- html
- ie6
- ie7
- ie8
- java
- javascript
- jsp
- jstl
- performance
- roller
- saxon
- scriptless
- selectors
- structure
- theme
- validation
- varnish
- webslices
- welcome
- xalan
- xhtml
- xml
- xslt
Comments:
Great! Saved me a lot of time
10.12.2009 11:13 - Posted by Ronen - Permalink