《玫瑰之名》:JSP高手入:一个简单的关于自定义标签的JSP程序,输出内容为何不对?

来源:百度文库 编辑:高考问答 时间:2024/04/29 07:27:34
标签处理类InfoTagExample.java
************************************
package edu.jsp.tag;

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

public class InfoTagExample extends TagSupport
{
String userName;
public InfoTagExample()
{
super();
}

public void setUserName(String user)
{
this.userName = user;
}

public int doStratTag() throws JspTagException
{
System.out.println("doStartTag");
try{
pageContext.getOut().write("welcome" + userName);
} catch(Exception e){
e.printStackTrace();
}

return (SKIP_BODY);
}
}
************************************

TLD文件
************************************
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>
<tlib-version>1.2</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>mytag</short-name>
<description>This taglib provides tag example</description>
<tag>
<name>info</name>
<tag-class>edu.jsp.tag.InfoTagExample</tag-class>
<body-content>empty</body-content>
<attribute>
<name>userName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
************************************

Web.xml
************************************
<taglib>
<taglib-uri>demotag</taglib-uri>
<taglib-location>/WEB-INF/mytag.tld</taglib-location>
</taglib>
************************************

taginfo.jsp
************************************
<%@ taglib uri="demotag" prefix="mytag" %>

<html>
<head>
<title>custom tag</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<HR>
<mytag:info userName="John"/>
<HR>
</body>
</html>
************************************

编译通过后输出的只有两个横线(即<HR>),而没有“welcome John”,请问为什么??
各文件所在路径:
InfoTagExample.java (\WEB-INF\classes\edu\jsp\tag\InfoTagExample.class)
mytag.tld (\WEB-INF\mytag.tld)
Web.xml (\WEB-INF\Web.xml)
taginfo.jsp (\jsp\ch12Example\taginfo.jsp)

回llqqbb_net:
将jsp文件中的taglib uri="demotag"改为taglib uri="/WEB-INF/mytag.tld"跟我原来的程序是一样的意思
只不过我是在web.xml中引用自定义标签库的:
<taglib-uri>demotag</taglib-uri>
<taglib-location>/WEB-INF/mytag.tld</taglib-location>

<attribute>
<name>userName</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue> //这行的true改为false就ok
</attribute>

<%@ taglib uri=">/WEB-INF/mytag.tld" prefix="mytag" %>