我有一个非常简单的JSP标记来生成一个面板。
标签正在扩展SimpleTagSupport。静态内容呈现正常,但正文中的简单out-tag未正确调用。
public abstract class AbstractTag extends SimpleTagSupport {
private static Logger logger = LoggerFactory.getLogger(AbstractTag.class);
private static final long serialVersionUID = 7666479886150080344L;
private String elementType;
private String htmlID;
private HtmlClassAspect classAspect = new HtmlClassAspect();
private List<AbstractAspect> aspects = new ArrayList<AbstractAspect>();
private AbstractTag surroundingTag;
private List<AbstractTag> beforeBodyChildTags = new ArrayList<AbstractTag>();
private List<AbstractTag> afterBodyChildTags = new ArrayList<AbstractTag>();
private List<AbstractTag> beforeParentChildTags = new ArrayList<AbstractTag>();
private List<AbstractTag> afterParentChildTags = new ArrayList<AbstractTag>();
private boolean isInlineTag = false;
public AbstractTag(String elementType) {
this.elementType = elementType;
}
public abstract void setupTag();
public void doStartTag() throws JspException, IOException {
setupTag();
JspWriter out = getJspContext().getOut();
// add htmlID if present
if (!Check.isNull(htmlID)) {
addAspect(new HtmlIDAspect(htmlID));
}
if (surroundingTag != null) {
this.setParent(surroundingTag);
surroundingTag.doStartTag();
}
// generate open-tag
if (!Check.isNull(elementType)) {
out.print("<");
out.print(elementType);
// apply class aspect if not empty
if (classAspect.getValues().size() > 0) {
classAspect.apply(out);
}
// apply aspects
for (AbstractAspect aspect : aspects) {
aspect.apply(out);
}
// close opening tag
if (!elementType.equalsIgnoreCase(HTML.DIV)) {
if (beforeBodyChildTags.size() == 0 && getJspBody() == null && afterBodyChildTags.size() == 0) {
out.print(" /");
}
}
if(!isInlineTag){
out.println(">");
}else{
out.print(">");
}
}
for (AbstractTag child : beforeParentChildTags) {
child.setJspContext(getJspContext());
child.doTag();
}
}
public void doEndTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
// generate close tag if needed
if (!Check.isNull(elementType)) {
if (beforeBodyChildTags.size() > 0 || getJspBody() != null || afterBodyChildTags.size() > 0 || elementType.equalsIgnoreCase(HTML.DIV)) {
out.print("</");
out.print(elementType);
out.print(">");
}
}
// generate afterParent child-Tags
for (AbstractTag child : afterParentChildTags) {
child.setJspContext(getJspContext());
child.doTag();
}
if (surroundingTag != null) {
this.setParent(surroundingTag);
surroundingTag.doEndTag();
// for (AbstractTag tag : surroundingTag.getAfterParentChildTags())
// {
// tag.setJspContext(getJspContext());
// tag.doTag();
// }
}
}
@Override
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
doStartTag();
// before body children
for (AbstractTag child : beforeBodyChildTags) {
child.setJspContext(getJspContext());
child.doTag();
}
// body
if (getJspBody() != null) {
getJspBody().invoke(null);
}
// after body children
for (AbstractTag child : afterBodyChildTags) {
child.setJspContext(getJspContext());
child.doTag();
}
doEndTag();
}
public AbstractTag getSurroundingTag() {
return surroundingTag;
}
public void setSurroundingTag(AbstractTag surroundingTag) {
this.surroundingTag = surroundingTag;
this.surroundingTag.setJspContext(getJspContext());
this.surroundingTag.addChildBeforeBody(this);
}
public void addChildBeforeParent(AbstractTag component) {
component.setParent(this);
beforeParentChildTags.add(component);
}
public List<AbstractTag> getBeforeParentChildTags() {
return beforeParentChildTags;
}
public void addChildBeforeBody(AbstractTag component) {
component.setParent(this);
beforeBodyChildTags.add(component);
}
public List<AbstractTag> getBeforeBodyChildTags() {
return beforeBodyChildTags;
}
public void addChildAfterBody(AbstractTag component) {
component.setParent(this);
afterBodyChildTags.add(component);
}
public List<AbstractTag> getAfterBodyChildTags() {
return afterBodyChildTags;
}
public void addChildAfterParent(AbstractTag component) {
component.setParent(this);
afterParentChildTags.add(component);
}
public List<AbstractTag> getAfterParentChildTags() {
return afterParentChildTags;
}
public void addAspect(AbstractAspect aspect) {
if (aspect instanceof HtmlClassAspect) {
throw new IllegalArgumentException("Not allowed to add HtmlClassAspect; use AbstractTag#addClass() instead");
}
aspects.add(aspect);
}
public String getHtmlID() {
return htmlID;
}
public void setHtmlID(String htmlID) {
this.htmlID = htmlID;
}
public void addClass(String... htmlClass) {
for (String cls : htmlClass) {
classAspect.addValue(cls);
}
}
public void setInlineTag(boolean isInlineTag) {
this.isInlineTag = isInlineTag;
}
}
JSP:
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://.../taglib/ui" prefix="ui"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:if test="${not empty errorMsg}">
<ui:panel type="WARNING" title="Error">
<c:out value="${error_msg}"/>
</ui:panel>
</c:if>
HTML输出:
<div class="panel panel-warning">
<c:out value="${error_msg}"/>
</div>
我现在正在研究这几天......