• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TagExtraInfo Question

 
Chris Salivar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody tell me why I am getting the 'Undefined variable: name' error in my JSP for the following code running on orion server:
hello.jsp:
---------------------------------------------------
<%@ taglib uri="mytags" prefix="mt" %>
<HTML>
<HEAD>
<TITLE>Hello!</TITLE>
</HEAD>
<mt:hello>
Inside the body of the tag <%=name%>
</mt:hello>
<%=name%> (THIS name NOT BEING IDENTIFIED BY THE JSP)
</BODY>
</HTML>
HelloTagExtraInfo.java:
------------------------------------------------------
import javax.servlet.jsp.tagext.*;
public class HelloTagExtraInfo extends TagExtraInfo {
public HelloTagExtraInfo() {
super();
}
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo(
"name", "java.lang.String",
true, VariableInfo.AT_BEGIN)};
}
}
HelloTag.java:
----------------------------------------------------
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
public class HelloTag extends TagSupport
{
public HelloTag() {
super();
}
public int doStartTag() throws JspTagException {
pageContext.setAttribute("name", "chris");
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspTagException {
return EVAL_PAGE;
}
}
taglib.tld:
--------------------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion>
<shortname>mt</shortname>
<tag>
<name>hello</name>
<tagclass>com.chris.tag.HelloTag</tagclass>
<teiclass>com.chris.tag.HelloTagExtraInfo</teiclass>
<bodycontent>JSP</bodycontent>
<info>A simple hello</info>
</tag>
</taglib>
It works fine if I remove the name expression outside the tag body in the JSP page.
TIA
- Chris
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The scope for a variable defined in a TagExtraInfo is NESTED by default. In other words, it is not accessible after the particular closing tag unless you explicitly define it otherwise.
 
ken chou
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

On your jsp page, you need to declare the varable "name" like the following:
<%! String name = null; %>
 
Chris Salivar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron, I have declared the scope of the variable to be AT_BEGIN so its not the default one.
Ken, declaring a variable in the JSP page would beat the purpose of using custom tags because we don't want to use java code in the JSP page.
Is it some other problem? Anybody?
TIA
-Chris
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should work with a scope of AT_BEGIN. Which server are you using?
Cheers
Simon
------------------
Simon Brown
Co-author of Professional JSP 2nd Edition
 
Chris Salivar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Simon, it magically started working when i woke up in the morning and tried it again. BTW I am using the orion server.
- Chris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic