81,094
社区成员
发帖
与我相关
我的任务
分享
public class ButtonAuthorityTag extends BodyTagSupport {
private static final long serialVersionUID = 1L;
private final String DEFAULT_TYPE = "button";
private final String DEFAULT_CLASS = "btn btn-primary";
/**
* Creates TagWriter
* @return Created TagWriter
*/
TagWriter createTagWriter() {
TagWriter tagWriter = new TagWriter(this.pageContext);
return tagWriter;
}
@Override
public int doStartTag() throws JspException {
if(StringUtil.isBlank(this.type)){
type = this.DEFAULT_TYPE;
}
if(StringUtil.isBlank(this.cssClass)){
cssClass = this.DEFAULT_CLASS;
}
TagWriter tagWriter = createTagWriter();
//这里有个校验,成功返回true
if (validate()) {
tagWriter.startTag("input");
tagWriter.writeAttribute("type", type);
tagWriter.writeAttribute("id", id);
tagWriter.writeAttribute("name", name);
if (value.contains("spring:message")) {
MessageSource ms = (MessageSource) AppCtxUtil.getCtx().getBean("messageSource");
//截取code值
String key = value.replace(" ", "");
key = key.substring(key.indexOf("code=")+6);
key=key.substring(0,key.indexOf("/>")-1);
Locale lang = (Locale)session.getAttribute(SessionForm.LOCAL);
tagWriter.writeAttribute("value", ms.getMessage(key, null, lang));
} else {
tagWriter.writeAttribute("value", value);
}
tagWriter.writeAttribute("onclick", onclick);
tagWriter.writeAttribute("class", cssClass);
if ("true".equals(this.disabled)|| "disabled".equals(this.disabled)) {
tagWriter.writeAttribute("disabled", disabled);
}
tagWriter.endTag();
return EVAL_BODY_INCLUDE;
//return SKIP_BODY;
}
return SKIP_BODY;
}
@Override
public int doAfterBody() throws JspException {
return SKIP_BODY;
}
@Override
public int doEndTag() throws JspException {
BodyContent bodyContent = super.getBodyContent();
String body = bodyContent.getString();
this.value = body;
return EVAL_PAGE;
}
@Override
public void doInitBody() throws JspException {
}
@Override
public void setBodyContent(BodyContent bodyContent) {
this.bodyContent = bodyContent;
}
private String value = "";
private String type = "";
private String id = "";
private String name = "";
private String cssClass = "";
private String onclick = "";
private String disabled = "";
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
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 String getCssClass() {
return cssClass;
}
public void setCssClass(String cssClass) {
this.cssClass = cssClass;
}
public String getOnclick() {
return onclick;
}
public void setOnclick(String onclick) {
this.onclick = onclick;
}
public String getDisabled() {
return disabled;
}
public void setDisabled(String disabled) {
this.disabled = disabled;
}
JSP页面上的我的标签是这样<ts:buttonAuth type="button" onclick="search()" ><spring:message code='button.lable.Add'/></ts:buttonAuth>
我的疑惑是我想把标签体的内容也就是<spring:message code='button.lable.Add'/>赋值给value属性,但是我不知道怎么去获取标签体内的值。我用BodyContent bodyContent = super.getBodyContent();可是bodyContent是null,不知道为什么。