学了很多天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.index_jsp</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
<!-- JSPC servlet mappings end -->
<taglib>
<taglib-uri>/hello</taglib-uri>
<taglib-location>/WEB-INF/tlds/hello.tld</taglib-location>
</taglib>
</web-app>
[解决办法]
自定义标签不怎么简单吧,其实挺麻烦的,都不怎么用。别郁闷了
[解决办法]
TldLocationsCache: XML parsing error on file /WEB-INF/tlds/hello.tld: (line 1, col 43)
你的 helll.tld 的第一行,第43列语法错误,仔细看看吧!
[解决办法]
第一行换成这个
<?xml version="1.0" encoding="ISO-8859-1"?>
你的那个里面竟然有特殊字符,我也不知道怎么回事!
[解决办法]
在你的 tld 文件中配置 < uri > /hello</uri> (web.xml文件中就不配置了)
试下看.....
[解决办法]
我现在还不会用自定义标签呢。。
惭愧``
[解决办法]
没怎么用自定义标签
[解决办法]