自定义标签

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




    
  erp  library     
  erp core
  1.1
  e
  http://txjava/jsp/erp/core



  
   
  
    orderTypetext
    cn.tx.tag.ERPOrderTypeTag
    JSP
    
       	
        orderType
        true
        true
    
  

  
   
  
    orderStatetext
    cn.tx.tag.ERPOrderStateTag
    JSP
    
       	
        orderType
        true
        true
    
    
    
       	
        orderState
        true
        true
    
  


1.解析:
自定义标签_第1张图片
自定义标签_第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 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();
	}
}

解析:
自定义标签_第3张图片

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

你可能感兴趣的:(自定义标签)