前言
基于jsp自定义标签我们上一篇已经讲解了一些基本的语法、属性、以及开发步骤和注意事项。
那么我们今天就开始来了解以及使用jsp自定义标签。
常用作用域来存取数据有四个:
- pageContext
- request
- session
- application
案例:z:foreach、z:if、 z:deptList 、 z:out 、z:select(基于rebel.tld)
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<!-- 标签库描述符 -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>Simple Tags</short-name>
<uri>/zking</uri>
<tag>
<name>if</name>
<tag-class>com.zking.IfTag</tag-class>
<body-content>jsp</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!--自定义out标签 -->
<tag>
<name>out</name>
<tag-class>com.zking.OutTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!-- 自定义froeach标签 -->
<tag>
<name>foreach</name>
<tag-class>com.zking.ForeachTag</tag-class>
<body-content>jsp</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>
<!-- 自定义数据标签 -->
<tag>
<name>deptList</name>
<tag-class>com.zking.DeptlistTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!-- 自定义下拉框标签 -->
<tag>
<name>Select</name>
<tag-class>com.zking.SelectTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>cssStyle</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>cssClass</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>headValue</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>headText</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>valueProperty</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textProperty</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>selectValue</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
UI标签
(1) z:out
UI标签的展示方法:
注:JspWriter out = pageContext.getOut();
out.println(html)
public class OutTag extends BodyTagSupport{
private Object value;
public OutTag() {}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
System.out.println("doStartTag----------out");
System.out.println("value="+value);
JspWriter out = pageContext.getOut();
try {
out.print(value);
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
return super.doStartTag();
}
}
(2)z:select
这里我们要用到俩个jar包。
String 属性值=BeanUtils.getProperty(对象, 属性名);
注:PropertyUtils.getProperty 反射取值。
public class SelectTag extends BodyTagSupport{
private String name;//名称
private String cssStyle;//样式
private String cssClass;
private String headValue="-1";//头value
private String headText="----请选择----";//头文本
private String valueProperty;//deptid:java对象属性名
private String textProperty;//deptName:java对象属性名
private List<Object> items;//数据源
private String var;//保存当前数据到作用域的名字
private String selectValue;
public String getSelectValue() {
return selectValue;
}
public void setSelectValue(String selectValue) {
this.selectValue = selectValue;
}
public SelectTag() {}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCssStyle() {
return cssStyle;
}
public void setCssStyle(String cssStyle) {
this.cssStyle = cssStyle;
}
public String getCssClass() {
return cssClass;
}
public void setCssClass(String cssClass) {
this.cssClass = cssClass;
}
public String getHeadValue() {
return headValue;
}
public void setHeadValue(String headValue) {
this.headValue = headValue;
}
public String getHeadText() {
return headText;
}
public void setHeadText(String headText) {
this.headText = headText;
}
public String getValueProperty() {
return valueProperty;
}
public void setValueProperty(String valueProperty) {
this.valueProperty = valueProperty;
}
public String getTextProperty() {
return textProperty;
}
public void setTextProperty(String textProperty) {
this.textProperty = textProperty;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
@Override
public int doStartTag() throws JspException {
//获取到out
JspWriter out = pageContext.getOut();
try {
//将下拉框输出到html上面显示
out.print(toHtml());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
public String toHtml() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
//定义StringBuffer拼接下拉框
StringBuffer bf=new StringBuffer();
bf.append("<select name='"+this.name+"' style='"+this.cssStyle+"' class='"+this.cssClass+"'>");
//判断下拉框的默认值
if("".equals(this.headText)) {
bf.append("<option value='"+this.headValue+"'>'"+this.headText+"'</option>");
}
//判断数据源是否有值
if(null!=items) {
for (Object obj : items) {
String deptId =BeanUtils.getProperty(obj, valueProperty);
String deptName = BeanUtils.getProperty(obj, textProperty);
if(deptId.equals(this.selectValue)) {
bf.append("<option selected value='"+deptId+"'>"+deptName+"</option>");
}
bf.append("<option value='"+deptId+"'>"+deptName+"</option>");
}
}
bf.append("</select>");
//返回拼接好的下拉框
return bf.toString();
}
}
控制标签
(1)z:if
public class IfTag extends BodyTagSupport{
private boolean test;
public IfTag() {}
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
@Override
public int doStartTag() throws JspException {
System.out.println("doStartTag--------if");
if(test) {
return EVAL_BODY_INCLUDE;//计算主体并且包含到输出
}
return SKIP_BODY;
}
}
(2)z:foreach
public class ForeachTag extends BodyTagSupport{
private String var;//保存当前数据到作用域的一个key(键/名字)
private List<Object> items;//数据源
public ForeachTag() {}
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 {
if(null==items||0==items.size()) {
return SKIP_BODY;
}
Iterator<Object> it = items.iterator();
Object next = it.next();
pageContext.setAttribute(var, next);
//保存iterator的指针
pageContext.setAttribute("it", it);
return EVAL_BODY_INCLUDE;//计算主体并且包含到输出
}
@Override
public int doAfterBody() throws JspException {
Iterator<Object> it =(Iterator<Object>) pageContext.getAttribute("it");
while(it.hasNext()) {
Object next = it.next();
pageContext.setAttribute(var, next);
return EVAL_BODY_AGAIN;//再次计算主体
}
return SKIP_BODY;
}
}
数据标签
(1)z:deptList
public class DeptlistTag extends BodyTagSupport{
private String var;//保存数据到作用域的名字
private DeptDAO deptDAO=new DeptDAO();
public DeptlistTag() {}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
@Override
public int doStartTag() throws JspException {
List<Dept> list = deptDAO.list();
pageContext.setAttribute(var, list);
return SKIP_BODY;//跳过主体
}
}
测试页面:
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@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>
<style type="text/css">
.c1{
font-size: 30px;
color:red;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
List<String> list=new ArrayList<>();
list.add("hello");
list.add("大家好");
list.add("我叫java");
request.setAttribute("list",list);
%>
<h1>自定义if标签</h1>
<z:if test="true">自定义if值为真</z:if>
<h1>自定义out标签</h1>
<z:out value="我是自定义out标签"></z:out>
<h1>自定义foreach标签</h1>
<z:foreach items="${list }" var="v">
<div>${v}</div>
</z:foreach>
<h1>自定义deptList标签</h1>
<z:deptList var="deptList"></z:deptList>${deptList }
<h1>自定义select标签</h1>
<z:Select name="SelectName" valueProperty="deptId" textProperty="deptName" headText="~~~~请选择~~~~~" headValue="-1" items="${deptList}" cssStyle="width:400px" var="v" cssClass="c1" selectValue="5"/>
</body>
</html>
输出结果:
哈哈!开心,今天的砖终于搬完了!
转载:https://blog.csdn.net/qq_45384482/article/details/105565979
查看评论