JSP - Custom tags
Oi! Alguém me pode ajudar com este pequeno problema.
Estou experimentado as minhas primeiras custom tags, mas não está resultando.
[b:6cc87f81a5]Esta é a minha JSP[/b:6cc87f81a5]:
<¬@ taglib uri=´/WEB-INF/tlds/MyTags.tld´ prefix=´myjsp´ ¬>
<¬
String s = ´ERROR TEST!´;
¬>
<myjsp:error name=´<¬= s ¬>´ />
[b:6cc87f81a5]Esta é a minha classe [/b:6cc87f81a5](ErrorTag):
package jspproject;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class ErrorTag implements Tag, Serializable {
private PageContext pc = null;
private Tag parent = null;
private String name = null;
public void setPageContext(PageContext p) {
pc = p;
}
public void setParent(Tag t) {
parent = t;
}
public Tag getParent() {
return parent;
}
public void setName(String s) {
name = s;
}
public String getName() {
return name;
}
public int doStartTag() throws JspException {
try {
if(name != null) {
pc.getOut().write(´Error: ´ + name + ´!´);
} else {
pc.getOut().write(´Some other error!´);
}
} catch(IOException e) {
throw new JspTagException(´An IOException occurred.´);
}
return SKIP_BODY;
}
public int doEndTag() throws JspException {
return Tag.SKIP_PAGE;
}
public void release() {
parent = null;
name = null;
}
}
[b:6cc87f81a5]Este é o meu ficheiro TLD[/b:6cc87f81a5]:
<?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.2</jspversion>
<shortname>DemoTags</shortname>
<uri>http://www.stardeveloper.com</uri>
<info>Demo Tags Library</info>
<tag>
<name>error</name>
<tagclass>jspproject.ErrorTag</tagclass>
<bodycontent>empty</bodycontent>
<info>JSP Error Tag</info>
<attribute>
<name>name</name>
<required>false</required>
</attribute>
</tag>
</taglib>
[b:6cc87f81a5]E este é o resultado[/b:6cc87f81a5]:
Error: <¬= s ¬>!
Alguém me pode dizer o que estou fazendo de errado :x ?
Se eu colocar uma String na JSP como por exemplo: <myjsp:error name=´Error Test!´ />, funciona perfeitamente.
Estou experimentado as minhas primeiras custom tags, mas não está resultando.
[b:6cc87f81a5]Esta é a minha JSP[/b:6cc87f81a5]:
<¬@ taglib uri=´/WEB-INF/tlds/MyTags.tld´ prefix=´myjsp´ ¬>
<¬
String s = ´ERROR TEST!´;
¬>
<myjsp:error name=´<¬= s ¬>´ />
[b:6cc87f81a5]Esta é a minha classe [/b:6cc87f81a5](ErrorTag):
package jspproject;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class ErrorTag implements Tag, Serializable {
private PageContext pc = null;
private Tag parent = null;
private String name = null;
public void setPageContext(PageContext p) {
pc = p;
}
public void setParent(Tag t) {
parent = t;
}
public Tag getParent() {
return parent;
}
public void setName(String s) {
name = s;
}
public String getName() {
return name;
}
public int doStartTag() throws JspException {
try {
if(name != null) {
pc.getOut().write(´Error: ´ + name + ´!´);
} else {
pc.getOut().write(´Some other error!´);
}
} catch(IOException e) {
throw new JspTagException(´An IOException occurred.´);
}
return SKIP_BODY;
}
public int doEndTag() throws JspException {
return Tag.SKIP_PAGE;
}
public void release() {
parent = null;
name = null;
}
}
[b:6cc87f81a5]Este é o meu ficheiro TLD[/b:6cc87f81a5]:
<?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.2</jspversion>
<shortname>DemoTags</shortname>
<uri>http://www.stardeveloper.com</uri>
<info>Demo Tags Library</info>
<tag>
<name>error</name>
<tagclass>jspproject.ErrorTag</tagclass>
<bodycontent>empty</bodycontent>
<info>JSP Error Tag</info>
<attribute>
<name>name</name>
<required>false</required>
</attribute>
</tag>
</taglib>
[b:6cc87f81a5]E este é o resultado[/b:6cc87f81a5]:
Error: <¬= s ¬>!
Alguém me pode dizer o que estou fazendo de errado :x ?
Se eu colocar uma String na JSP como por exemplo: <myjsp:error name=´Error Test!´ />, funciona perfeitamente.
Mephystus
Curtidas 0