JSP标签_2

JSP标签

1、自定义标签开发步骤

① 助手类 ; ② tld ; ③ taglib ;

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() {
		super();
	}
}
<tag>
  <!-- 标签库中的标签名 -->
    <name>demo</name>
    <!-- 标签对应的助手类的全路径名 -->
    <tag-class>com.shiyi.jsp.day01.DemoTag</tag-class>
   	<!-- JSP -->
    <body-content>JSP</body-content>
    <attribute>
    	<!-- 属性名 -->
        <name>test</name>
        <!-- 属性值是否必填 -->
        <required>false</required>
        <!-- 是否支持表达式 -->
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>
2、 UI标签

UI标签out 标签:

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 (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
}
-------------------------
然后在WebContent下新建一个jsp文件,在<body>内写以下代码
<body>
	<z:set var="name" value="zhangsan"></z:set>
	<z:out value="${name }"></z:out>
</body>
-------------------------
最后在WEB-INF下新建一个以tld结尾的file文件
<tag>
    <name>out</name>
    <tag-class>com.shiyi.jsp.day02.OutTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
  -----------------------
  输出的结果为:zhangsan

UI标签select 标签:

/**
 * 1、值的传递 id、name
 * 2、数据源 items
 * 3、展示列与数据存储列与实体类的对应关系  textKey存储到数据库的   textVal展示给别人看的  
 * 4、数据回显 selectedVal
 * 5、可能下拉框有默认值(头标签) headerTextKey  headerTextVal
 * @author shiyi
 *
 */
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;
	private String textVal;
	private String selectedVal;
	private String headerTextKey;
	private String headerTextVal;
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			try {
				try {
					out.print(toHTML());
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				}
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			}
		} catch (IOException 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;
		for (Object obj : items) {
//			<option value=''></option>
			Field textKeyField = obj.getClass().getDeclaredField(textKey);
			textKeyField.setAccessible(true);
			value = (String) textKeyField.get(obj);
			html = (String) PropertyUtils.getProperty(obj, textVal);
			if(value.equals(selectedVal)) {
				sb.append("<option selected value='"+value+"'>"+html+"</option>");
			}else {
				sb.append("<option value='"+value+"'>"+html+"</option>");
			}
		}
		return sb.toString();
	}
<body>
	<z:set var="name" value="zhangsan"></z:set>
	<z:out value="${name }"></z:out>
	
	<z:if test="true">lisi</z:if>
	<z:if test="false">wangwu</z:if>
	<%
		List list = new ArrayList();
	    list.add(new Student("001","小李"));
		list.add(new Student("002","小刘"));
		list.add(new Student("003","小王"));
		request.setAttribute("stus", list);
	%>
	
	<z:foreach items="${stus }" var="stu">
		${stu.id },${stu.name }<br>
	</z:foreach>
	
	<!-- 运行的结果为
		zhangsan lisi
		001,小李
		002,小刘
		003,小王
	 -->
	<!-- 修改 -->
	<z:select selectedVal="002" headerTextKey="-1" headerTextVal="===请选择==="
	 textVal="name" items="${stus }"  selectedVal="2" textKey="id"></z:select>
</body>
</html>

注意:JspWriter writer = pageContext.getOut();

UI标签checkbox 标签:

3、控制标签

控制标签if标签:

助手类:
package com.shiyi.jsp.day02;

import java.io.IOException;
import java.lang.reflect.Field;
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 static final long serialVersionUID = -5301514341025414346L;
	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 getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	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();
		}
		return super.doStartTag();
	}
	private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		StringBuffer sb=new StringBuffer();
		String value;
		String html;
		for (Object obj : item) {
			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(checkedVal.contains(value)) {
				sb.append("<input checked type='checkbox' value='"+value+"' />"+html+"");
			}
			else {
				sb.append("<input type='checkbox' value='"+value+"' />"+html+"");
			}
		}
		return sb.toString();
	}
}

public class IfTag extends BodyTagSupport {
	private static final long serialVersionUID = -8477321984637500253L;
	private boolean test;
	public boolean isTest() {
		return test;
	}
	public void setTest(boolean test) {
		this.test = test;
	}
	@Override
	public int doStartTag() throws JspException {
		return test ? EVAL_BODY_INCLUDE : SKIP_BODY;
	}
}

进行属性的设值:
<tag>
	    <name>checkbox</name>
	    <tag-class>com.yangjie.jsp.CheckboxTag</tag-class>
	    <body-content>JSP</body-content>
	    <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>checkedVal</name>
	        <required>false</required>
	        <rtexprvalue>true</rtexprvalue>
	    </attribute>
  </tag>

用实列进行检测:

```Java
<%
	List ls=new ArrayList();
	ls.add(new student("1","文学"));
	ls.add(new student("2","地理"));
	ls.add(new student("3","历史"));
	List la = new ArrayList();
	session.setAttribute("la", la);
	lb.add("1");
	lb.add("3");
	request.setAttribute("ls", ls);
	request.setAttribute("la", la);
	
%>
<z:checkbox textVal="name" checkedVal="${lb}" item="${ls}" textKey="id"></z:checkbox>

控制标签forEach标签:

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

注意:page(pageContext)|request(…)|session(…)|application(…) 存储和交换数据

4、数据标签

数据标签就是用来存储数据的。
数据标签set标签:

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);
//		传session方法
//		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
//		request.getSession();
		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;
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值