jsp自定义标签 2.标签的分类
ui标签(out) 特点是显示数据,并且数据不是来源于标签体的,而是来源于jsp标签本身 控制标签(if/foreach) 特点是控制的对象是标签体 数据标签(set) 特点是存储数据,没有任何的页面效果
5.自定义set,out,if,foreach,select标签我们需要的架包 第一个Set标签
package com.lj.jsp.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; public class SetTag extends BodyTagSupport { private static final long serialVersionUID = 7664908711859566911L; 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); return SKIP_BODY; } }第二个标签Out
package com.lj.jsp.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyTagSupport; /** * * * JspWrite * * **/ public class OutTag extends BodyTagSupport { private static final long serialVersionUID = -8343176651327848062L; 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 super.doStartTag(); } }第三个标签if标签
package com.lj.jsp.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; /** * 数据标签 * * set标签的作用是将value值赋给var * **/ public class IfTag extends BodyTagSupport { private static final long serialVersionUID = 6775933230953509958L; 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; } }第四个标签Foreach标签
package com.lj.jsp.tag; 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 = -4452303119959186621L; 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; } @Override public int doStartTag() throws JspException { 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; }else { return EVAL_PAGE; } } }第五个标签Select标签
package com.lj.jsp.tag; 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; /** * select 的功能: * 1、新增查询页面,只要通过一个标签就可以完成数据的绑定,而不用使用c:foreach去循环绑定数据 * 2、修改页面,同样通过这个标签完成数据的遍历展示以及默认选中指定向 * * 分析: * 1、需要往后台传值 id,name * 2、定义数据库存储的对应的标签属性、前台展示的标签属性 textkey textval * 3、定义下拉框的默认值 headkey headval * 4、下拉框加载数据 items * 5、属性值接受数据库中保存的value值 被选中的值 selectedval * * */ public class SelectTag extends BodyTagSupport{ private static final long serialVersionUID = -7954129574986562370L; private String id; private String name; private List<Object> items = new ArrayList<>(); private String textkey; private String textval; private String headkey; private String headval; private String selectedval; 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 getHeadkey() { return headkey; } public void setHeadkey(String headkey) { this.headkey = headkey; } public String getHeadval() { return headval; } public void setHeadval(String headval) { this.headval = headval; } public String getSelectedval() { return selectedval; } public void setSelectedval(String selectedval) { this.selectedval = selectedval; } @Override public int doStartTag() throws JspException { JspWriter out = pageContext.getOut(); try { //对你想展现在页面上的html代码进行拼接 out.print(toHTML()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return super.doStartTag(); } //拼接标签代码 private String toHTML() throws Exception, Exception { StringBuilder sb = new StringBuilder();//更快 sb.append("<select id='"+id+"' name='"+name+"'>"); //加载类似于下拉框默认显示的 请选择 //判定是否有值 if(!(headkey == null || "".equals(headkey) || headval == null || "".equals(headval))) { sb.append("<option selected value='"+headkey+"'>"+headval+"</option>"); } String val = "";//写option value 的 String html = "";//写在option标签体的 展示看 //循环打印数据 items = value for (Object obj : items) { Field f = obj.getClass().getDeclaredField(textkey); f.setAccessible(true); val = (String) f.get(obj); f = obj.getClass().getDeclaredField(textval); f.setAccessible(true); html = (String) f.get(obj); //考虑如果是修改页面时,下拉框回选数据库所存储的值 也就是默认选中数据库所存储的值 if(val.equals(selectedval)) { sb.append("<option selected value='"+val+"'>"+html+"</option>"); }else { sb.append("<option value='"+val+"'>"+html+"</option>"); } } sb.append("</select>"); return sb.toString();//返回拼接的值 } }tld文件:
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.lj.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.lj.com/xml/ns/j2ee http://java.lj.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>c</short-name> <uri>/zking</uri> <!-- demo --> <tag> <!-- 填写的是标签库中的标签名 --> <name>demo</name> <!-- 标签对应的后台助手类 --> <tag-class>com.lj.jsp.DemoTag</tag-class> <!-- 标签类别 --> <body-content>JSP</body-content> <attribute> <!-- 标签所携带的属性 --> <name>test</name> <!-- 属性值是否必填--> <required>false</required> <!-- 是否支持表达式 --> <rtexprvalue>false</rtexprvalue> </attribute> </tag> <!-- set --> <tag> <name>set</name> <tag-class>com.lj.jsp.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> <!-- out --> <tag> <name>out</name> <tag-class>com.lj.jsp.OutTag</tag-class> <body-content>JSP</body-content> <attribute> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <!-- if --> <tag> <name>if</name> <tag-class>com.lj.jsp.IfTag</tag-class> <body-content>JSP</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <!-- foreach --> <tag> <name>foreach</name> <tag-class>com.lj.jsp.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> <!-- select --> <tag> <name>select</name> <tag-class>songwanxi.jsp.tag.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>headkey</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute><attribute> <name>headval</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>selectedval</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>demo2
<%@page import="com.lj.jsp.Teacher"%> <%@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.lj.com/jsp/jstl/core" %> <%@ taglib prefix="z" uri="/zking" %> <!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>jsp第二堂课</title> </head> <body> <z:set var="name" value="zs"></z:set> <z:out value="${name }"></z:out> <z:if test="true">ls</z:if> <z:if test="false">ww</z:if> <!-- foreach --> <% /* 模拟查过来的集合 */ List list = new ArrayList(); list.add(new Teacher("1","晓")); list.add(new Teacher("2","丽")); list.add(new Teacher("3","度")); pageContext.setAttribute("data", list); %> <m:foreach items="${data}" var="t"> ${t.sid},${t.sname}<br> </m:foreach> <hr> <!-- select --> <h2>新增查询页面下拉框</h2> <m:select textVal="sname" items="${data }" textKey="sid"></m:select> <hr> <m:select headerTextKey="-1" headerTextVal="--请选择--" textVal="sname" items="${data }" textKey="sid"></m:select> <hr> <h2>修改页面下拉框</h2> <m:select headerTextKey="-1" headerTextVal="--请选择--" textVal="sname" items="${data }" textKey="sid" selectedVal="2"></m:select> </body> </html>