欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义标签的开发

程序员文章站 2022-06-08 23:11:12
...

Set,Out标签的开发

1.Set标签有var和value两个属性,Out标签只有value一个属性,先定义它们助手类

package com.shl.jsp2;

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

public class SetTag extends BodyTagSupport{

	private static final long serialVersionUID = 1L;

	private String var;
	private Object value;
	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;
	}
	
	@Override
	public int doStartTag() throws JspException {
		pageContext.setAttribute(var, value);//将值存入pageContext
		return SKIP_BODY;
	}
}

package com.shl.jsp2;

import java.io.IOException;

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 = 8655118409172739205L;
	
	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);//输出值
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	
}

2.配置tld文件

<tag>
    <name>set</name>
    <tag-class>com.shl.jsp2.SetTag</tag-class>
    <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.shl.jsp2.OutTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
    	<name>value</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

3.在jsp界面进行测试

<s:set var="name" value="shl"></s:set>
<s:out value="${name }"></s:out>

if标签的开发

1.if只有test一个属性值,定义它们助手类

	private boolean test;

	public boolean isTest() {
		return test;
	}

	public void setTest(boolean test) {
		this.test = test;
	}
	
	
	@Override
	public int doStartTag() throws JspException {
		//如果test为true,则进入doAfterBody方法,如果为false,则直接进入doEndTag方法
		return test?EVAL_BODY_INCLUDE:SKIP_BODY;
	}

2.配置tld文件

 <tag>
    <name>if</name>
    <tag-class>com.shl.jsp2.IfTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
    	<name>test</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

3.在jsp界面进行测试

<s:if test="true">lalal</s:if>//会出现
<s:if test="false">ppp</s:if>//不会出现

foreach标签的开发

1.foreach有var和items两个属性值,定义它们助手类

package com.shl.jsp2;

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

2.配置tld文件

 <tag>
    <name>foreach</name>
    <tag-class>com.shl.jsp2.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>

3.在jsp界面进行测试

<%
	List li=new ArrayList();
	li.add(new Student("111","shl"));
	li.add(new Student("222","qwe"));
	li.add(new Student("333","asd"));
	request.setAttribute("stus", li);
%>

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

结果:
自定义标签的开发

select标签的开发

1.先定义它们助手类

package com.shl.jsp2;

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

	
}

2.配置tld文件

<tag>
    <name>select</name>
    <tag-class>com.shl.jsp2.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>

3.在jsp界面进行测试

<%
	List li=new ArrayList();
	li.add(new Student("111","shl"));
	li.add(new Student("222","qwe"));
	li.add(new Student("333","asd"));
	request.setAttribute("stus", li);
%>

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

如果我们没有想要选中的值时可以将selectedVal属性去掉,没有默认选中的值时可以将headerTextKey和headerTextVal这两个属性去掉就可以了

Checkbox标签的开发

checkedbox要展示数据,还要有默认选中,所以需要有四个属性值:textKey(传入值),textVal(显示值),checkedVal(回显数据集合),item(数据集合)。

1.定义助手类

package com.shl.jsp2;

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;

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 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();
		} 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();
		String value;
		String html;
		for (Object obj : item) {
			//获取value和html
			value=(String) PropertyUtils.getProperty(obj, textKey);
			html=(String) PropertyUtils.getProperty(obj, textVal);
			
			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();
	}
}

2.配置tld文件

<tag>
    <name>checkbox</name>
    <tag-class>com.shl.jsp2.CheckboxTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
    	<name>textKey</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    	<name>textVal</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
    	<name>checkedVal</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
    	<name>item</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

3.在jsp界面进行测试

<%
	List li=new ArrayList();
	li.add(new Student("111","shl"));
	li.add(new Student("222","qwe"));
	li.add(new Student("333","asd"));
	request.setAttribute("stus", li);
	
	List li2=new ArrayList();//回调集合
	li2.add("111");
	li2.add("222");
	request.setAttribute("li2", li2);
%>

<s:checkbox textVal="name" item="${stus }" textKey="id" checkedVal="${li2 }"></s:checkbox>

结果:
自定义标签的开发