JSP自定义标签

本文介绍了一个自定义的Button标签库实现,该标签库能够创建不同类型的按钮,并支持多种属性设置,如id、name、type等,适用于JSP页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

感觉自己写一个Button的标签库

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*");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值