当一个表单的属性比如:爱好,我们用了chexkbox可以多选,对于选中的值我已","号隔开存入数据库,当我们编辑这个表单时,如何对于的chexkbox自动选中。
我们已自定义thymeleaf标签来做。
1、thymeleaf标签
package com.hzj.oademo.config.thymeleaf;
import org.apache.commons.lang3.StringUtils;
import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractAttributeTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.standard.expression.IStandardExpression;
import org.thymeleaf.standard.expression.IStandardExpressionParser;
import org.thymeleaf.standard.expression.StandardExpressions;
import org.thymeleaf.templatemode.TemplateMode;
/**
* 自定义thymeleaf标签,处理checkbox选中问题,
* 系统checkbox保存值都是,号隔开存在字段中
* */
public class ThVTagProcessor extends AbstractAttributeTagProcessor {
private static final String TEXT_ATTRIBUTE = "lvalue";
private static final int PRECEDENCE = 10000;
/*templateMode: 模板模式,这里使用HTML模板。
dialectPrefix: 标签前缀。即xxx:text中的xxx。在此例子中prefix为thSys。
elementName:匹配标签元素名。举例来说如果是div,则我们的自定义标签只能用在div标签中。为null能够匹配所有的标签。
prefixElementName: 标签名是否要求前缀。
attributeName: 自定义标签属性名。这里为text。
prefixAttributeName:属性名是否要求前缀,如果为true,Thymeeleaf会要求使用text属性时必须加上前缀,即thSys:text。
precedence:标签处理的优先级,此处使用和Thymeleaf标准方言相同的优先级。
removeAttribute:标签处理后是否移除自定义属性。*/
public ThVTagProcessor(String dialectPrefix) {
super(
TemplateMode.HTML,
dialectPrefix,
"input",
false,
TEXT_ATTRIBUTE,
true,
PRECEDENCE,
true);
}
@Override
protected void doProcess(ITemplateContext iTemplateContext, IProcessableElementTag tag, AttributeName attributeName, String attributeValue, IElementTagStructureHandler structureHandler) {
final IEngineConfiguration configuration = iTemplateContext.getConfiguration();
final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);
final IStandardExpression expression = parser.parseExpression(iTemplateContext, attributeValue);
//lvalue的值
final Object lvalue = expression.execute(iTemplateContext);
//value的值
final String value = tag.getAttributeValue("value");
if(lvalue instanceof String && StringUtils.isNotBlank((String)lvalue)){
String val=(String)lvalue;
String[] array = StringUtils.split(val,",");
for(String a:array){
if(a.equals(value)){
structureHandler.setAttribute("checked","true");
break;
}
}
}
}
}
2、标签加入thymeleaf
package com.hzj.oademo.config.thymeleaf;
import org.thymeleaf.dialect.AbstractProcessorDialect;
import org.thymeleaf.processor.IProcessor;
import org.thymeleaf.standard.StandardDialect;
import org.thymeleaf.standard.processor.StandardXmlNsTagProcessor;
import org.thymeleaf.templatemode.TemplateMode;
import java.util.HashSet;
import java.util.Set;
public class ThVDialect extends AbstractProcessorDialect {
//定义方言名称
private static final String DIALECT_NAME="Sys Dialect";
public ThVDialect() {
//设置自定义方言与"方言处理器"优先级相同
super(DIALECT_NAME, "th", StandardDialect.PROCESSOR_PRECEDENCE);
}
@Override
public Set<IProcessor> getProcessors(String dialectPrefix) {
Set<IProcessor> processors=new HashSet<IProcessor>();
processors.add(new ThVTagProcessor(dialectPrefix));
// processors.add(new ThNumberFormatTagProcessor(dialectPrefix));
processors.add(new StandardXmlNsTagProcessor(TemplateMode.HTML, dialectPrefix));
return processors;
}
}
3、在config里配置
@Bean
public ThVDialect thVDialect() {
return new ThVDialect();
}
4、页面调用方式。lval就是“,”隔开的选中值如 "1,2,3,4"
<label th:each="brand:${brands}" class="check-box">
<input th:lvalue="${lval}" id="brands" name="brands" type="checkbox" th:value="${brand.brandId}" th:text="${brand.brandShortName}">
</label>