自定义标签(二)

本文介绍了一种自定义JSP标签库的方法,包括UI标签如out、set和select,以及控制标签如if和forEach的实现。通过具体示例展示了如何创建、配置和使用这些标签,以增强网页的动态性和交互性。
摘要由CSDN通过智能技术生成

1.自定义标签

1.UI标签
2.控制标签

UI标签

1.out标签

package day02;

import java.io.IOException;

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

public class OutTag extends BodyTagSupport {
	
	private static final long serialVersionUID = -6799569790010764952L;
	private Object value;
	
	public Object getValue() {
		return value;
	}
	public void setValue(Object value) {
		this.value = value;
	}
	
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
//		打印
			out.print(value.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
}

tld标签库

<tag>
  <!-- 标签库的标签名 -->
    <name>out</name>
    <!-- 标签对应的助手类的全路径名 -->
    <tag-class>day02.OutTag</tag-class>
    <!-- JSP -->
    <body-content>JSP</body-content>
    <attribute>
    <!-- 属性名 -->
        <name>value</name>
        <!-- 属性是否必填 -->
        <required>true</required>
        <!-- 是否支持表达式 -->
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
  

2.set标签

package day02;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class SetTag extends BodyTagSupport {

	private static final long serialVersionUID = -4049322046477208654L;
	
	private String var;
	private Object value;
	
	@Override
	public int doStartTag() throws JspException {
		pageContext.setAttribute(var, value);
		
		return SKIP_BODY;
	}
	
	public String getVar() {
		return var;
	}
	public void setVar(String var) {
		this.var = var;
	}
	public Object getValue() {
		return value;
	}
	public void setValue(Object values) {
		this.value = values;
	}
	
	
}

tld标签库

<tag>
  <!-- 标签库的标签名 -->
    <name>set</name>
    <!-- 标签对应的助手类的全路径名 -->
    <tag-class>day02.SetTag</tag-class>
    <!-- JSP -->
    <body-content>JSP</body-content>
    <attribute>
    <!-- 属性名 -->
        <name>value</name>
        <!-- 属性是否必填 -->
        <required>true</required>
        <!-- 是否支持表达式 -->
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
    <!-- 属性名 -->
        <name>var</name>
        <!-- 属性是否必填 -->
        <required>true</required>
        <!-- 是否支持表达式 -->
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

3.select标签

package day02;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

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

import org.apache.commons.beanutils.PropertyUtils;

public class SelectTag extends BodyTagSupport {

	private static final long serialVersionUID = -4852313144224508998L;
		private String id;
		private String name;
		private List<Object> items = new ArrayList<Object>();
		private String textKey;    //textKey=id
		private String selectedVal;
		private String headerTextKey;
		private String headerTextVal;
		private String textVal;
		
		@Override
		public int doStartTag() throws JspException {
			JspWriter out = pageContext.getOut();
				try {
					out.print(toHTML());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			return super.doStartTag();
		}
		
		
		private String toHTML() throws Exception {
			StringBuffer sb = new StringBuffer();
			sb.append("<select id='"+id+"' name='"+name+"'>");//初始化
//			判断不能为空
			if(!(headerTextKey==null || "".equals(headerTextKey)||headerTextVal==null||"".equals(headerTextVal))) {
				sb.append("<option selected value='"+headerTextKey+"'>"+headerTextVal+"</option>");
			}
			String value;
			String html;
			for (Object obj : items) {
//				反射获取下拉框的显示值,数据值
				Field textKeyfield = obj.getClass().getDeclaredField(textKey);
				textKeyfield.setAccessible(true);
				value = (String) textKeyfield.get(obj);
				html = (String) PropertyUtils.getProperty(obj, textVal); 
//				设置默认选中
				/*if(value.equals(selectedVal)) {
					sb.append("<option selected value='"+value+"'>"+html+"</option>");
				}
				else {
					sb.append("<option value='"+value+"'>"+html+"</option>");
				}*/
	
//				sb.append("<option value='"+value+"'>"+html+"</option>");
			}
			sb.append("</select>");
			return sb.toString();
		}

	
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public List<Object> getItems() {
			return items;
		}
		public void setItems(List<Object> items) {
			this.items = items;
		}
		public String getTextKey() {
			return textKey;
		}
		public void setTextKey(String textKey) {
			this.textKey = textKey;
		}
		public String getSelectedVal() {
			return selectedVal;
		}
		public void setSelectedVal(String selectedVal) {
			this.selectedVal = selectedVal;
		}
		public String getHeaderTextKey() {
			return headerTextKey;
		}
		public void setHeaderTextKey(String headerTextKey) {
			this.headerTextKey = headerTextKey;
		}
		public String getHeaderTextVal() {
			return headerTextVal;
		}
		public void setHeaderTextVal(String headerTextVal) {
			this.headerTextVal = headerTextVal;
		}
		
		
		
}

1.值得传递 id.name
2.数据源 items
3.展示列与数据存贮列与实体类的对应关系 textKey textVal
4.数据会显 selectedtVal
5.可能下拉框有默认值(头标签) headerTextKey , headerTextVal

2.控制标签

1.if
2.forEach

if标签

package day02;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class IfTag extends BodyTagSupport {
	
	private boolean test;

	public boolean isTest() {
		return test;
	}

	public void setTest(boolean test) {
		this.test = test;
	}
	
	@Override
	public int doStartTag() throws JspException {
		return test  ?	EVAL_BODY_INCLUDE	:	SKIP_BODY;
	}

}

自己写的方便自己调用

2.forEach

package day02;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class ForeachTag extends BodyTagSupport {
	
	private static final long serialVersionUID = -9011198067061824818L;

	private String var;
	private List<Object> items = new ArrayList<>();
	
	/**
	 * 执行完这个方法,var所代表的指针一定要向下移动一位;
	 */
	@Override
	public int doStartTag() throws JspException {
		if(items.size()==0) {
			return SKIP_BODY;
		}
		else {
			Iterator<Object> it = items.iterator();
			pageContext.setAttribute(var, it.next());
			pageContext.setAttribute("it", it);
			return EVAL_BODY_INCLUDE;
		}
		
	}
	
	@Override
	public int doAfterBody() throws JspException {
		Iterable<Object> it = (Iterable<Object>) pageContext.getAttribute("it");
		if(((Iterator<Object>) it).hasNext()) {
			pageContext.setAttribute(var, ((Iterator<Object>) it).next());
			pageContext.setAttribute("it", it);
			return EVAL_BODY_AGAIN;
		}
		return EVAL_PAGE;
	}
	
	public String getVar() {
		return var;
	}
	public void setVar(String var) {
		this.var = var;
	}
	public List<Object> getItems() {
		return items;
	}
	public void setItems(List<Object> items) {
		this.items = items;
	}
	
	
}

3.checkebox标签

1.建立一个类

package day03;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

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

public class checkeboxTag  extends BodyTagSupport{
	
