自定义jsp标签

 哈喽大家好~我来啦~~~~~今天和大家分享的是自定义jsp标签!!!

目录

一,foreach标签(1)var

(2) items

 二,z:select标签

(1)id

(2)name

(3)items

(4)textKey

(5)textVal

(6)headerTextKey

(7)headerTextVal

(8)selectedVal


一,foreach标签
(1)var

(2) items

ForeachTag类

 分析两个属性
  items里面放的是集合所以数据类型为list<object>
  var:String
  分析线路:
  第二条:eval_body_include
  第三条:eval_body_again

代码如下:

package com.zlp.tag;

import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * <c:forEach items="${clas12}" var="c"
 * 分析会有两个属性
 * items:;List<Object>
 * var:String
 * 分析线路:
 * 第二条:eval_body_include
 *第三条:eval_body_again
 * @author zjjt
 *
 */
public class ForeachTag extends BodyTagSupport {
private String var;
private List<Object> items;
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;
}
	@Override
		public int doStartTag() throws JspException {
		Iterator<Object> it = items.iterator();
		pageContext.setAttribute(var,it.next() );
		pageContext.setAttribute("it",it);//为了保留迭代时指针现有的位置

		return EVAL_BODY_INCLUDE;
		}
	@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;
		}else { 
			return EVAL_PAGE;
		}
	}
}

 然后建立一个实体类

package com.zlp.entity;
 
import java.io.Serializable;
 
/**
 * 实体类:教师类
 * @author zbb
 *2022年6月20日下午9:40:23
 */
public class Teacher implements Serializable{
	
	
	private static final long serialVersionUID = 1L;
	private String tid;
	private String name;
	public String getTid() {
		return tid;
	}
	public void setTid(String tid) {
		this.tid = tid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Teacher() {
		// TODO Auto-generated constructor stub
	}
	public Teacher(String tid, String name) {
		super();
		this.tid = tid;
		this.name = name;
	}
	
	
}

tld文件里自定义for标签 

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

下一步就要把属性放在界面中

<%@page import="java.util.ArrayList"%>
<%@page import="com.zlp.entity.Teacher"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://jsp.veryedu.cn" prefix="z" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <%
 	List<Teacher> ls = new ArrayList<Teacher>();
 	ls.add(new Teacher("t001","zs"));
 	ls.add(new Teacher("t002","ls"));
 	ls.add(new Teacher("t003","ww"));
	request.setAttribute("ls", ls);
 %>
<z:for items="${ls}" var="t">
	${t.tid }:${t.name}
</z:for>
</body>
</html>

运行结果如下:

 二,z:select标签

(1)id

(2)name

(3)items

(4)textKey

(5)textVal

(6)headerTextKey

(7)headerTextVal

(8)selectedVal

首先我们先来分析一下

1.后台要遍历--->数据源-->items
  2.需要一个对象的属性代表下拉框对应的展示内容-->textval
  3.需要一个对象的属性代表下拉框对应的value值--->textKey
  4.默认的头部选项展示内容--->headerTextVal
  5.默认的头部选项选值-->headTextVal
  6.数据中存储的值,为了方便做数据回显-->selectedVal
 * 
 * 以下三个条件可以省略不写
 * 7.id
 * 8.name
 * 9.cssStyle
 

下面我们来看看代码展示:

package com.zlp.tag;
 
import java.io.IOException;
import java.lang.reflect.Field;
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;
 
/**
 * 
 * @author zlp
 *
 * 时间:2022年6月20日下午9:44:23
 */
public class SelectTag extends BodyTagSupport{
 
	private List<Object> items;
	private String textVal;
	private String textKey;
	private String headerTextVal;
	private String headerTextKey;
	private String selectedVal;
	private String id;
	private String name;
	
	
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.print(toHTML());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	
	
	
	private String toHTML() throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("<select id='"+id+"' name='"+name+"' >");
		if(headerTextKey!=null&&!"".equals(headerTextKey)) {
			sb.append("<option value='"+headerTextKey+"'>"+headerTextVal+"</option>");
		}
		for (Object obj : items) {
//			<r:select textVal="tname" items="${list }" textKey="tid"></r:select>
			Field textKeyFiled = obj.getClass().getDeclaredField(textKey);
			textKeyFiled.setAccessible(true);
			Object value = textKeyFiled.get(obj);//真正下拉框展示值
			
//			System.out.println(selectedVal);
//			System.out.println(value);
//			int n = Integer.parseInt(selectedVal); 
			if(selectedVal != null && !"".equals(selectedVal) && selectedVal.equals(value.toString())) {
//			if(n != 0 && n>0 && n==value) {
				sb.append("<option selected value='"+value+"'>"+PropertyUtils.getProperty(obj, textVal)+"</option>");
 
			}
			else {
				sb.append("<option value='"+value+"'>"+PropertyUtils.getProperty(obj, textVal)+"</option>");
				
			}
			
		}
		sb.append("</select>");
		return sb.toString();
	}
 
 
 
	public List<Object> getItems() {
		return items;
	}
	public void setItems(List<Object> items) {
		this.items = items;
	}
	public String getTextVal() {
		return textVal;
	}
	public void setTextVal(String textVal) {
		this.textVal = textVal;
	}
	public String getTextKey() {
		return textKey;
	}
	public void setTextKey(String textKey) {
		this.textKey = textKey;
	}
	public String getHeaderTextVal() {
		return headerTextVal;
	}
	public void setHeaderTextVal(String headerTextVal) {
		this.headerTextVal = headerTextVal;
	}
	public String getHeaderTextKey() {
		return headerTextKey;
	}
	public void setHeaderTextKey(String headerTextKey) {
		this.headerTextKey = headerTextKey;
	}
	public String getSelectedVal() {
		return selectedVal;
	}
	public void setSelectedVal(String selectedVal) {
		this.selectedVal = selectedVal;
	}
	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 SelectTag() {
		// TODO Auto-generated constructor stub
	}
	public SelectTag(List<Object> items, String textVal, String textKey, String headerTextVal, String headerTextKey,
			String selectedVal, String id, String name) {
		this.items = items;
		this.textVal = textVal;
		this.textKey = textKey;
		this.headerTextVal = headerTextVal;
		this.headerTextKey = headerTextKey;
		this.selectedVal = selectedVal;
		this.id = id;
		this.name = name;
	}
	
	
	
}

 运行结果如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值