自定义标签开发步骤
1.1 助手类
1.2 tld
1.3 taglib
out 标签
第一步先写一个助手类代码如下:
继承了BodyTagSupport
public class OutTag extends BodyTagSupport {
private static final long serialVersionUID = -2288689618162607401L;
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.toString());
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
}
第二步写tld
<tag>
<name>out</name>
<tag-class>com.zrh.jsp.day02.OutTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
第三步输出值
<z:out value="${name }"></z:out>
set 标签
步骤和out一样,代码相似,就展示重要步骤了
代码如下
public class SetTag extends BodyTagSupport {
private static final long serialVersionUID = -547043983190642867L;
private String var;
private Object value;
@Override
public int doStartTag() throws JspException {
pageContext.setAttribute(var, value);
return SKIP_BODY;
}
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;
}
}
if 标签
if标签就比较简单了
代码如下:
public class IfTag extends BodyTagSupport {
private static final long serialVersionUID = -2838015447802594985L;
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;
}
tld配置的话有多少个属性就写多少个
forEach标签
public class ForeachTag extends BodyTagSupport {
private static final long serialVersionUID = -5619053683239283643L;
private String var;
private List<Object> items = new ArrayList<>();
/**
* 执行完这个方法的时候,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;
}
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;
}
}
select标签
select标签是我们的大bos
首先他的继承助手类是
public class SelectTag extends BodyTagSupport {
}
public class SelectTag extends BodyTagSupport {
private static final long serialVersionUID = -3939376511758370301L;
private String id;
private String name;
private List items = new ArrayList<>();
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 {
out.print(toHTML());
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException 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)||headerTextVal==null||"".equals(headerTextVal))) {
sb.append("<option value='"+headerTextKey+"'>"+headerTextVal+"</option>");
}
String value;
String html;
for (Object obj : items) {
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 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 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 String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSelectedVal() {
return selectedVal;
}
public void setSelectedVal(String selectedVal) {
this.selectedVal = selectedVal;
}
}
注意:一定记得写set,get
tld配置代码:
<tag>
<name>select</name>
<tag-class>com.zrh.jsp.day02.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>
<%
List list = new ArrayList();
list.add(new Student("001","xiaoli"));
list.add(new Student("002","xiaogfhd"));
list.add(new Student("003","xiaoshs"));
request.setAttribute("stus", list);
%>
<z:foreach items="${stus }" var="stu">
${stu.id },${stu.name }<br>
</z:foreach>
<z:select headerTextKey="-1" headerTextVal="====请选择====" textVal="name" items="${stus }" selectedVal="2" textKey="id"></z:select>
输出结果为:
checkbox标签
checkTag类
/**
* 1、值得传递 id name
* 2、数据源 items
* 3、展示列与数据存储列(value)与实体类的对应关系
* textVal textkey
* 4、数据回显 selectedVal
* 5、可能下拉框有默认值(头标签) headerTextKey headerTextVal
* @author zrh
*
*/
public class CheckTag extends BodyTagSupport {
private static final long serialVersionUID = -3939376511758370301L;
private String textkey;
private String textVal;
private List<Object> checkVal = new ArrayList<>();
private List<Object> items = new ArrayList<>();
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
StringBuffer sb = new StringBuffer();
String value;
String html;
for (Object obj : items) {
Field textkeyField = obj.getClass().getDeclaredField(textkey);
textkeyField.setAccessible(true);
value = (String) textkeyField.get(obj);
html = (String) PropertyUtils.getProperty(obj, textVal);
if(checkVal.contains(value)) {
sb.append("<input checked type='checkbox' value='"+value+"' />"+html+"");
}
else {
sb.append("<input type='checkbox' value='"+value+"'>"+html+"");
}
}
sb.append("</select>");
return sb.toString();
}
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> getCheckVal() {
return checkVal;
}
public void setCheckVal(List<Object> checkVal) {
this.checkVal = checkVal;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
}
tld配置
<tag>
<name>checkbox</name>
<tag-class> com.zrh.jsp.day02.CheckTag</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>checkVal</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<%
List list = new ArrayList();
list.add(new Student("001","xiaoli"));
list.add(new Student("002","xiaogfhd"));
list.add(new Student("003","xiaoshs"));
request.setAttribute("stus", list);
List l = new ArrayList();
l.add("001");
l.add("002");
request.setAttribute("list", l);
%>
<z:foreach items="${stus }" var="stu">
${stu.id },${stu.name }<br>
</z:foreach>
<z:checkbox textkey="id" textVal="name" items="${stus }" checkVal="${list }"></z:checkbox>
</body>
结果如下