spring boot thymeleaf checkbox选中

本文介绍了一种自定义Thymeleaf标签的方法,用于处理多选框(Checkbox)的选中状态。通过实现Thymeleaf的AbstractAttributeTagProcessor,能够使Checkbox在编辑表单时根据数据库中存储的值自动选中。

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

当一个表单的属性比如:爱好,我们用了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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值