日期:2014-05-18  浏览次数:20708 次

学了很多天jsp了,连最简单的java自定义标签都没学会啊
谁能帮我看看,最简单的标签啊,这辈子第一个标签程序...不知道运行的错误在哪
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT下的java文件(HelloTag.java)

package JohnWorks.taglibs;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class HelloTag extends TagSupport
{
  public int doStartTag() throws JspException,IOException
  {
  JspWriter out=pageContent.getOut();
  out.println("<h1>Hello!</h1>");
  return SKIP_BODY;
  }
  public int doEndTag()
  {
  return EVAL_PAGE;
  }
}  

jsp文件:(show.jsp)

<%@ taglib uri="/hello" prefix="mytag" %>
<html>
<body>
<mytag:hello/>
</body>
</head>

C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\WEB-INF\tlds 目录下的tld文件:(hello.tld)
<?xml version="1.0" endcoding="ISO-8859-1"?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  version="2.0">
   
  <tlib-version>1.0</tlib-version>
  <short-name>hello</short-name>

  <description>
  An example tag.
  </description>
 
  <tag>
  <name>hello</name>
  <tag-class>JohnWorks.taglibs.HelloTag</tag-class>
  <body-content>empty</body-content>
  </tag>
</taglib>

C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\WEB-INF XML文件配置:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Copyright 2004 The Apache Software Foundation

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app 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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">

  <display-name>Welcome to Tomcat</display-name>
  <description>
  Welcome to Tomcat
  </description>

<!-- JSPC servlet mappings start -->

  <servlet>
  <servlet-name>org.apache.jsp.index_jsp</servlet-name>
  <servlet-class>org.apache.jsp.index_jsp</servlet-class>
  </servlet>

  <servlet-mapping>
  <servlet-name>org.apache.jsp.in