	private static final long serialVersionUID = 3355414999452130655L;
	
	private String textKey;
	private String textVal;
	private List<Object> checkedVal=new ArrayList<>();
	private List<Object> item=new ArrayList<>();
	
	public List<Object> getItem() {
		return item;
	}
	public void setItem(List<Object> item) {
		this.item = item;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTextKey() {
		return textKey;
	}
	public List<Object> getCheckedVal() {
		return checkedVal;
	}
	public void setCheckedVal(List<Object> checkedVal) {
		this.checkedVal = checkedVal;
	}
	public void setTextKey(String textKey) {
		this.textKey = textKey;
	}
	public String getTextVal() {
		return textVal;
	}
	public void setTextVal(String textVal) {
		this.textVal = textVal;
	}
	
	public checkeboxTag() {
		super();
	}	
	
	public checkeboxTag(String textKey, String textVal, List<Object> checkedVal, List<Object> item) {
		super();
		this.textKey = textKey;
		this.textVal = textVal;
		this.checkedVal = checkedVal;
		this.item = item;
	}
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.print(toHTML());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	private String toHTML() throws Exception {
		StringBuffer sb=new StringBuffer();
		String value;
		String html;
		for (Object obj : item) {
//			用放射获取属性值
			Field textKeyfield = obj.getClass().getDeclaredField(textKey);
			textKeyfield.setAccessible(true);
			value=(String)textKeyfield.get(obj);
			Field textValfield = obj.getClass().getDeclaredField(textVal);
			textValfield.setAccessible(true);
			html=(String)textValfield.get(obj);
			if(checkedVal.contains(value)) {//判断集合是否包含value,包含设置选择
				sb.append("<input checked type='checkbox' value='"+value+"' />"+html+"");
			}
			else {
				sb.append("<input type='checkbox' value='"+value+"' />"+html+"");
			}
		}
		return sb.toString();
	}
	
}


2.配置

 <name>checkbox</name>
    <tag-class>day03.checkeboxTag</tag-class>
    <body-content>JSP</body-content>
    
     <attribute>
        <name>textKey</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
     </attribute>
     <attribute>
        <name>textVal</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
     </attribute>
      <attribute>
        <name>item</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
     </attribute>
     <attribute>
        <name>checkedVal</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
     </attribute>

3.开始测试

<%
	List aa=new ArrayList();
	aa.add(new Student("1","aa"));
	aa.add(new Student("2","bb"));
	aa.add(new Student("3","cc"));
	//设置默认选中
	List bb=new ArrayList();
	bb.add("1");
	//bb.add("2");
	bb.add("3");
	request.setAttribute("aa", aa);
	request.setAttribute("bb", bb);
	%>
	<z:checkbox textVal="name" checkedVal="${bb}" item="${aa}" textKey="id"></z:checkbox> 

运行结果:输出aa和cc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值