1、jsp标签
JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护。
2. 自定义标签开发步骤
1、 助手类 创建一个标签助手类(继承BodyTagSupport)标签属性必须助手类的属性对应、且要提供对应get/set方法
2、 tld
创建标签库描述文件(tld) 添加自定义标签的配置,tld文件必须保存到WEB_INF目录或其子目录
3、 taglib
<%@taglib uri="" prefix="" %>
注意导包:
commons-beanutils-1.8.0.jar
commons-logging.jar
一、助手类
com.huangyucan.jsp.SetTag
package com.huangyucan.jsp; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; public class SetTag extends BodyTagSupport{ private static final long serialVersionUID = 6412356344625170505L; private String var; private String value; public String getVar() { return var; } public void setVar(String var) { this.var = var; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public int doStartTag() throws JspException { pageContext.setAttribute(var, value); return SKIP_BODY; } }
com.huangyucan.jsp.OutTag
package com.huangyucan.jsp; 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 = 1502984121374458742L; private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public int doStartTag() throws JspException { JspWriter out=pageContext.getOut(); try { out.print(value); } catch (IOException e) { e.printStackTrace(); } return SKIP_BODY; } }
com.huangyucan.jsp.IFTag
package com.huangyucan.jsp; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; public class IFTag extends BodyTagSupport { private static final long serialVersionUID = 622073552521997125L; 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; } }
com.huangyucan.jsp.ForeachTag
package com.huangyucan.jsp; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; /** * c:foreach * var 指针 * items 指的是需要遍历的集合对象 * <c:foreach var="stu" items="stus"> * ${stu.name}... * </c:foreach> * @author Admin * */ public class ForeachTag extends BodyTagSupport { private static final long serialVersionUID = -9078639899233104183L; private String var; private List<Object> items=new ArrayList<Object>(); 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; } return EVAL_PAGE; } }
com.huangyucan.jsp.SelectTag
package com.huangyucan.jsp; 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.BeanUtils; /** * <select id='' name=''> <option value='-1' selected>===请选择===</option> </select> <z:select...></z:select> 查询维度;下拉列表 修改页面;下拉列表 数据回显 1、id、name 2、数据源、存入数据库中的值testkey、展示列testVal 3、加入属性(默认的头部属性值headTextKey、默认的展示列值headTextVal) 4、加入属性,能够实现数据回显的功能selectedVal * @author Admin * */ public class SelectTag extends BodyTagSupport{ private static final long serialVersionUID = 3905214201820858296L; private String id; private String name; private List<Object> items=new ArrayList<Object>(); private String textKey; private String textVal; private String headTextKey; private String HeadTextVal; 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 getHeadTextKey() { return headTextKey; } public void setHeadTextKey(String headTextKey) { this.headTextKey = headTextKey; } public String getHeadTextVal() { return HeadTextVal; } public void setHeadTextVal(String headTextVal) { this.HeadTextVal = headTextVal; } public String getSelectedVal() { return selectedVal; } public void setSelectedVal(String selectedVal) { this.selectedVal = selectedVal; } @Override public int doStartTag() throws JspException { JspWriter out=pageContext.getOut(); try { out.write(toHTML()); } catch (IOException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { e.printStackTrace(); } return super.doStartTag(); } /** * 拼接出下拉列表所对应的select的html代码 * <select id='' name=''> <option value='-1' selected>===请选择===</option> </select> * @return * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws NoSuchMethodException * @throws InvocationTargetException */ private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { StringBuilder sb=new StringBuilder(); sb.append("<select id='"+id+"' name='"+name+"'>"); if(headTextKey==null||"".equals(headTextKey)||HeadTextVal==null||"".equals(HeadTextVal)) { sb.append("<option value='"+headTextKey+"' selected>"+HeadTextVal+"</option> "); } String val; String html; for (Object obj : items) { // 要让学生id存入数据,让学生的名字展示在jsp页面 // <option value='s001'>zs</opyion> // obj sid sname Field textKeyField= obj.getClass ().getDeclaredField(textKey); textKeyField.setAccessible(true); val=(String) textKeyField.get(obj); html=BeanUtils.getProperty(obj, textVal); if(val.equals(selectedVal)) { sb.append("<option value='"+val+"' selected>"+html +"</opyion>"); } else { sb.append("<option value='"+val+"' >"+html +"</opyion>"); } } sb.append("</select>"); return sb.toString(); } }
二、tld
set、out、if、foreach、select标签的定义
<?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>JSTL 1.1 core library</description> <display-name>JSTL core</display-name> <tlib-version>1.1</tlib-version> <short-name>c</short-name> <uri>/huang</uri> <!-- <tag> 标签库中的标签(类似c:set c:out的定义) <name></name> 是标签运行具体代码,也就是助手类,下面填写的是助手类的全路径名 <tag-class></tag-class> <body-content></body-content> <attribute> 该标签的属性 <name></name> 该属性是否必填 <required></required> 是否支持表达式 <rtexprvalue></rtexprvalue> </attribute> </tag> --> <tag> <name>set</name> <tag-class>com.huangyucan.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> <tag> <name>out</name> <tag-class>com.huangyucan.jsp.OutTag</tag-class> <body-content>JSP</body-content> <attribute> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>if</name> <tag-class>com.huangyucan.jsp.IFTag</tag-class> <body-content>JSP</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>foreach</name> <tag-class>com.huangyucan.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> <tag> <name>select</name> <tag-class>com.huangyucan.jsp.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>headTextKey</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>HeadTextVal</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>selectedVal</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
jsp实现类
<%@page import="com.huangyucan.jsp.Student"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/huang" prefix="my" %> <!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> </head> <body> <my:set var="name" value="jhkhk"></my:set> <my:out value="${name }"></my:out> <br> ------------------------------- <br><my:if test="true">hh</my:if> <my:if test="false">jk</my:if><br> ------------------------------- <% List list=new ArrayList(); list.add(new Student("s001","旺财")); list.add(new Student("s002","肥波")); list.add(new Student("s003","小明")); request.setAttribute("stus", list); %> <br><my:foreach items="${stus }" var="stu"> ${stu.sid },${stu.sname }<br> </my:foreach> -------------------------------- <br><my:select textVal="sname" items="${stus }" textKey="sid"></my:select> </body> </html>