|
|
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 4 of 6
JSP 2.0 introduced a much simpler tag-handling protocol. If you extend the SimpleTagSupport class, you just have to implement the doTag() method for handling a JSP tag.
The TestMethodTag.jsp page calls the testMethod() JavaBean method using the following syntax:
<tm:testMethod object="..." text="..." number="..." logic="..."/>
When the application server translates the JSP page into a servlet, the above tag is replaced with a Java code fragment that
calls the methods of a TestMethodTag instance created for handling the tag.
The tag handler extends the JSP 2.0 API's SimpleTagSupport class and defines one field for each attribute. These fields will maintain the tag attributes' values:
package com.devsphere.articles.calltag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
public class TestMethodTag extends SimpleTagSupport {
private TestBean object;
private String text;
private int number;
private boolean logic;
For each tag attribute, there must be a set method, which gets the attribute value and stores it in a field so that the tag handler can use it later:
public void setObject(TestBean object) {
this.object = object;
}
public void setText(String text) {
this.text = text;
}
public void setNumber(int number) {
this.number = number;
}
public void setLogic(boolean logic) {
this.logic = logic;
}
After setting the tag handler's attributes, the Java fragment (resulting from the JSP tag) invokes the tag handler's doTag() method, which calls the bean method. The doTag() method prints the string value returned by testMethod(). Therefore, the JSP output contains the returned value:
public void doTag() throws JspException, IOException {
String ret = object.testMethod(text, number, logic);
JspWriter out = getJspContext().getOut();
out.println(ret);
}
}
Suppose you want to use the value returned by the bean method in a JSP. For example, you might have to pass it as an attribute value to another tag. Or, you might want to control its output in the JSP page:
<tm:testMethod2 object="..." text="..." number="..." logic="...">
... ${ret} ...
</tm:testMethod2>
The TestMethodTag2 handler class resembles TestMethodTag. Both have the same fields and methods, but the doTag() method is implemented differently:
package com.devsphere.articles.calltag;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
public class TestMethodTag2 extends SimpleTagSupport {
private TestBean object;
private String text;
private int number;
private boolean logic;
public void setObject(TestBean object) {
this.object = object;
}
public void setText(String text) {
this.text = text;
}
public void setNumber(int number) {
this.number = number;
}
public void setLogic(boolean logic) {
this.logic = logic;
}
The TestMethodTag2's doTag() method calls testMethod() and uses setAttribute() to create a JSP variable (named ret) that holds the object returned by the bean method within the page scope.