jsp标签(二)

jsp标签(forEach select)

案例:
(一)标签库源码

select:

package com.jiangjiayan.tag;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.swing.plaf.SliderUI;

import org.apache.commons.beanutils.PropertyUtils;

public class SelectTag extends BodyTagSupport {

	private List items;
	
	private String name;
	private String valueKey;//保存option中value的key
	private String textKey;//保存option中text文本的key
	
	private String Style;
	
	private String headKey;
	private String headValue;
	
	private String selectOptionValue;//默认选中
	
	
	
	public String getHeadValue() {
		return headValue;
	}

	public void setHeadValue(String headValue) {
		this.headValue = headValue;
	}

	public String getSelectOptionValue() {
		return selectOptionValue;
	}

	public void setSelectOptionValue(String selectOptionValue) {
		this.selectOptionValue = selectOptionValue;
	}

	public String getHeadKey() {
		return headKey;
	}

	public void setHeadKey(String headKey) {
		this.headKey = headKey;
	}

	public String getHeadvalue() {
		return headValue;
	}

	public void setHeadvalue(String headvalue) {
		this.headValue = headvalue;
	}

	public String getStyle() {
		return Style;
	}

	public void setStyle(String style) {
		Style = style;
	}

	public SelectTag() {
		// TODO Auto-generated constructor stub
	}

	public List getItems() {
		return items;
	}

	public void setItems(List items) {
		this.items = items;
	}

	public String getName() {
		return name;
	}

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

	public String getValueKey() {
		return valueKey;
	}

	public void setValueKey(String valueKey) {
		this.valueKey = valueKey;
	}

	public String getTextKey() {
		return textKey;
	}

	public void setTextKey(String textKey) {
		this.textKey = textKey;
	}
	
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			String html=toHTML();
			System.out.println(html);
			out.println(html);
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException();
		}
		return super.doStartTag();
	}
	
	private String  toHTML() throws Exception{
		StringBuffer sb=new StringBuffer();
		//拼接");
		//拼接
		if(null!=items&&0!=items.size()) {
			/*if(null!=headKey&&"".equals(headKey.trim())) {
				sb.append("");
			}*/
			
			Object value=null;
			Object text=null;
			for(Object obj:items) {
				//反射
				value=PropertyUtils.getProperty(obj,valueKey);
				text=PropertyUtils.getProperty(obj,textKey);
				System.out.println(selectOptionValue);
				System.out.println(value);
				/*if(value.toString().equals(selectOptionValue.trim())) {
					sb.append("");
				}else {*/
				sb.append("");
				/*}*/
			}
		}
		sb.append("");
		return sb.toString();
	}
	
}
 
  

forEach:

package com.jiangjiayan.tag;

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 List items;
	private String var;//保存集合中元素的key
	
	private String varStatus;//保存index,count属性的对象的key
	
	public static class Status{
		public int index=0;

		public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}
		
		public int getCount() {
			return this.index+1;
		}
		
		void increment() {
			this.index++;
		}
	}
	
	
	public String getVarStatus() {
		return varStatus;
	}


	public void setVarStatus(String varStatus) {
		this.varStatus = varStatus;
	}


	public List getItems() {
		return items;
	}


	public void setItems(List items) {
		this.items = items;
	}


	public String getVar() {
		return var;
	}


	public void setVar(String var) {
		this.var = var;
	}


	public ForEachTag() {
		// TODO Auto-generated constructor stub
	}
	
	
	@Override
	public int doAfterBody() throws JspException {
		Iterator it =(Iterator) pageContext.getAttribute("it");
		while(it.hasNext()) {
			if(null!=varStatus&&"".equals(varStatus)) {
				Status status =(Status) pageContext.getAttribute(varStatus);
				status.increment();
				pageContext.setAttribute(varStatus, status);
			}
			Object next = it.next();
			pageContext.setAttribute(var, next);
			return EVAL_BODY_AGAIN;
		}
		return super.doAfterBody();
	}
	
	@Override
	public int doStartTag() throws JspException {
		if(null!=varStatus&&!"".equals(varStatus)) {
			pageContext.setAttribute(varStatus, new Status());
		}
		
		if(null!=items&&0!=items.size()) {
			Iterator it = items.iterator();
			Object next=it.next();
			pageContext.setAttribute(var, next);
			pageContext.setAttribute("it", it);
			return EVAL_BODY_INCLUDE;
		}
		return super.doStartTag();
	}
}
 
  

(二)编写标签库描写文件



	JSTL 1.1 core library
	JSTL core
	1.1
	z
	/zking
	
	
    forEach
    com.jiangjiayan.tag.ForEachTag
    JSP
    
        items
        true
        true
     
    
        var
        true
        false
     
    
        varStatus
        true
        false
     
  
  
  
    select
    com.jiangjiayan.tag.SelectTag
    empty
    
        items
        true
        true
     
    
        name
        false
        false
     
    
        valueKey
        true
        false
     
    
        textKey
        true
        false
    
     

(三)在jsp页面使用自定义标签

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@taglib prefix="z" uri="/zking" %>




Insert title here


<%
	List list=new ArrayList();
	list.add("zs");
	list.add("ls");
	list.add("ww");
	String names="zs";
	pageContext.setAttribute("names", names);
	request.setAttribute("name", list);

%>

自定义标签之out标签

自定义标签之if标签

hahhha

自定义标签之forEach标签

<%--
  • ${s },${n.index },${n.count }
--%>
  • ${s },${n.index },${n.count }

自定义标签之set标签


自定义标签之select标签


运行结果:
jsp标签(二)_第1张图片
属性问题:java.lang.NoSuchMethodException
解决方法:在实体类添加一个无参的构造方法

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