JSP自定义标签-第一个简单的例子

此例子标签的效果

这里写图片描述

开发步骤

定义实体类
public class Emp {

    private String empId;

    private String name;

    private String phoneNumber;

    public Emp() {
    }

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}
创建标签库描述文件:/WEB-INF/tag/emptag.tdl
<?xml version="1.0" encoding="UTF-8"?>

<taglib version="2.0" 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 web-jsptaglibrary_2_0.xsd">

<tlib-version>1.0</tlib-version>

<jsp-version>2.0</jsp-version>

<short-name>webapp</short-name>

<uri>/webapptaglib</uri>

<tag>

    <name>empInfo</name>

    <tag-class>com.sdTag.EmpTag</tag-class>

    <body-content>empty</body-content>

    <attribute>

         <name>emp</name>

         <required>false</required>

         <rtexprvalue>true</rtexprvalue>

    </attribute>

 </tag>

</taglib>
在web.xml中配置映射
<jsp-config>
    <taglib>
        <taglib-uri>/webapptaglib</taglib-uri>
        <taglib-location>/WEB-INF/tag/emptag.tdl</taglib-location>
    </taglib>
</jsp-config>
自定义标签类
package com.sdTag;

import java.io.IOException;

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

import com.Emp;

public class EmpTag extends TagSupport {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Emp emp;

    public int doStartTag() throws JspException {
        JspWriter out = this.pageContext.getOut();
        try {
            if (emp == null) {

                out.println("No data Found...");

                return SKIP_BODY;

            }
            out.println("<table width='500px' border='3' bordercolor='red' align='center'>");

            out.println("<tr>");

            out.println("<td width='20%'>Emp Id:</td>");

            out.println("<td>" + emp.getEmpId() + "</td>");

            out.println("</tr>");

            out.println("<tr>");

            out.println("<td>Name:</td>");

            out.println("<td>" + emp.getName() + "</td>");

            out.println("</tr>");

            out.println("<tr>");

            out.println("<td>Phone Number:</td>");

            out.println("<td>" + emp.getPhoneNumber() + "</td>");

            out.println("</tr>");

            out.println("</table>");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return SKIP_BODY;
    }

    @Override

    public void release() {

        super.release();

        this.emp = null;

    }



    //getter and setters

    public Emp getEmp() {

        return emp;

    }

    public void setEmp(Emp emp) {

        this.emp = emp;

    }

}
最后在JSP中引用自定义标签
<%@page import="com.Emp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/webapptaglib" prefix="webapp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <%

    Emp emp = new Emp();

    emp.setEmpId("AP001");

    emp.setName("Tom");

    emp.setPhoneNumber("10086");

    pageContext.setAttribute("emp", emp);  

    %>

    <webapp:empInfo emp="${pageScope.emp }" />

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值