jsp自定义标签

it2022-05-05  116

jsp自定义标签

1.标签语言特点 <开始标签 属性=“属性值”>标签体</结束标签> 2.标签的分类

ui标签(out) 特点是显示数据,并且数据不是来源于标签体的,而是来源于jsp标签本身控制标签(if/foreach) 特点是控制的对象是标签体数据标签(set) 特点是存储数据,没有任何的页面效果 3.自定义标签的开发及使用步骤创建一个标签助手类(继承BodyTagSupport)标签属性必须助手类的属性对应、且要提供对应get/set方法创建标签库描述文件(tld),添加自定义标签的配置 注:tld文件必须保存到WEB-INF目录或其子目录在JSP通过taglib指令导入标签库,并通过指定后缀访问自定义标签

4.了解标签生命周期图 5.自定义set,out,if,foreach,select标签 需要使用到的jar包:

Set标签 package com.su.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; /** * 数据标签 * * set标签的作用是将value值赋给var * **/ 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.su.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyTagSupport; /** * * out属于ui标签(需要展示效果,依靠标签属性展现页面效果) * 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.su.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.su.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.su.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.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>c</short-name> <uri>/zking</uri> <!-- demo --> <tag> <!-- 填写的是标签库中的标签名 --> <name>demo</name> <!-- 标签对应的后台助手类 --> <tag-class>com.su.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.su.tag.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.su.tag.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.su.tag.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.su.tag.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.su.tag.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.sun.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>

最新回复(0)