自定义(set,out,if,foreach,select,Checkbox)标签
首先,我们还是来了解一下自定义标签的基本步骤
1、创建一个标签助手类(继承BodyTagSupport,凡是继承了BodyTagSupport就是一个标签助手类)
标签属性必须助手类的属性对应、且要提供对应get/set方法
2、创建标签库描述文件(tld),添加自定义标签的配置
3 、在JSP通过taglib指令导入标签库,并通过指定后缀 访问自定义标签
再次了解标签语言:
SKIP_BODY:跳过主体
EVAL_BODY_INCLUDE:计算标签主体内容并[输出]
EVAL_BODY_BUFFERED:计算标签主体内容并[缓存]
EVAL_PAGE:计算页面的后续部分
SKIP_PAGE:跳过页面的后续部分
EVAL_BODY_AGAIN:再计算主体一次
1、Set、Out 标签
1.1:Set的助手类
1.2:Out的助手类
1.3:Set和Out的标签配置
1.4:使用标签访问
由上图右边可得输出为 AA ,说明成功使用了自定义的标签
2、if标签
2.1:if的助手类
//test是true则执行代码主体部分,否则就跳过
@Override
public int doStartTag() throws JspException {
return test ? EVAL_BODY_INCLUDE:SKIP_BODY;
}
2.2:if的标签配置
2.3:使用标签访问
3、foreach标签
在配置前,我们先写一个Student的实体类
3.1:foreach的助手类
package com.zking.jsp;
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 = -2802046603364948022L;
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;
}
/**
* 执行完这个方法的时候,var所代表的指针一定要向下移动一位
*/
@Override
public int doStartTag() throws JspException {
if(items.size()==0) {
return SKIP_BODY;
}else {
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;
}
}
3.2:foreach的标签配置
3.3:应用foreach
在jsp中写以下代码:
<%
List list = new ArrayList();
list.add(new Student("001","AA"));
list.add(new Student("002","BB"));
list.add(new Student("003","CC"));
request.setAttribute("stus", list);
%>
<z:foreach items="${stus }" var="stu" >
${stu.id },${stu.name }<br>
</z:foreach>
输出结果为:
001,AA
002,BB
003,CC
4、select标签
首先我们需要分析select标签,方便我们理解
1.值的传递 id,name
2.数据源 items
3.展示列与数据存储列与实体类的对应关系 textKey textVal
4.数据回显 selectedVal
5.可能下拉框有默认值(头标签) headerText headerTextVal
4.1:select的助手类
public class SelectTag extends BodyTagSupport {
private static final long serialVersionUID = -5312778324462196944L;
private String id;
private String name;
private List<Object> items=new ArrayList<Object>();
private String textKey;
private String textVal;
private String selectedVal;
private String headerTextKey;
private String headerTextVal;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
try {
out.print(toHTML());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
StringBuffer sb=new StringBuffer();
sb.append("<select id='"+id+"' name='"+name+"'>");
if(!(headerTextKey == null || "".equals(headerTextKey) || headerTextKey == null || "".equals(headerTextKey))){//设置默认的选项就展示
sb.append("<option selected value='"+headerTextKey+"'>"+headerTextVal+"</option>");
}
String value;
String html;
for (Object obj : items) {
// <option value=''></option>
Field textKeyField = obj.getClass().getDeclaredField(textKey);
textKeyField.setAccessible(true);//打开访问权限
value = (String) textKeyField.get(obj);
html=(String) PropertyUtils.getProperty(obj, textVal);
if(value.equals(selectedVal)) {
sb.append("<option selected value='"+value+"'>"+html+"</option>");
}else {
sb.append("<option value='"+value+"'>"+html+"</option>");
}
}
sb.append("</select>");
return sb.toString();
}
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 getSelectedVal() {
return selectedVal;
}
public void setSelectedVal(String selectedVal) {
this.selectedVal = selectedVal;
}
public String getHeaderTextKey() {
return headerTextKey;
}
public void setHeaderTextKey(String headerTextKey) {
this.headerTextKey = headerTextKey;
}
public String getHeaderTextVal() {
return headerTextVal;
}
public void setHeaderTextVal(String headerTextVal) {
this.headerTextVal = headerTextVal;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
4.2:在z.tld中,select的标签配置
<tag>
<!-- 标签库中的标签名 -->
<name>select</name>
<!-- 标签对应的助手类的全路径名 -->
<tag-class>com.zking.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>selectedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>headerTextKey</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headerTextVal</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
4.3:select标签的应用
在jsp中写以下代码:
//这里默认设置id为002的BB
<z:select selectedVal="002" headerTextKey="-1" headerTextVal="===请选择===" items="${stus }" textVal="name" textKey="id"></z:select>
结果为:
5.Checkbox标签
5.1:Checkbox的助手类
textKey(传入值),textVal(显示值),checkedVal(回显数据集合),item(数据集合)
package com.shl.jsp2;
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.PropertyUtils;
public class CheckboxTag extends BodyTagSupport{
private String textKey;
private String textVal;
private List<Object> checkedVal = new ArrayList<>();
private List<Object> item = new ArrayList<>();
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 List<Object> getCheckedVal() {
return checkedVal;
}
public void setCheckedVal(List<Object> checkedVal) {
this.checkedVal = checkedVal;
}
public List<Object> getItem() {
return item;
}
public void setItem(List<Object> item) {
this.item = item;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
StringBuffer sb = new StringBuffer();
String value;
String html;
for (Object obj : item) {
Field field = obj.getClass().getDeclaredField(textKey);
field.setAccessible(true);
value = (String) field.get(obj);
Field field2 = obj.getClass().getDeclaredField(textVal);
field2.setAccessible(true);
html = (String) field2.get(obj);
if(checkedVal.contains(value)) {
sb.append("<input checked type='checkbox' value='"+value+"' />"+html+"");
}else {
sb.append("<input type='checkbox' value='"+value+"' />"+html+"");
}
}
return sb.toString();
}
5.2:在z.tld中,Checkbox的标签配置
<tag>
<name>checkbox</name>
<tag-class>com.shl.jsp2.CheckboxTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textVal</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>checkedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>item</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
5.3:jsp界面测试
<%
List lss=new ArrayList();
lss.add(new Student("001","音乐"));
lss.add(new Student("002","登山"));
lss.add(new Student("003","游泳"));
lss.add(new Student("004","阅读"));
request.setAttribute("lss", lss);
%>
<z:checkbox textVal="name" item="${lss}" textKey="id" checkedVal="${ls}"></z:checkbox>
结果为: