(1)首先将org.springside.modules.web.taglib下面的3个类加到工程
BSAbstractMultiCheckedElementTag.java
BSCheckboxesTag.java
BSRadioButtonsTag.java
(3)接着将JSP中的
<%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %><div class="control-group"> <label class="control-label" for="roles">角色:</label> <div class="controls"> <span><input id="roles1" name="roles" type="checkbox" value="1"/><label for="roles1">ADMIN</label></span> <span><input id="roles2" name="roles" type="checkbox" value="2"/><label for="roles2">EDITOR</label></span> <input type="hidden" name="_roles" value="on"/> </div> </div>
改成SpringSide4以后生成的HTML:
<div class="control-group"> <label class="control-label" for="roles">角色:</label> <div class="controls"> <label for="roles1" class="checkbox"><input id="roles1" name="roles" type="checkbox" value="1"/>ADMIN</label> <label for="roles2" class="checkbox"><input id="roles2" name="roles" type="checkbox" value="2"/>EDITOR</label> <input type="hidden" name="_roles" value="on"/> </div> </div>
private void writeElementTag(TagWriter tagWriter, Object item, Object value, Object label, int itemIndex) throws JspException { tagWriter.startTag(getElement()); if (itemIndex > 0) { Object resolvedDelimiter = evaluate("delimiter", getDelimiter()); if (resolvedDelimiter != null) { tagWriter.appendValue(resolvedDelimiter.toString()); } } tagWriter.startTag("input"); String id = resolveId(); writeOptionalAttribute(tagWriter, "id", id); writeOptionalAttribute(tagWriter, "name", getName()); writeOptionalAttributes(tagWriter); tagWriter.writeAttribute("type", getInputType()); renderFromValue(item, value, tagWriter); tagWriter.endTag(); tagWriter.startTag("label"); tagWriter.writeAttribute("for", id); tagWriter.appendValue(convertToDisplayString(label)); tagWriter.endTag(); tagWriter.endTag(); }
org.springside.modules.web.taglib.BSAbstractMultiCheckedElementTag
private void writeElementTag(TagWriter tagWriter, Object item, Object value, Object label, int itemIndex) throws JspException { String id = resolveId(); //在用户自定义的cssClass前面加上inputType做为class //以前是<label class="labelCssClass">,现在变成<label class="checkbox labelCssClass"> String resolvedLabelClass = getInputType(); if (labelCssClass != null) { resolvedLabelClass += " " + labelCssClass; } //start tag从<span>改成<label> tagWriter.startTag("label"); tagWriter.writeAttribute("for", id); tagWriter.writeAttribute("class", resolvedLabelClass); if (itemIndex > 0) { Object resolvedDelimiter = evaluate("delimiter", getDelimiter()); if (resolvedDelimiter != null) { tagWriter.appendValue(resolvedDelimiter.toString()); } } //input tag从label前面移到了后面 tagWriter.startTag("input"); writeOptionalAttribute(tagWriter, "id", id); writeOptionalAttribute(tagWriter, "name", getName()); writeOptionalAttributes(tagWriter); tagWriter.writeAttribute("type", getInputType()); renderFromValue(item, value, tagWriter); tagWriter.endTag(); tagWriter.appendValue(convertToDisplayString(label)); tagWriter.endTag(); }
总结变化:
(1)Spring中的checkbox tag有个element属性,默认为span,生成的checkbox和label整个被<span>包裹住<span><input id="roles1" name="roles" type="checkbox" value="1"/><label for="roles1">ADMIN</label>
<label for="roles1" class="checkbox"><input id="roles1" name="roles" type="checkbox" value="1"/>ADMIN</label>