|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 6
package com.devsphere.articles.calltag;
public class TestFunction {
public static String testMethod(TestBean object,
String text, int number, boolean logic) {
return object.testMethod(text, number, logic);
}
}
The compiled TestFunction.class file must be placed together with TestBean.class into the Web application's /WEB-INF/classes directory. As an alternative, the two classfiles can be packed in a jar file and stored in /WEB-INF/lib.
Before calling the testMethod() function, the TestFunction.jsp page must specify the function's prefix and the library's Uniform Resource Identifier (URI):
<%@ taglib prefix="tf" uri="http://devsphere.com/articles/calltag/TestFunction.tld"%>
The <jsp:useBean> tag creates an instance of the TestBean class:
<jsp:useBean id="obj" class="com.devsphere.articles.calltag.TestBean"/>
The testMethod() function is called twice. The first call gets some constant parameters, while the second call gets the values of the bean
properties as parameters:
<HTML>
<BODY>
${tf:testMethod(obj, "abc", 123, true)}
<HR>
${tf:testMethod(obj, obj.text, obj.number, obj.logic)}
</BODY>
</HTML>
The TestFunction.jsp page produces the following HTML output:
<HTML> <BODY> abc 123 true <HR> abcabc 246 true </BODY> </HTML>
As mentioned earlier, the JSP function must be declared in a tag library descriptor. The TestFunction.tld file defines some version number, the tf short name used in JSP pages as prefix for testMethod(), the library's URI, the function's name, the name of the class containing the static method, and the method's signature.
The URI doesn't have to point to an existing Web resource, but it must be unique. You may not use the same URI for two different
tag libraries.
Here is the TestFunction.tld file's content:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>tf</short-name>
<uri>http://devsphere.com/articles/calltag/TestFunction.tld</uri>
<function>
<name>testMethod</name>
<function-class>
com.devsphere.articles.calltag.TestFunction
</function-class>
<function-signature>
java.lang.String testMethod(
com.devsphere.articles.calltag.TestBean,
java.lang.String, int, boolean)
</function-signature>
</function>
</taglib>
The TestFunction.tld file must be placed into the Web application's /WEB-INF directory. The same directory also contains the web.xml application descriptor, which declares the library within a <taglib> element. The URI that identifies the library in JSP pages and the TLD file's location are specified within two separate XML
elements, <taglib-uri> and <taglib-location>:
<taglib>
<taglib-uri>
http://devsphere.com/articles/calltag/TestFunction.tld
</taglib-uri>
<taglib-location>
/WEB-INF/TestFunction.tld
</taglib-location>
</taglib>
Tag libraries were introduced by JSP 1.1, which defined the Tag and BodyTag interfaces. JSP 1.2 added IterationTag and support for catching exceptions. These interfaces have handler methods such as doStartTag(), doInitBody(), doAfterBody(), and doEndTag(). Once you understand how these methods should be implemented, it's easy to build tag libraries. However, many developers
viewed JSP 1.x's tag-handling mechanism as unnecessarily complex.