jsp标签2

我们知道自定义标签开发步骤离不开三个东西
1.助手类
2.tld
3.taglib
分析:
在c标签大致分为UI标签,控制标签,数据标签

ui标签

就是我们展示在界面的内容,用ui标签也就是不需要写标签体的标签又能展示内容的就是ui标签

控制标签

我们看到控制就可以判断,他是会控制文本,标签等的一类,包括是否展示,条件的改变等。

数据标签

没有标签体,也没有任何判断,用途:存储数据(直观),比如set标签。
接下来,我们主要来探析以下foreach标签和select 标签的开发。

foreach标签

按照步骤来
foreach只有var和items两个属性值,这时候我们就可以定义助手类

package com.ly.jsp;

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

	private String var;
	private List<Object> items=new ArrayList<>();
	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;
	}
	
	/**
	 * 执行完这个方法的时候,var代表的指针要向下移动一位
	 */
	@Override
	public int doStartTag() throws JspException {
		//如果items为空,直接进入doEndTag方法
		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;//进入doAfterBody()方法
		}
	}
	
	@Override
	public int doAfterBody() throws JspException {
		Iterator<Object> it = (Iterator<Object>) pageContext.getAttribute("it");
		if(it.hasNext()) {//如果有下一个值
			pageContext.setAttribute(var, it.next());
			pageContext.setAttribute("it", it);
			return EVAL_BODY_AGAIN;
		}
		return EVAL_PAGE;
	}
}


第二步:配置tld文件

 <tag>
    <name>foreach</name>
    <tag-class>com.ly.jsp.ForeachTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
    	<name>var</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    	<name>items</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

<%
	List li=new ArrayList();
	li.add(new Student("1","a"));
	li.add(new Student("2","aa"));
	li.add(new Student("3","aaa"));
	request.setAttribute("stu", li);
%>

<s:foreach items="${stu }" var="stu">
	${stu.id }:${stu.name }<br>
</s:foreach>

在这里插入图片描述

select标签

package com.ly.jsp;

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.数据源    items    (可不写)
 * 3.展示列与数据存储列与实体类的对应关系    textKey  textVal  
 * 		<option value="1">湖南</option>
 * 4.数据回显  selectedVal   (可不写)
 * 5.可能下拉框有默认值(头标签)   headerTextKey  headerTextVal (可不写)
 * @author Administrator
 *
 */
public class SelectTag extends BodyTagSupport {

	private static final long serialVersionUID = 1L;

	private String id;
	private String name;
	private List<Object> items=new ArrayList<>();
	private String textKey;
	private String textVal;
	private String selectedVal;
	private String headerTextKey;
	private String headerTextVal;
	
	
	@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 (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	
	//将值都用字符串拼接起来
	private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		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;
		//遍历集合,将值加入option中
		for (Object obj: items) {
			//第一种方式     反射
//			Field textKeyField = obj.getClass().getDeclaredField(textKey);
//			textKeyField.setAccessible(true);
//			value=(String) textKeyField.get(obj);
			
			//第二种    导jar包
			value=(String) PropertyUtils.getProperty(obj, textKey);
			
			html=(String) PropertyUtils.getProperty(obj, textVal);
			
			if(value.equals(selectedVal)) {
				sb.append("<option selected 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 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;
	}

	
}



tld配置

<tag>
    <name>select</name>
    <tag-class>com.ly.jsp.SelectTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
    	<name>id</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    	<name>name</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    	<name>items</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
    	<name>textKey</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    	<name>textVal</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    	<name>selectedVal</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
    	<name>headerTextKey</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    	<name>headerTextVal</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

<%
	List li=new ArrayList();
	li.add(new Student("1","a"));
	li.add(new Student("2","aa"));
	li.add(new Student("3","aaa"));
	request.setAttribute("stu", li);
%>

<s:select headerTextKey="-1" headerTextVal="请选择" textVal="name" items="${stu }" selectedVal="222" textKey="id"></s:select>

checkbox标签

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

tld配置

checkbox
com.ly.taglib.CheckboxTag
JSP

 <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>
测试
<%
	List ls=new ArrayList();
	ls.add(new Student("1","a"));
	ls.add(new Student("2","aa"));
	ls.add(new Student("3","aaa"));
	List list=new ArrayList();
	list.add("1");
	list.add("2");
	request.setAttribute("ls", ls);
	request.setAttribute("list", list);
	
%>
	


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值