JAVA中使用TAGLIB配置过程

本文详细介绍了在JAVA中创建并使用自定义TAGLIB的步骤,包括编写JAVABEAN文件、创建TLD文件、配置WEB.xml、在页面中引入及解决常见错误。通过这些步骤,开发者可以实现自定义的页面控件并正确引用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.准备自定义JAVABEAN文件

import java.io.IOException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;


public class CustomTag extends TagSupport {

public int doEndTag() throws JspTagException
{
try {
pageContext.getOut().write("Hello word!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return EVAL_PAGE;
}
}

2.新增TAGSAMPLE.TLD文件

<?xml version="1.0" encoding="UTF-8"?>
<!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.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>1.2</short-name>
  <tag>
    <name>hello</name>
    <tag-class>zf.CustomTag</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>

3.在WEB.xml新增节点

<jsp-config> 
  <taglib>
  <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
  <taglib-location>/WEB-INF/tag/TAGSAMPLE.TLD</taglib-location>
  </taglib>
</jsp-config>

4.在引用 页面加入:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="ewf" %>

5.可以直接引用自定义的页面控件 

<ewf:hello></ewf:hello>

6.注意事項 :

提示錯誤:Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"

解決方式:引用以下兩個jar包jstl.jar,standard.jar


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值