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

2 篇文章 0 订阅
2 篇文章 0 订阅

Foreach标签

1、继承助手类

public class ForeachTag extends BodyTagSupport {
}

2、开始编码

private List<Object> item;
	private String var;
	
	
	public List<Object> getItem() {
		return item;
	}
	public void setItem(List<Object> 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<Object> 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<Object> 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("<select id='' name=''>");
		
		if(!(headerTextKey==null||"".equals(headerTextVal)||headerTextVal==null||"".equals(headerTextVal))) {
			sb.append("<option selected value='"+headerTextKey+"'>"+headerTextVal+"</option>");
		}
		String value;
		String html;
		for (Object obj : items) {
			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(value.equals(selectedVal)) {
				sb.append("<option selected value='"+value+"'>"+html+"</option>");
			}
			else {
				sb.append("<option value='"+value+"'>"+html+"</option>");
			}
		}
		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;
	}
	public SelectTag(String id, String name, List<Object> items, String textKey, String textVal, String selectedVal,
			String headerTextKey, String headerTextVal) {
		super();
		this.id = id;
		this.name = name;
		this.items = items;
		this.textKey = textKey;
		this.textVal = textVal;
		this.selectedVal = selectedVal;
		this.headerTextKey = headerTextKey;
		this.headerTextVal = headerTextVal;
	}
	public SelectTag() {
		super();
	}
	
	
	
	

Test标签

1、继承助手类

public class TestTag extends BodyTagSupport{
}

2、开始编码

/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String name;
	
	public String getName() {
		return name;
	}




	public void setName(String name) {
		this.name = name;
	}

	public TestTag() {
		super();
	}

	public TestTag(String name) {
		super();
		this.name = name;
	}



	public static void main(String[] args) {
		
	}
	
	
	/**
	 * doStartTag  执行到开始标签时执行的动作
	 * 执行到<z:test>开始标签位置时对应执行的动作  doStartTag
	 * 2个返回值
	 * SKIP_BODY 跳过主体内容
	 * EVAL_BODY_INCLUDE 计算页面主体内同并包含再输出
	 * 
	 */
	@Override
	public int doStartTag() throws JspException {
	System.out.println("-------------doStartTag-------------");
		return EVAL_BODY_INCLUDE;
	}
	
	
	/**
	 * doAfterBody
	 * 介于<z:test>内容</z:test>之间执行的动作
	 * 2个返回值
	 * EVAL_BODY_AGAIN 再次计算主体内容并输出
	 * SKIP_BODY 跳过主体内容
	 * 
	 */
	@Override
	public int doAfterBody() throws JspException {
		System.out.println("-------------doAfterBody-------------");
		return SKIP_BODY;
	}
	
	/**
	 * doEndTag  执行到结束标签时执行的动作
	 * 执行</c:test>结束标签位置时对应执行的动作  doEndTag
	 * 2个返回值
	 * SKIP_PAGE  跳过页面的后续内容
	 * EVAL_PAGE  计算页面的后续内容
	 * 
	 * 
	 */
	@Override
	public int doEndTag() throws JspException {
		System.out.println("-------------doEndTag---------------");
		return SKIP_PAGE;
	}
	

Checkbox标签

1、继承助手类

public class CheckboxTag extends BodyTagSupport {
}

2、开始编码

    private String textKey;
	private String textVal;
	private List<Object> checkedVal = new ArrayList<>();
	private List<Object> item = new ArrayList<>();
	
	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 List<Object> getCheckedVal() {
		return checkedVal;
	}
	public void setCheckedVal(List<Object> checkedVal) {
		this.checkedVal = checkedVal;
	}
	public List<Object> getItem() {
		return item;
	}
	public void setItem(List<Object> item) {
		this.item = item;
	}
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		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 super.doStartTag();
	}
	private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		String value;
		String html;
		for (Object obj : item) {
			Field field = obj.getClass().getDeclaredField(textKey);
			field.setAccessible(true);
			value = (String) field.get(obj);
			Field field2 = obj.getClass().getDeclaredField(textVal);
			field2.setAccessible(true);
			html = (String) field2.get(obj);
			if(checkedVal.contains(value)) {
				sb.append("<input checked type='checkbox' value='"+value+"' />"+html+"");
			}else {
				sb.append("<input type='checkbox' value='"+value+"' />"+html+"");
			}
		}
		return sb.toString();
	}


配置文件

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
    
  <description>wyy 1.1 core library</description>
  <display-name>wyy core</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>w</short-name>
  <uri>/wyy</uri><!-- 这是你引入标签库的语句 -->


  <tag>
 	 <!-- 标签名 -->
    <name>test</name>
    <!-- 标签工具类 -->
    <tag-class>com.wyy.taglib.TestTag</tag-class>
    <!-- 标签的内容类型:empty表示空标签,jsp表示可以为任何合法的JSP元素 -->
    <body-content>JSP</body-content>
    
    <!-- 自定义标签的属性定义,请注意一定要在标签类中提供对应的get/set方法 -->
    <attribute>
    <!-- 自定义标签的属性名称 -->
        <name>name</name>
        <!-- true表示必填 -->
        <required>true</required>
        <!-- true支持动态值,可以向值里面填jsp表达式、EL表达式,false则不支持 -->
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
  
    <tag>
    <!-- 标签名 -->
    <name>if</name>
    <!-- 标签工具类 -->
    <tag-class>com.wyy.taglib.IfTag</tag-class>
    <!-- 标签的内容类型:empty表示空标签,jsp表示可以为任何合法的JSP元素 -->
    <body-content>JSP</body-content>
    
    <!-- 自定义标签的属性定义,请注意一定要在标签类中提供对应的get/set方法 -->
    <attribute>
    <!-- 自定义标签的属性名称 -->
        <name>test</name>
        <!-- true表示必填 -->
        <required>true</required>
        <!-- true支持动态值,可以向值里面填jsp表达式、EL表达式,false则不支持 -->
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
  
  
  <tag>
    <name>out</name>
    <tag-class>com.wyy.taglib.OutTag</tag-class>
    <body-content>JSP</body-content>
    
    <attribute>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

	<tag>
    <name>foreach</name>
    <tag-class>com.wyy.taglib.ForeachTag</tag-class>
    <body-content>JSP</body-content>
    
    <attribute>
        <name>item</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
     <attribute>
        <name>var</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
  
  <tag>
    <name>select</name>
    <tag-class>com.wyy.taglib.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>
  
  
    <tag>
    <name>checkbox</name>
    <tag-class>com.wyy.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>
 

</taglib>

界面调用

<body>
<%
	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);
%>
	
	
	<z:select selectedVal="003" headerTextKey="-1" headerTextVal="---请选择---" textVal="name" items="${ls}" textKey="id"></z:select>
	<z:checkbox textVal="name" item="${ls2}" textKey="id" checkedVal="${ls}"></z:checkbox>
	
</body>

效果显示

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值