自定义标签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文件


    set
    com.Dl.jsp2.SetTag
    JSP
    
    	var
        true
        false
    
    
    	value
        true
        true
    
  

  
    out
    com.shl.jsp2.OutTag
    JSP
    
    	value
        true
        true
    
  

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文件

 
    if
    com.dl.jsp2.IfTag
    JSP
    
    	test
        true
        true
    
  

checkbox标签

配置描述文件

 
    checkbox
    com.xy.taglib.CheckboxTag
    JSP
    
     
        textKey
        true
        true
     
     
        textVal
        true
        true
     
      
        item
        true
        true
     
     
        checkedVal
        false
        true
     
     
  

CheckeboxTag助手类

public class CheckboxTag extends BodyTagSupport {
	
	private String textKey;//传入值
	private String textVal;//显示值
	private List checkedVal=new ArrayList<>();//回显数据集合
	private List item=new ArrayList<>();//数据集合
	
	public List getItem() {
		return item;
	}
	public void setItem(List item) {
		this.item = item;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	public String getTextKey() {
		return textKey;
	}
	public List getCheckedVal() {
		return checkedVal;
	}
	public void setCheckedVal(List 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 checkedVal, List 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(""+html+"");
			}
			else {
				sb.append(""+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);
	
%>
	
	 

你可能感兴趣的:(学习)