jsp自定义标签2

自定义标签2

自定义标签开发步骤

1.1 助手类
1.2 tld
1.3 taglib

助手类

package com.jsp.day01;

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;
	
	@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 value) {
		this.value = value;
	}
	
	

}

package com.jsp.day01;

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

	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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	
}

tld

   

    set

    com.jsp.day01.SetTag

    JSP
    

        var

        true

        false
    
    

        value

        true

        true
    
  
  
  
  
    

    out

    com.jsp.day01.OutTag

    JSP
    

        value

        true

        true
    
  

taglib

最后一步就是导入自己的标签库

<%@page import="com.jsp.day01.Student"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="z" uri="/zking" %>




Insert title here


	
		


select标签

助手类

package com.jsp.day01;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
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;


/**
 * 1.值的传递  id,name
 * 2.class(可要可不要)
 * 数据源   items
 * 3.展示列(用户可看的)与数据存(value)储列与实体类的对应关系 textKey textvalue
 * 4.数据回显 选中了需要回显 selectedVal
 * 5.可能下拉框有默认值(头标签)  headerTextKey headerTextVal
 * 
 * 
 * @author lqx
 *
 */
public class SelectTag extends BodyTagSupport {

	private static final long serialVersionUID = 4901181316672465145L;
	private String name;
	private String id;
	private List items = new ArrayList();
	private String textKey;// textKey=id
	private String textVal;
	private String selectedVal;
	private String headerTextKey;
	private String headerTextVal;
	
	
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			try {
				out.print(toHTML());
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	
	
	
	private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		StringBuffer sb = new StringBuffer();
		sb.append("");
		return sb.toString();
	}



	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public List getItems() {
		return items;
	}
	public void setItems(List items) {
		this.items = items;
	}
	public String getTextKey() {
		return textKey;
	}
	public void setTextKey(String textKey) {
		this.textKey = textKey;
	}
	public String getTextVal() {
		return textVal;
	}
	public void setTextVal(String textVal) {
		this.textVal = textVal;
	}
	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;
	}
	
	
	
	
	
	
	
	
	
	
}

 
  

配置

         

    select

    com.jsp.day01.SelectTag

    JSP
    

        id

        false

        false
    
      
    
        name

        false

        false
    
        

        items

        true

        true
    
      
    
        textKey

        true

        false
    
        

        textVal

        true

        false
    
      
    
        selectedVal

        false

        true
    
        

        headerTextKey

        false

        false
    
      
    
        headerTextVal

        false

        false
    
  

jsp页面展示

	<%
		List list = new ArrayList();
		list.add(new Student("001","xiaocheng"));
		list.add(new Student("002","xiaoli"));
		
		list.add(new Student("003","xiaowang"));
		request.setAttribute("stus", list);	 
		%>


checkbox标签

助手类

package com.jsp.day01;

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;

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();
	}

	
	
	
}

 
  

配置

 
    checkbox
    com.jsp.day01.CheckboxTag
    JSP
    
     
        textKey
        true
        true
     
     
        textVal
        true
        true
     
      
        item
        true
        true
     
     
        checkedVal
        false
        true
     
     
  

jsp页面展示

	<%
		List list = new ArrayList();
		list.add(new Student("001","xiaoaa"));
		list.add(new Student("002","xiaoaa"));
		list.add(new Student("003","xiaoaa"));
		List l = new ArrayList();
		l.add("001");
		l.add("002");
		request.setAttribute("list", list);
		request.setAttribute("l", l);
	%>


	

总的来说自定义标签就是先建立一个助手类,类里面定义自己需要的标签属性,
让后在自己的自定义标签库里面进行配置,最后在jsp页面进行使用,自定义标签就完成了,
它的生命周期也是在当前页面哦,一旦页面关
闭,它就生命周期就以为着结束
并且自定义标签的一些好处就是减少自己的一些代码量,并且可以按照自己的习惯方便自己编程!以上内容只限参考!

你可能感兴趣的:(JSP,JSP)