第六章 自定义标签

tdTag.java

package com.msit.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
 * 输出内容的 自定义标签
 * @author Administrator
 *
 */

public class tdTag extends TagSupport {

 private String type="div";  //输出类型
 
 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }

 @Override  //带@是注解
 public int doStartTag() throws JspException {
  //创建输出对象
  JspWriter out=pageContext.getOut();
  try {
   if(type.equals("table")){
    out.print("<table border=1>");
    
    for(int i=0;i<5;i++){
     out.print("<tr>");
     out.print("<td>标输出表格</td>");
     out.print("</tr>");
    }
    out.print("</table>");
   }else if(type.equals("div")) {
    for(int i=0;i<5;i++){
    out.print("<div style='background-color: #fff;width: 180px;height: 35px'>");
    out.print("<font color='#fff'>输出DIV</font>");
    out.print("</div>");
   }
  }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return SKIP_BODY;  //SKIP 跳过或忽略
 }
 
 @Override
 public int doEndTag() throws JspException {
  
  return EVAL_PAGE; //EVAL 要评估和计算
 }
}

ptd.tld

<?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>
 <short-name>ptd</short-name>
 <uri>/msit/ptd</uri>
 <tag>
  <name>ptd</name>
  <tag-class>com.msit.tag.tdTag</tag-class>
  <body-content>empty</body-content>
  <attribute>
   <description>输出类型 可以是table也可以是div</description>
   <name>type</name>
   <rtexprvalue>false</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
 </tag>
</taglib>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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-app_2_5.xsd">
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <!-- jsp配置 -->
 <jsp-config>
   <taglib>
   <taglib-uri>http://com.msit.jsp/tag/ptd</taglib-uri>
   <taglib-location>/WEB-INF/ptd.tld</taglib-location>
  </taglib>

</jsp-config>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://com.msit.jsp/tag/ptd" prefix="pt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>

      调用自定义标签 <br>
      设置属性:<pt:ptd type="table" />
     <hr>
     <pt:ptd/>
  </body>
</html>

MyTagPrint.java

package com.msit.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
 * 定义第一个输出内容的自定义便签  处理类
 * @author Administrator
 *
 */
public class MyTagPrint extends TagSupport {
 
 //输出的类型
 private String type;  //table | ul
 private int count;  //输出次数
 private String context="Tag Context!!!"; //输出内容
 
 
 public int getCount() {
  return count;
 }

 public void setCount(int count) {
  this.count = count;
 }

 public String getContext() {
  return context;
 }

 public void setContext(String context) {
  this.context = context;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }

 @Override
 public int doStartTag() throws JspException {
  //创建处处对象
  JspWriter out= pageContext.getOut();
  try {
   if(type.equals("table")){
   out.print("<table border=1>");
   //循环的是行
   for(int i=0;i<count;i++){
    out.print("<tr>");
   out.print("<td>"+context+"</td><td>"+context+"</td>");
   out.print("</tr>");
   }
   out.print("</table>");
   }else if(type.equals("ul")){
    out.print("<ul>");
    for(int i=0;i<count;i++){
     out.print("<li>"+context+"</li>");
    }
    out.print("</ul>");
   }else{
    out.print("Hello Tag !!!!!");
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  return SKIP_BODY; //SKIP 跳过或忽略
 }
 
 @Override
 public int doEndTag() throws JspException {
  return EVAL_PAGE;  //评估或处理
 }

}

 

myprint.tld

<?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>
 <short-name>pt</short-name>
 <uri>/myprint</uri>
 <tag>
  <name>myprint</name>
  <!-- 标签处理类 -->
  <tag-class>com.msit.tag.MyTagPrint</tag-class>
  <body-content>empty</body-content>
  <!-- 标签属性说明 -->
  <attribute>
   <!-- 描述 -->
   <description>值可以输入table或者ul</description>
   <!-- 属性名 -->
   <name>type</name>
   <!-- 是否是必须的 值为true就是必须的 -->
   <required>true</required>
   <rtexprvalue>false</rtexprvalue>
   <!-- 属性类型 -->
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <description>输出次数</description>
   <name>count</name>
   <required>true</required>
   <rtexprvalue>false</rtexprvalue>
   <type>java.lang.Integer</type>
  </attribute>
  <attribute>
   <description>输出内容</description>
   <name>context</name>
   <rtexprvalue>false</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
 </tag>
</taglib>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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-app_2_5.xsd">
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <!-- jsp配置 -->
 <jsp-config>

  <taglib>
   <!-- jsp当中要使用标签 要用到的uri -->
   <taglib-uri>/msit/myprint</taglib-uri>
   <!-- 标签的描述文件TLD文件地址 -->
   <taglib-location>/WEB-INF/myprint.tld</taglib-location>
  </taglib>

</jsp-config>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>

index.html

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/msit/myprint" prefix="p" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
    调用自定义标签 <br>
    按表格输出:<p:myprint type="table" count="10" context="您好,Tag!" />
    <hr>

     按无序列表输出:<p:myprint type="ul" count="20" />   
    <div style='background-color: #000;width: 100px;height: 35px'>
    标签输出DIV

    </div> 
  </body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值