自定义标签

案例如下:
目的:为了在jsp页面里面根据我传入的值来显示相应的文本信息
第一步:新建一个erp.tld

<?xml version="1.0" encoding="UTF-8" ?>

<taglib 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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
    
  <description>erp  library</description>     
  <display-name>erp core</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>e</short-name>
  <uri>http://txjava/jsp/erp/core</uri>



  <tag>
   
  
    <name>orderTypetext</name>
    <tag-class>cn.tx.tag.ERPOrderTypeTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
       	
        <name>orderType</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
   
  
    <name>orderStatetext</name>
    <tag-class>cn.tx.tag.ERPOrderStateTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
       	
        <name>orderType</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    
    <attribute>
       	
        <name>orderState</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

</taglib>

1.解析:
在这里插入图片描述
在这里插入图片描述
接下来把标签类写出来:

第一个标签类:
解析:从该类中可以看出只有一个属性
在这里插入图片描述

package cn.tx.tag;

import java.io.IOException;

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

import cn.tx.utils.ERPConstants;

public class ERPOrderTypeTag extends TagSupport {
	
	
	
	//设置订单类型值
		private String orderType;
		
		
		

		public String getOrderType() {
			return orderType;
		}




		public void setOrderType(String orderType) {
			this.orderType = orderType;
		}
	
	




	@Override
	public int doStartTag() throws JspException {
		//获得JSPWriter对象向jsp页面写文本
		JspWriter out = pageContext.getOut();
		String text = "";
		switch(orderType){
			case ERPConstants.ORDER_TYPE_BUY:
				text = ERPConstants.ORDER_TYPE_BUY_TEXT;
				break;
			case ERPConstants.ORDER_TYPE_TRANS:
				text = ERPConstants.ORDER_TYPE_TRANS_TEXT;
				break;
			case ERPConstants.ORDER_TYPE_INSTOCK:
				text = ERPConstants.ORDER_TYPE_INSTOCK_TEXT;
				break;
			case ERPConstants.ORDER_TYPE_SALES:
				text = ERPConstants.ORDER_TYPE_SALES_TEXT;
				break;
		}
		try {
			out.write(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	
	
	
	
	

}

第二个标签类:
解析:从该类中可以看出该标签类中有2个属性
在这里插入图片描述

package cn.tx.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import cn.tx.utils.ERPConstants;
public class ERPOrderStateTag extends TagSupport {
	//设置订单类型值
		private String orderType;	
		private String orderState;
		public String getOrderState() {
			return orderState;
		}
	public void setOrderState(String orderState) {
			this.orderState = orderState;
		}
	public String getOrderType() {
			return orderType;
		}
public void setOrderType(String orderType) {
			this.orderType = orderType;
		}


@Override
	public int doStartTag() throws JspException {
		//获得JSPWriter对象向jsp页面写文本
		JspWriter out = pageContext.getOut();
		String text = "";
		switch(orderType){
			case ERPConstants.ORDER_TYPE_BUY:
				switch(orderState){
					case ERPConstants.ORDER_TYPE_BUY_AUDIT:
						text = ERPConstants.ORDER_TYPE_BUY_AUDIT_TEXT;
						break;
					case ERPConstants.ORDER_TYPE_BUY_AUDIT_REJECT:
						text = ERPConstants.ORDER_TYPE_BUY_AUDIT_REJECT_TEXT;
						break;
					case ERPConstants.ORDER_TYPE_BUY_AUDIT_PASS:
						text = ERPConstants.ORDER_TYPE_BUY_AUDIT_PASS_TEXT;
						break;
				}
				break;
			case ERPConstants.ORDER_TYPE_TRANS:
				
				switch(orderState){
					case ERPConstants.ORDER_TYPE_TRANS_BUY:
						text = ERPConstants.ORDER_TYPE_TRANS_BUY_TEXT;
						break;
					case ERPConstants.ORDER_TYPE_TRANS_ASSGIN:
						text = ERPConstants.ORDER_TYPE_TRANS_ASSGIN_TEXT;
						break;
					case ERPConstants.ORDER_TYPE_TRANS_BUYING:
						text = ERPConstants.ORDER_TYPE_TRANS_BUYING_TEXT;
						break;
				}
				break;
			case ERPConstants.ORDER_TYPE_INSTOCK:
				switch(orderState){
					case ERPConstants.ORDER_TYPE_INSTOCK_WAIT:
						text = ERPConstants.ORDER_TYPE_INSTOCK_WAIT_TEXT;
						break;
					case ERPConstants.ORDER_TYPE_INSTOCK_INING:
						text = ERPConstants.ORDER_TYPE_INSTOCK_INING_TEXT;
						break;
					case ERPConstants.ORDER_TYPE_INSTOCK_FINISH:
						text = ERPConstants.ORDER_TYPE_INSTOCK_FINISH_TEXT;
						break;
				}
				
				break;
			case ERPConstants.ORDER_TYPE_SALES:
				text = ERPConstants.ORDER_TYPE_SALES_TEXT;
				break;
		}
		try {
			out.write(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
}

解析:
在这里插入图片描述

实际在jsp中使用如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java全栈研发大联盟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值