JSP自定义标签库的七大常用标签

文章目录

    • Foreach标签
    • If标签
    • Out标签
    • Set标签
    • Select标签
    • Test标签
    • Checkbox标签
    • 配置文件
    • 界面调用
    • 效果显示

Foreach标签

1、继承助手类

public class ForeachTag extends BodyTagSupport {
}

2、开始编码

private List item;
	private String var;
	
	
	public List getItem() {
		return item;
	}
	public void setItem(List item) {
		this.item = item;
	}
	public String getVar() {
		return var;
	}
	public void setVar(String var) {
		this.var = var;
	}
	
	@Override
	public int doStartTag() throws JspException {
		//判断传入集合是否为空
		if(null==item||0==item.size()) {
			return SKIP_BODY;
		}
		else {
			Iterator it = item.iterator();
			JspWriter out = pageContext.getOut();
			//获取第一个元素
			Object next = it.next();
			//保存到作用域中,以var命名,前台用var来取集合的值
			pageContext.setAttribute(var, next);
			//将获取了一个元素的迭代器保存到Page作用域中
			pageContext.setAttribute("it", it);
			try {
				out.print(next);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doAfterBody() throws JspException {
		Iterator it =(Iterator)pageContext.getAttribute("it");
		//在doAfterBody里面,不需要用while
		if(it.hasNext()) {
			Object next = it.next();
			//保存到作用域中
			pageContext.setAttribute(var, next);
			//将迭代器保存到page中,等到下一次doAfterBody中再次取出遍历,遍历到没有元素为止
			pageContext.setAttribute("it", it);
			return EVAL_BODY_AGAIN;
		}
		return SKIP_BODY;
	}
 
  

If标签

1、继承助手类

public class IfTag extends BodyTagSupport{
}

2、开始编码

private boolean test;
	
	//传入一个参数,这是一个判断的结果
	public boolean isTest() {
		return test;
	}

	public void setTest(boolean test) {
		this.test = test;
	}

	@Override
	public int doStartTag() throws JspException {
		if(test) {
			return EVAL_BODY_INCLUDE;
		}
		else {
			return SKIP_BODY;
		}
	}
	
	@Override
	public int doAfterBody() throws JspException {
		return SKIP_BODY;
	}
	
	@Override
	public int doEndTag() throws JspException {
		System.out.println("-------------doEndTag-------------");
		return EVAL_BODY_AGAIN;
	}

Out标签

1、继承助手类

public class OutTag extends BodyTagSupport {
}

2、开始编码

private String value;

	public String getValue() {
		return value;
	}

	public void setValue(String 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 EVAL_BODY_INCLUDE;
	}

Set标签

1、继承助手类

同Out可得,与Out的原理一样

2、开始编码

同Out可得,与Out的原理一样、代码及其类似

Select标签

1、继承助手类

/**
 * 1、值的传递     id、name
 * 2、数据源		items
 * 3、展示列与数据存储列与实体类的对应关系		textKey  textVal
 * 4、数据回显      selectedVal
 * 5、下拉框可能有默认值  headerTextKey  headerTextVal
 * 
 * 
 * 
 *
 */
public class SelectTag extends BodyTagSupport{
}

2、开始编码

private String id;
	private String name;
	private List items=new ArrayList<>();
	private String textKey;  //id=textKey
	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 (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	
	
	
//	
	



	private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		StringBuffer sb=new StringBuffer();
		sb.append(""+html+"");
			}else {
				sb.append(""+html+"");
			}
		}
		return sb.toString();
	}


 
  

配置文件


    
  wyy 1.1 core library
  wyy core
  1.1
  w
  /wyy


  
 	 
    test
    
    com.wyy.taglib.TestTag
    
    JSP
    
    
    
    
        name
        
        true
        
        false
    
  
  
    
    
    if
    
    com.wyy.taglib.IfTag
    
    JSP
    
    
    
    
        test
        
        true
        
        true
    
  
  
  
  
    out
    com.wyy.taglib.OutTag
    JSP
    
    
        value
        true
        true
    
  

	
    foreach
    com.wyy.taglib.ForeachTag
    JSP
    
    
        item
        true
        true
    
     
        var
        true
        false
    
  
  
  
    select
    com.wyy.taglib.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
     
  
  
  
    
    checkbox
    com.wyy.taglib.CheckboxTag
    JSP
    
    
	        textKey
	        true
	        true
	    
	    
	        textVal
	        true
	        true
	    
	    
	        item
	        true
	        true
	    
	    
	        checkedVal
	        false
	        true
	    
  	
 


界面调用


<%
	List ls=new ArrayList();
	ls.add(new Student("001","aaaa"));
	ls.add(new Student("002","bbbb"));
	ls.add(new Student("003","cccc"));
	request.setAttribute("ls", ls);
	
	List ls2=new ArrayList();
	ls2.add(new Student("001","aaaa"));
	ls2.add(new Student("002","bbbb"));
	ls2.add(new Student("003","cccc"));
	request.setAttribute("ls2", ls2);
%>
	
	
	
	
	

效果显示

JSP自定义标签库的七大常用标签_第1张图片

你可能感兴趣的:(jsp标签,标签,jsp)