第九章自定义标签第二节利用标签输出“自定义标签”到客户端

helloWorld.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
 <%@ taglib prefix="please" uri="/WEB-INF/lang.tld"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title heretitle>
head>
<body>
     <please:helloWorld/>
body>
html>

HelloWorldTag.java

package com.lang.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class HelloWorldTag extends TagSupport{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public int doStartTag() throws JspException {
        JspWriter out = this.pageContext.getOut();
        try {
            out.println("自定义标签");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return TagSupport.SKIP_BODY; //直接结束标签
    }
}

配置文件:lang.tld



<taglib xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
    <tlib-version>1.0tlib-version>
    <short-name>pleaseTagshort-name>

    <tag>
       <name>helloWorldname>
       <tag-class>
          com.lang.tag.HelloWorldTag
       tag-class>
       <body-content>emptybody-content>
    tag>
taglib>

输出结果:
这里写图片描述

你可能感兴趣的:(jsp/servlet)