自定义标签2

自定义标签2

Set,Out标签的开发
if标签的开发
foreach标签的开发
select标签的开发
Set,Out标签的开发
CheckeboxTag标签的开发

1.助手类

package com.dl.jsp2;

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

public class SetTag extends BodyTagSupport{

	private static final long serialVersionUID = 1L;

private String var;
private Object value;
public String getVar() {
	return var;
}
public void setVar(String var) {
	this.var = var;
}
public Object getValue() {
	return value;
}
public void setValue(Object value) {
	this.value = value;
}

@Override
public int doStartTag() throws JspException {
	pageContext.setAttribute(var, value);//将值存入pageContext


	return SKIP_BODY;
	}
}

package com.dl.jsp2;

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 = 8655118409172739205L;

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);//输出值
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();

		}
		return SKIP_BODY;
	}
	
}

2.配置tld文件

<tag>
    <name>set</name>
    <tag-class>com.Dl.jsp2.SetTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
    	<name>var</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    	<name>value</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <name>out</name>
    <tag-class>com.shl.jsp2.OutTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
    	<name>value</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

if标签的开发

1.if只有test一个属性值,定义它们助手类
private boolean test;
public boolean isTest() {
	return test;
}
public void setTest(boolean test) {
	this.test = test;
}
@Override
public int doStartTag() throws JspException {
	//如果test为true,则进入doAfterBody方法,如果为false,则直接进入doEndTag方法
	return test?EVAL_BODY_INCLUDE:SKIP_BODY;
}

2.配置tld文件

 <tag>
    <name>if</name>
    <tag-class>com.dl.jsp2.IfTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
    	<name>test</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

checkbox标签

配置描述文件

 <tag>
    <name>checkbox</name>
    <tag-class>com.xy.taglib.CheckboxTag</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>
     
  </tag>

CheckeboxTag助手类

public class CheckboxTag extends BodyTagSupport {
	
	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 CheckboxTag() {
		super();
	}
	
	public CheckboxTag(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();
		}
		return super.doStartTag();
	}
	private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		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();
	}
}

代码验证复选框标签是否正确

<%
	List ls=new ArrayList();
	ls.add(new Student("001","aa"));
	ls.add(new Student("002","bb"));
	ls.add(new Student("003","cc"));
	List ls1=new ArrayList();
	ls1.add("001");
	ls1.add("002");
	request.setAttribute("ls", ls);
	request.setAttribute("ls1", ls1);
	
%>
	
	<z:checkbox textVal="name" checkedVal="${ls1}" item="${ls}" textKey="id"></z:checkbox> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值