自定义jsp案例

set设置数据标签助手类

package com.jsp;

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

public class SetTag extends BodyTagSupport {

	private static final long serialVersionUID = 4965191755704898746L;

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

out输出数据标签助手类

package com.jsp;

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 = 6097318304486137249L;
	
	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 (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	
}

foreach遍历输出数据标签助手类

  • foreach输出需要遍历一个实体类
package com.jsp;

public class Student {
	private String id;
	private String name;
	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 Student(String id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Student() {
	}
}
package com.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 = -2643407962233076616L;
	
	private String var;
	private List<Object> items =new  ArrayList<>();

	/**
	 * 执行完这个方法的时候 ,var所代表的指针一定向下移动一位;
	 */
	
	@Override
	public int doStartTag() throws JspException {
		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;
		}
	}
	
	@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;
	}
	
	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;
	}	
}

select下拉框数据标签助手类

package com.jsp;

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、展示列与数据存储列与实体类对应关系  textKey textVal
 * 4、数据回显 selectedVal
 * 5、可能下拉框默认值(头标签) headerTextKey  headerTextVal 
 * @author 林凡
 *
 */

public class SelectTag extends BodyTagSupport {
	private static final long serialVersionUID = 5375902073989817712L;
	private String id;
	private String name;
	private List<Object> items =new ArrayList<Object>();
	private String 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 {
			out.print(toHTML()); 
		} catch (Exception e) { 
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	 
	
	
	private String toHTML() throws NoSuchFieldException, SecurityException {
		StringBuffer sb =new StringBuffer();
		sb.append("<select id='"+id+"' name='"+name+"'>");
		//判断头标签不能为空
		if(!(headerTextKey ==null || "".equals(headerTextKey)|| 
				headerTextVal ==null ||"".equals(headerTextVal) )) {
				sb.append("<option checked  value='"+headerTextKey+"'>"+headerTextVal+"</option>");
			
		}
		String value = null;
		String html = null;
		for (Object obj : items) {

			//利用反射获取类对象的属性,也就是我们下拉框的显示值和数据值
			Field field = obj.getClass().getDeclaredField(textKey);
			field.setAccessible(true);
			try {
				value = (String) field.get(obj);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				html =(String) PropertyUtils.getProperty(obj,textVal);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//设置回显,设置的值和你下拉框的值相同就默认选中
			if(value.equals(selectedVal)) {
				sb.append("<option checked  value='"+value+"'>"+html+"</option>");
			}
			else {
				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;
	}
}

给标签配置标签名和属性名

<?xml version="1.0" encoding="UTF-8" ?>

<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>linfan 1.1 core library</description>
  <display-name>linfan core</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>c</short-name>
  <uri>/linfan</uri>
  
  <tag>
    <!-- 标签库中的标签名 -->
    <name>set</name>
    <!-- 标签对应的助手类的全路径名 -->
    <tag-class>com.jsp.SetTag</tag-class>
    <!-- jsp -->
    <body-content>JSP</body-content>
    <attribute>
        <!-- 属性名 -->
        <name>var</name>
        <!-- 属性值是否必填 -->
        <required>true</required>
        <!-- 是否支持表达试 -->
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <!-- 属性名 -->
        <name>value</name>
        <!-- 属性值是否必填 -->
        <required>true</required>
        <!-- 是否支持表达试 -->
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
   <tag>
    <!-- 标签库中的标签名 -->
    <name>out</name>
    <!-- 标签对应的助手类的全路径名 -->
    <tag-class>com.jsp.OutTag</tag-class>
    <!-- jsp -->
    <body-content>JSP</body-content>
    <attribute>
        <!-- 属性名 -->
        <name>value</name>
        <!-- 属性值是否必填 -->
        <required>true</required>
        <!-- 是否支持表达试 -->
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

	
<tag>
    <!-- 标签库中的标签名 -->
    <name>foreach</name>
    <!-- 标签对应的助手类的全路径名 -->
    <tag-class>  com.jsp.ForeachTag</tag-class>
    <!-- jsp -->
    <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>
  
  <tag>
    <!-- 标签库中的标签名 --> 
    <name>select</name>
    <!-- 标签对应的助手类的全路径名 -->
    <tag-class>com.jsp.SelectTag</tag-class>
    <!-- jsp -->
    <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>

  </taglib>

测试出数据

<%@page import="com.zking.jsp.day02.Student"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%>
    <%@ taglib uri="/linfan" prefix="z" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<<p>1.利用set设置值并且用out标签输出的值</p>
<z:set var="name" value="zhansan"></z:set>
<z:out value="${name }"></z:out>
<br>

<p>2.利用foreach遍历输出的值</p>
 	<%
		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); 
	
	%> 
	
	<z:foreach items="${stus }" var="stu">
		${stu.id },${stu.name }<br>
	</z:foreach>
	
	<p>3.利用select遍历的值</p>
<z:select   headerTextKey="-1" headerTextVal="===请选择===" textVal="name" items="${stus }" selectedVal="2" textKey="id"></z:select>
	
</body>
</html>
  • 输出为
    在这里插入图片描述

自定义一个checkbox标签助手类

public class CheckboxTag extends BodyTagSupport  {

	private static final long serialVersionUID = -5301514341025414346L;
	
	private String [] items;
	private String name;
	public String[] getItems() {
		return items;
	}
	public void setItems(String[] items) {
		this.items = items;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public CheckboxTag(String[] items, String name) {
		super();
		this.items = items;
		this.name = name;
	}
	public CheckboxTag() {
		super();
	}
	
	
	@Override
	public int doStartTag() throws JspException {
		JspWriter out =pageContext.getOut();
		try {
			out.print(toHTML());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return super.doStartTag();
	}
	private String toHTML() {
		StringBuffer sb =new StringBuffer();
		for (String it : items) {
			sb.append("<input type='checkbox' value ='"+it+"' name ='"+name+"' >"+it+"&nbsp;&nbsp;&nbsp;&nbsp;" );
		}
		return sb.toString();
	}
  • 给checkbox标签配置属性值
<tag>
    <name>checkbox</name>
    <tag-class>com.zking.jsp.day02.CheckboxTag</tag-class>
    <body-content>JSP</body-content>
    
    
      <attribute>
        <name>items</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
     </attribute>
     <attribute>
        <name>name</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
     </attribute>
  </tag>

  • 测试结果
<p>4.复选框</p>
	<% 
		String [] str ={"aaa","aaa","aaa"};
		request.setAttribute("str", str);
	%>
	<z:checkbox items="${str }" name="name"></z:checkbox>
  • 运行结果
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值