jsp中自定义tag使用时错误

hcqhappy 2005-04-15 03:36:45
package java.jstl.test;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class sum extends SimpleTagSupport {
private int num1 = 0;
private int num2 = 0;

private void setNum1(int num1){
this.num1 = num1;
}
private void setNum2(int num2){
this.num2 = num2;
}

private void daTag() throws JspException ,IOException {

JspContext ctx = getJspContext();
JspWriter out = ctx.getOut();

int result = num1 + num2;
ctx.setAttribute("result" , Integer.toString(result));

out.println(num1 + " + " + num2 + " = " + result);
}

}
MyTaglib.tld
<?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">

<description>wo</description>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>test</short-name>
<uri></uri>
<small-icon></small-icon>


<tag>
<description>add</description>
<name>Add</name>
<tag-class>java.jstl.test.sum</tag-class>
<body-content>empty</body-content>

<attribute>
<name>num1</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

<attribute>
<name>num2</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

</tag>

</taglib>
...全文
99 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞行的兔子 2005-04-18
  • 打赏
  • 举报
回复
将<name>Add</name>
等等这些<name>中的东西和你引用是<prifix:tagname>中的tagname对应!
也就是说如果<prefix:sum>的话,肯定有个name标签中的内容是sum
hcqhappy 2005-04-18
  • 打赏
  • 举报
回复
ding
hcqhappy 2005-04-15
  • 打赏
  • 举报
回复
<%@ page contentType="text/html;charset=GB2312"%>
<%@ taglib uri="/WEB-INF/tlds/MyTaglib.tld" prefix = "addsum"%>

<html>
<head>
<title>sum</title>
</head>
<body>
<h1><addsum:sum num1="5" num2 = "9" /></h1>
最后结果 : ${result}
</body>
</html>

错误提示
org.apache.jasper.JasperException: /sum.jsp(9,4) No tag "sum" defined in tag library imported with prefix "addsum"
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:83)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:402)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:236)
org.apache.jasper.compiler.Parser.parseCustomTag(Parser.java:1346)
org.apache.jasper.compiler.Parser.parseElements(Parser.java:1598)
org.apache.jasper.compiler.Parser.parse(Parser.java:171)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:247)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:149)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:135)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:237)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:456)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