自定义标签(二)set,out,if,foreach,select,checkbox
在自定义标签一里面,我已经提到了我们要自定义标签需要写助手类和实体类在这里我就直接截屏了。
1.set标签与out标签
out 的助手类
这个是我们配置out属性的代码
<tag>
<name>out</name>
<tag-class>com.zking.jsp.day02.OutTag</tag-class>//助手类的权限列名
<body-content>JSP</body-content>
<attribute>
<name>value</name>//属性名
<required>true</required>//属性值是否必填
<rtexprvalue>true</rtexprvalue>//是否支持表达式
</attribute>
</tag>
set助手类
这个是我们配置set属性的代码
<tag>
<name>set</name>
<tag-class>com.zking.jsp.day02.SetTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
下面是我们的jsp界面的out,set的测试代码以及结果
<z:set var="name" value="哈哈哈"></z:set>
<z:out value="${name }"></z:out>
结果是:哈哈哈
2.if标签
if标签的助手类
配置if属性的代码
<tag>
<name>if</name>
<tag-class>com.zking.jsp.day02.IfTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>text</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
下面是我们的jsp界面的if的测试代码以及结果
<z:if text="true">锻炼</z:if>
<z:if text="false">老地方就是</z:if>
结果是:锻炼,老地方是不会出来的
3.foreach标签
foreach的助手类,还有set,get的方法没有截取到,不过不影响。
<tag>
<name>foreach</name>
<tag-class>com.zking.jsp.day02.ForechTag</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>
下面是jsp界面的测试代码
<%
List list = new ArrayList();
list.add(new Student("001","呵呵"));
list.add(new Student("002","嘿嘿"));
list.add(new Student("003","哈哈"));
request.setAttribute("stus", list);
%>
<z:foreach items="${stus }" var="stu">
${stu.id },${stu.name }<br>
</z:foreach>
结果:
001,呵呵
002,嘿嘿
003,哈哈
4.select标签
select标签是一个比较复杂的标签,首先我们需要分析它的一些特性
- 1.值的传递 id,name
- 2.数据源 items
- 3.展示列与数据存储列与实体类的对应关系 textKey textVal
- 4.数据回显 selectedVal
- 5.可能下拉框有默认值(头标签) headerText headerTextVal
下面是select的助手类
public class SelectTag extends BodyTagSupport {
private static final long serialVersionUID = 5375902073989817712L;
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;//设置默认值的ID
private String headerTextVal;//设置默认值
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
try {
out.print(toHTML());//定义一个爸爸,写HTML代码
} 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();
}
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
StringBuffer sb = new StringBuffer();//定义一个sb,拼接上HTML代码
sb.append("<select id='"+id+"' name='"+name+"'");
if(!(headerTextKey == null || "".equals(headerTextKey)||
headerTextVal == null || "".equals(headerTextVal))) {//如果有设置默认的选项就展示,例如:===请选择===
sb.append("<option selected 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 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;
}
}
5.checkbox 标签
我们在自定义复选框标签的时候需要注意的有几点
1.复选框的 属性, 传入的值,需要显示的值,选中的可能是不是一个值而是一个集合 ,还有我们要取的值。
标签类
助手类,
public class CheckboxTag extends BodyTagSupport {
private static final long serialVersionUID = 1725404744433327629L;
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;
}
public CheckboxTag() {
super();
}
public CheckboxTag(String textKey, String textVal, List<Object> checkedVal, List<Object> item) {
super();
this.textKey = textKey;
this.textVal = textVal;
this.checkedVal = checkedVal;
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 (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IOException 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 : item) {
// 利用反射取属性值
Field field = obj.getClass().getDeclaredField(textKey);
field.setAccessible(true);
value = (String) field.get(obj);
html = (String) PropertyUtils.getProperty(obj, textVal);
System.out.println(value);// 测试
System.out.println(checkedVal);//
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();
}
}
jsp界面的代码
我有写student实体类,里面有两个属性id,name。这里就没有展示了
<%
List ls=new ArrayList(); //复选框的值
ls.add(new Student("001","篮球"));
ls.add(new Student("002","泰拳"));
ls.add(new Student("003","跆拳道"));
List lss=new ArrayList(); //已经选中的值
lss.add("001");
lss.add("003");
request.setAttribute("ls", ls);
request.setAttribute("lss", lss);
%>
<d:checkbox checkedVal="${lss }" item="${ls }" textVal="name" textKey="id"></d:checkbox>
结果:篮球和跆拳道是选中的