第九章自定义标签第三节自定义有属性的标签

helloworld2.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:helloWorld2 name="jspservlet吊炸天"/>
body>
html>

Helloworld2.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 HelloWorldTag2 extends TagSupport{

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

    private String name;
    public String getName() {
        return name;
    }
     public void setName(String name) {
        this.name = name;
    }
    @Override
    public int doStartTag() throws JspException {
        JspWriter out = this.pageContext.getOut();
        try {
            out.println(name+"自定义标签");
        } 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>helloWorld2name>
       <tag-class>
          com.lang.tag.HelloWorldTag2
       tag-class>
       <body-content>emptybody-content>
       <attribute>
         <name>namename>
         <required>truerequired>
         <rtexprvalue>truertexprvalue> 
       attribute>
    tag>
 taglib>

运行结果:
这里写图片描述

自定义标签属性有:
第九章自定义标签第三节自定义有属性的标签_第1张图片

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