TagExtraInfo and custom variable name
I want to define the name of a variable of a Custom Tag in a JSP file as in
<tld:customtag name="customName">
<jsp:getProperty name="customName" property="message">
</tld:customtag>
The custom tag looks as
publicclass CustomTagextends BodyTagSupport{
private String name;
publicvoid setName(String name){ this.name = name;}
publicint doStartTag()throws JspException{
pageContext.setAttribute(name,new CustomBean());
return EVAL_BODY_BUFFERED;
}
}
The TagExtraInfo class looks like
publicclass CustomTagExtraInfoextends TagExtraInfo{
private String name;
public VariableInfo[] getVariableInfo(TagData data){
returnnew VariableInfo[]{
new VariableInfo(name,"java.lang.Object", true, VariableInfo.NESTED)
};
}
...
}
The tag library descriptor file looks like:
...
<tag>
<name>customtag</name>
<tag-class>custompackage.CustomTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>name</name>
<required>true</required>
</attribute>
<teiclass>custompackage.CustomTagExtraInfo</teiclass>
</tag>
...
How can I initialise the value of thename field in theCustomTagExtraInfo object to the value of thename attribute in thecustomtag custom tag?
Thanks
(TheTagExtraInfo.setTagInfo(TagInfo tagInfo) method is called before anyTagExtraInfo.getVariableInfo(TagData data) method call, containing the information in the tag library descriptor, but I do not see how I can get to the information in the JSP file.)
# 1
I think you are maybe overcomplicating things.
You should be able to do this straight from the tld, without any extra coding required:
<tag>
<name>customtag</name>
<tag-class>custompackage.CustomTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<variable>
<name-from-attribute>name</name-from-attribute>
<variable-class>java.lang.Object</variable-class>
<declare>true</declare>
<scope>NESTED</scope>
</variable>
</tag>
You declare the variable as taking its name from an attribute. You then have to have that attribute as well.
Cheers,
evnafets
# 2
Thanks for the answer, but I explicitly wanted to have the name of the variable defined in the JSP file and not in the TLD. (The example given is a simplification of my project's need.)
Here is the full code of the test project:
Custom bean class:
public class CustomBean {
public String getMessage() {
return "Yes it works!";
}
}
Custom tag class:
public class CustomTag extends BodyTagSupport {
private String name;
public void setName(String name) { this.name = name; }
public int doStartTag() throws JspException {
pageContext.setAttribute(name, new CustomBean());
return EVAL_BODY_BUFFERED;
}
public int doAfterBody() throws JspException {
try {
bodyContent.writeOut(bodyContent.getEnclosingWriter());
} catch(IOException e) { throw new JspException(e); }
return SKIP_BODY;
}
}
Custom tag extra information class:
public class CustomTagExtraInfo extends TagExtraInfo {
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo(data.getAttributeString("name"), "java.lang.Object", true, VariableInfo.NESTED)
};
}
}
TLD file:
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" 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">
<tlib-version>1.0</tlib-version>
<short-name>customtagtest</short-name>
<uri>/WEB-INF/tlds/customtagtest</uri>
<info>Just a quick test</info>
<tag>
<name>customtag</name>
<tag-class>custompackage.CustomTag</tag-class>
<body-content>JSP</body-content>
<info>Tests if you can access the attribute value.</info>
<attribute>
<name>name</name>
<required>true</required>
</attribute>
<teiclass>custompackage.CustomTagExtraInfo</teiclass>
</tag>
</taglib>
JSP file:
<%@taglib uri="/WEB-INF/tlds/customtagtest.tld" prefix="test"%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Testing the custom tag</title>
</head>
<body>
<test:customtag name="customName">
<jsp:getProperty name="customName" property="message"/>
</test:customtag>
</body>
</html>