感觉自己写一个Button的标签库
定义一个tld的文件,在写JSP的时候把这个Tld文件包含进去
集合类中的方法
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import cq.myUtil.util.StringUtil;
public class ButtonTag extends BodyTagSupport {
// 按钮的id
private String id;
// 按钮的name
private String name;
// 按钮不可用
private String disabled;
// 传进来的参数,是link就传url进来。是action 就传action进来。
private String parames;
// 按钮的class
private String htmlClass;
// 按钮的样式
private String style;
// 按钮上面的名字
private String value;
// 打开页面的类型
private String target;
// 类型非为三种 link submit button reset
private String btntype;
// 单击事件
private String onclick;
public void setBtntype(String btntype) {
this.btntype = btntype;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setDisabled(String disabled) {
this.disabled = disabled;
}
public void setParames(String parames) {
this.parames = parames;
}
public void setHtmlClass(String htmlClass) {
this.htmlClass = htmlClass;
}
public void setStyle(String style) {
this.style = style;
}
public void setValue(String value) {
this.value = value;
}
public void setTarget(String target) {
this.target = target;
}
public void setOnclick(String onclick) {
this.onclick = onclick;
}
@Override
public int doStartTag() throws JspException {
StringBuilder builder = new StringBuilder();
builder.append("<input ");
if ((btntype != null) && (btntype.equals("submit"))) {
builder.append("type='submit' ");
} else if ((btntype != null) && (btntype.equals("button"))) {
builder.append("type='button' ");
} else if ((btntype != null) && (btntype.equals("reset"))) {
builder.append("type='reset' ");
} else {
if (StringUtil.isEmpty(target)) {
target = "_self";
}
builder.append("type='button' onclick=\"javascript:window.open(\'" + parames + "\',\'" + target+ "\');\"");
}
builder.append("value ='" + value + "' ");
if (!StringUtil.isEmpty(id)) {
builder.append("id='" + id + "' ");
}
if (!StringUtil.isEmpty(name)) {
builder.append("name='" + name + "' ");
}
if (!StringUtil.isEmpty(disabled)) {
if (!disabled.equals("false")) {
builder.append("disabled='" + disabled + "' ");
}
}
if (!StringUtil.isEmpty(onclick)) {
builder.append("onclick='" + onclick + "' ");
}
if (!StringUtil.isEmpty(htmlClass)) {
builder.append("class='" + htmlClass + "' ");
}
if (!StringUtil.isEmpty(style)) {
builder.append("style ='" + style + "' ");
}
builder.append(" />");
try {
pageContext.getOut().write(builder.toString());
} catch (Throwable cause) {
throw new RuntimeException("自定义按钮标签出错", cause);
}
return SKIP_BODY;
}
}
定义一个tld的文件,在写JSP的时候把这个Tld文件包含进去
<tag>
<name>button</name>
<tagclass>cq.nfs.tag.ButtonTag</tagclass>
<bodycontent>JSP</bodycontent>
<attribute>
<name>id</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>btntype</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>parames</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>htmlClass</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>disabled</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>style</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>target</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>onclick</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
集合类中的方法
public static boolean isEmpty(Object str) {
if (str == null) {
return true;
}
return str.toString().matches("\\s*");
}