jsp自定义标签2
程序员文章站
2022-05-30 20:50:29
...
自定义标签开发步骤
1.1 助手类
1.2 tld
1.3 taglib
数据标签开发一个set标签
开发一个UI标签中的out标签
应用一下,结果如下:
控制标签
开发一个if标签和forEach 标签
select标签
public class SelectTag extends BodyTagSupport {
private String id;
private String name;
private List<Object> items = new ArrayList<>();
private String textKey;
private String textVal;
private String selectVal;
private String headerTextKey;
private String headerTextVal;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws Exception {
StringBuffer sb = new StringBuffer();
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);
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 getSelectVal() {
return selectVal;
}
public void setSelectVal(String selectVal) {
this.selectVal = selectVal;
}
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;
}
}
在tld里:
<tag>
<name>select</name>
<tag-class>com.jsp.day2.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>selectVal</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>
如果要默认选中bbb的话,加上这些
if(value.equals(selectVal)) {
sb.append("<option selected value='"+value+"'>"+html+"</option>");
}else {
sb.append("<option value='"+value+"'>"+html+"</option>");
}