1.要自定义foreach
1.1首先写一个助手类
<?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>zking 1.1 core library</description> <display-name>zking core</display-name> <tlib-version>1.1</tlib-version> <short-name>zking</short-name> <uri>http://tag.veryedu.cn</uri> <tag> <name>foreach</name> <tag-class>package com.xzs.tage.ForEacTag</tag-class> <body-content>empty</body-content> <attribute> <name>var</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
让后写一个ForeachTag类
package com.xzs.tage; 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 ForEacTag extends BodyTagSupport { private List<Object> item=new ArrayList<Object>(); private String var; public ForEacTag() { super(); } public ForEacTag(List<Object> item, String var) { super(); this.item = item; this.var = var; } @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 SKIP_BODY; } @Override public int doStartTag() throws JspException { if(item==null||item.size()==0) return SKIP_BODY; Iterator<Object> it=item.iterator(); pageContext.setAttribute(var, it.next()); pageContext.setAttribute("it", it); return EVAL_BODY_INCLUDE; } }
结果为:
2.自定义dept标签
2.1在助手类中写一个dept的代码
<tag> <name>dept</name> <tag-class>com.xzs.tage.DeptTag</tag-class> <body-content>JSP</body-content> <attribute> <name>var</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag>
2.2并且还要写DeptTag类
package com.xzs.tage; import java.util.ArrayList; import java.util.List; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; import com.zking.entity.Dept; public class DeptTag extends BodyTagSupport { private String var; public String getVar() { return var; } public void setVar(String var) { this.var = var; } @Override public int doAfterBody() throws JspException { // TODO Auto-generated method stub return super.doAfterBody(); } @Override public int doStartTag() throws JspException { //模拟数据库访问的过程 List<Dept> lst=new ArrayList(); lst.add(new Dept(1,"研发部")); lst.add(new Dept(2,"销售部")); lst.add(new Dept(3,"财务部")); pageContext.setAttribute(var, lst); return SKIP_BODY; } }
在jsp中就可以使用了
<%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@page import="com.zking.entity.Dept"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="/zking" 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> <% List<Dept> lst=new ArrayList(); lst.add(new Dept(1,"研发部")); lst.add(new Dept(2,"销售部")); lst.add(new Dept(3,"财务部")); request.setAttribute("lst", lst); %> </head> <body> <p>自定义foreach标签</p> <c:forEach items="${ lst}" var="lst"> ${lst.id }:${lst.name}<hr> </c:forEach> <z:foreach items="${lst }" var="l"> ${l}<hr> </z:foreach> <z:dept var="ds"></z:dept> 商品部门使用数据:${ds} </hr> </body> </html>
运行结果为:
作用:在进行权限管理是可以大大提高效率
3.自定义select标签
3.1编写selectTag类
package com.xzs.tage; 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; public class SelectTag extends BodyTagSupport { private List<Object> items=new ArrayList<Object>(); private String optionVal; private String optionText; public String getOptionVal() { return optionVal; } public void setOptionVal(String optionVal) { this.optionVal = optionVal; } public String getOptionText() { return optionText; } public void setOptionText(String optionText) { this.optionText = optionText; } public List<Object> getItems() { return items; } public void setItems(List<Object> items) { this.items = items; } @Override public int doAfterBody() throws JspException { // TODO Auto-generated method stub return super.doAfterBody(); } @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 char[] toHTML() { //String与StringBUffer的区别 //String处理字符串拼接更加消耗服务器内存 StringBuffer s=new StringBuffer("<select>"); try { for (Object object : items) { String val; String text; val = getObjAttrValue(object,optionVal); text=getObjAttrValue(object,optionText); s.append("<option value='"+val+"'>"+text+"</option>"); } } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } s.append("</select>"); return null; } private String getObjAttrValue(Object object, String attr) throws Exception, IllegalAccessException { Class clz=object.getClass(); Field f; f = clz.getDeclaredField(attr); f.setAccessible(true); return f.get(object).toString(); } }
3.2编写助手类
<tag> <name>select</name> <tag-class>com.xzs.tage.SelectTag</tag-class> <body-content>JSP</body-content> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>optionVal</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>optionText</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag>
3.3在jsp页面
<%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@page import="com.zking.entity.Dept"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="/zking" 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> <% List<Dept> lst=new ArrayList(); lst.add(new Dept(1,"研发部")); lst.add(new Dept(2,"销售部")); lst.add(new Dept(3,"财务部")); request.setAttribute("lst", lst); %> </head> <body> <p>自定义foreach标签</p> <c:forEach items="${ lst}" var="lst"> ${lst.id }:${lst.name}<hr> </c:forEach> <z:foreach items="${lst }" var="l"> ${l}<hr> </z:foreach> <z:dept var="ds"></z:dept> 商品部门使用数据:${ds} </hr> <select> <c:forEach items="" var="a"> </c:forEach> </select> <z:select items="${lst }" optionVal="id" optionText="name"></z:select> </body> </html>
得到的结果为: