当前位置: 首页 > news >正文

做网站不要盲目跟风seo教程之关键词是什么

做网站不要盲目跟风,seo教程之关键词是什么,广宗网站建设,网站备案取消相信大家在使用 thymeleaf 的时候,绝大部分都是和 springboot 一块儿使用的,所以 th:field 属性用的很舒服。 但实际上,th:field 只有在 spring 环境下下有用,单独的 thymeleaf 是不支持的! 为什么我知道呢&#xff…

相信大家在使用 thymeleaf 的时候,绝大部分都是和 springboot 一块儿使用的,所以 th:field 属性用的很舒服。

但实际上,th:field 只有在 spring 环境下下有用,单独的 thymeleaf 是不支持的!

为什么我知道呢,因为在把若依的底层 spring 剔除,替换为 loveqq 的时候,发现表单用的 th:field 的数据没有回显!!!

而网上都是 spring 环境下的,没办法,只能看看源码了。具体过程就不详细写了,这里直接贴一下如何实现 th:field。

其实很简单,只要自定义 AbstractAttributeTagProcessor 就可以了,代码如下:
首先是基础实现类 AbstractLoveqqAttributeTagProcessor:

package com.kfyty.loveqq.framework.boot.template.thymeleaf.autoconfig.processor;import com.kfyty.loveqq.framework.core.utils.BeanUtil;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeDefinition;
import org.thymeleaf.engine.AttributeDefinitions;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.engine.IAttributeDefinitionsAware;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractAttributeTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.util.Validate;import java.util.HashMap;
import java.util.Map;/*** 描述: 标签处理器** @author kfyty725* @date 2024/6/05 18:55* @email kfyty725@hotmail.com*/
public abstract class AbstractLoveqqAttributeTagProcessor extends AbstractAttributeTagProcessor implements IAttributeDefinitionsAware {protected static final String ID_ATTR_NAME = "id";protected static final String TYPE_ATTR_NAME = "type";protected static final String NAME_ATTR_NAME = "name";protected static final String VALUE_ATTR_NAME = "value";protected static final String CHECKED_ATTR_NAME = "checked";protected static final String SELECTED_ATTR_NAME = "selected";protected static final String DISABLED_ATTR_NAME = "disabled";protected static final String MULTIPLE_ATTR_NAME = "multiple";protected AttributeDefinition idAttributeDefinition;protected AttributeDefinition typeAttributeDefinition;protected AttributeDefinition nameAttributeDefinition;protected AttributeDefinition valueAttributeDefinition;protected AttributeDefinition checkedAttributeDefinition;protected AttributeDefinition selectedAttributeDefinition;protected AttributeDefinition disabledAttributeDefinition;protected AttributeDefinition multipleAttributeDefinition;public AbstractLoveqqAttributeTagProcessor(final String dialectPrefix, final String elementName, final String attributeName, final int precedence) {super(TemplateMode.HTML, dialectPrefix, elementName, false, attributeName, true, precedence, false);}@Overridepublic void setAttributeDefinitions(AttributeDefinitions attributeDefinitions) {Validate.notNull(attributeDefinitions, "Attribute Definitions cannot be null");this.idAttributeDefinition = attributeDefinitions.forName(TemplateMode.HTML, ID_ATTR_NAME);this.typeAttributeDefinition = attributeDefinitions.forName(TemplateMode.HTML, TYPE_ATTR_NAME);this.nameAttributeDefinition = attributeDefinitions.forName(TemplateMode.HTML, NAME_ATTR_NAME);this.valueAttributeDefinition = attributeDefinitions.forName(TemplateMode.HTML, VALUE_ATTR_NAME);this.checkedAttributeDefinition = attributeDefinitions.forName(TemplateMode.HTML, CHECKED_ATTR_NAME);this.selectedAttributeDefinition = attributeDefinitions.forName(TemplateMode.HTML, SELECTED_ATTR_NAME);this.disabledAttributeDefinition = attributeDefinitions.forName(TemplateMode.HTML, DISABLED_ATTR_NAME);this.multipleAttributeDefinition = attributeDefinitions.forName(TemplateMode.HTML, MULTIPLE_ATTR_NAME);}@Overrideprotected void doProcess(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName, String attributeValue, IElementTagStructureHandler structureHandler) {if (this.continueProcess(context, tag, attributeName, attributeValue, structureHandler)) {this.doProcessInternal(context, tag, attributeName, attributeValue, structureHandler);}}protected boolean continueProcess(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName, String attributeValue, IElementTagStructureHandler structureHandler) {return this.getMatchingAttributeName().getMatchingAttributeName().getAttributeName().equals(attributeName.getAttributeName());}protected abstract void doProcessInternal(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName, String attributeValue, IElementTagStructureHandler structureHandler);public static Object buildEvaluatorContext(ITemplateContext context) {Map<String, Object> params = new HashMap<>();Object target = context.getSelectionTarget();// 先将目标属性放进去if (target != null) {params = BeanUtil.copyProperties(target);}// 搜索变量放进去for (String variableName : context.getVariableNames()) {Object variable = context.getVariable(variableName);params.put(variableName, variable);}return params;}
}

然后是 th:field 的实现:

package com.kfyty.loveqq.framework.boot.template.thymeleaf.autoconfig.processor;import com.kfyty.loveqq.framework.core.autoconfig.annotation.Component;
import com.kfyty.loveqq.framework.core.utils.OgnlUtil;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.standard.util.StandardProcessorUtils;import java.util.Map;
import java.util.Objects;/*** 描述: input field 处理器** @author kfyty725* @date 2024/6/05 18:55* @email kfyty725@hotmail.com*/
@Component
public class LoveqqInputFieldTagProcessor extends AbstractLoveqqAttributeTagProcessor {public LoveqqInputFieldTagProcessor() {this("th");}public LoveqqInputFieldTagProcessor(String dialectPrefix) {super(dialectPrefix, "input", "field", 0);}@Overrideprotected void doProcessInternal(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName, String attributeValue, IElementTagStructureHandler structureHandler) {Map<String, String> attributeMap = tag.getAttributeMap();String field = attributeValue.replaceAll("[*{}]", "");String value = OgnlUtil.compute(field, buildEvaluatorContext(context), String.class);if (!attributeMap.containsKey(NAME_ATTR_NAME)) {StandardProcessorUtils.setAttribute(structureHandler, this.nameAttributeDefinition, NAME_ATTR_NAME, field);}if (value != null && !attributeMap.containsKey(VALUE_ATTR_NAME)) {StandardProcessorUtils.setAttribute(structureHandler, this.valueAttributeDefinition, VALUE_ATTR_NAME, value);}if (value != null && Objects.equals(tag.getAttribute(TYPE_ATTR_NAME).getValue(), "radio")) {LoveqqInputCheckboxTagProcessor.processRadio(value, this.checkedAttributeDefinition, context, tag, structureHandler);}}
}

还有 th:checked 实现:

package com.kfyty.loveqq.framework.boot.template.thymeleaf.autoconfig.processor;import com.kfyty.loveqq.framework.core.autoconfig.annotation.Component;
import com.kfyty.loveqq.framework.core.utils.OgnlUtil;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeDefinition;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.exceptions.TemplateProcessingException;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.standard.util.StandardProcessorUtils;import java.util.Map;
import java.util.Objects;/*** 描述: input checkbox 处理器** @author kfyty725* @date 2024/6/05 18:55* @email kfyty725@hotmail.com*/
@Component
public class LoveqqInputCheckboxTagProcessor extends AbstractLoveqqAttributeTagProcessor {public LoveqqInputCheckboxTagProcessor() {this("th");}public LoveqqInputCheckboxTagProcessor(String dialectPrefix) {super(dialectPrefix, "input", "checked", 0);}@Overrideprotected void doProcessInternal(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName, String attributeValue, IElementTagStructureHandler structureHandler) {Map<String, String> attributeMap = tag.getAttributeMap();String express = attributeValue.replaceAll("[*${}]", "");String value = OgnlUtil.compute(express, buildEvaluatorContext(context), String.class);// radio checked 处理if (value != null && Objects.equals(tag.getAttribute(TYPE_ATTR_NAME).getValue(), "radio")) {processRadio(value, this.checkedAttributeDefinition, context, tag, structureHandler);}// 其他 checked 处理else {if (value != null && !attributeMap.containsKey(CHECKED_ATTR_NAME)) {StandardProcessorUtils.setAttribute(structureHandler, this.checkedAttributeDefinition, CHECKED_ATTR_NAME, value);}}}public static void processRadio(String value, AttributeDefinition checkedAttributeDefinition, ITemplateContext context, IProcessableElementTag tag, IElementTagStructureHandler structureHandler) {String checkedValue;if (tag.getAttribute("value") != null) {checkedValue = tag.getAttributeValue("value");} else if (tag.getAttribute("th:value") != null) {String express = tag.getAttributeValue("th:value").replaceAll("[*${}]", "");checkedValue = OgnlUtil.compute(express, buildEvaluatorContext(context), String.class);} else {throw new TemplateProcessingException("The input radio tag not found value.");}if (Objects.equals(checkedValue, value)) {StandardProcessorUtils.setAttribute(structureHandler, checkedAttributeDefinition, CHECKED_ATTR_NAME, value);}}
}

最后,需要把他们放入方言实现中:

package com.kfyty.loveqq.framework.boot.template.thymeleaf.autoconfig.dialect;import lombok.RequiredArgsConstructor;
import org.thymeleaf.processor.IProcessor;
import org.thymeleaf.standard.StandardDialect;import java.util.Set;/*** 描述: loveqq 方言实现** @author kfyty725* @date 2024/6/05 18:55* @email kfyty725@hotmail.com*/
@RequiredArgsConstructor
public class LoveqqStandardDialect extends StandardDialect {private final Set<IProcessor> processors;@Overridepublic Set<IProcessor> getProcessors(String dialectPrefix) {Set<IProcessor> processorSet = super.getProcessors(dialectPrefix);if (this.processors != null) {processorSet.addAll(this.processors);}return processorSet;}
}

使用 LoveqqStandardDialect 方言实现,并创建的时候将上面的两个处理器放进来就可以了。

http://www.hrbkazy.com/news/15661.html

相关文章:

  • 如何添加wordpress主题网站优化建议怎么写
  • 提供网站制作公司报价网站长尾关键词排名软件
  • 南开集团网站建设公司推广策划方案
  • 企业网站访问量的第一来源是( )电商网站建设方案
  • 第一次做网站做后感目前最火的推广平台
  • 商城网站建设开发公司网络推广有效果吗
  • 网上公司注册申请的流程快优吧seo优化
  • 长春什么时候解封电脑清理优化大师
  • 网站建设多少钱个人一句简短走心文案
  • 做qq动图的网站搜索引擎广告的优缺点
  • 网站开发语言占有率免费源码网站
  • 大连网站制作公司费用多少北京seo优化推广
  • 连锁酒店设计网站建设脚本外链平台
  • 苏州做网站最好公司哪家好网站设计费用
  • 龙华网站(建设龙华信科)武汉百度开户代理
  • 专业做房地产网站建设矿泉水软文广告500字
  • 如何做做网站如何推广一个平台
  • 防城港做网站竞价排名的服务模式是
  • 用vue做的网站模板太原网络推广公司
  • 个人网站一般做多大今日热点新闻事件摘抄2022
  • 天津企业建站程序深圳谷歌seo公司
  • 上海建设集团网站qq群引流推广平台
  • 网站建设结构总结今日十大热点新闻头条
  • 网站如何规划推广软件赚钱违法吗
  • 免费不收费用网站电子商务是干什么的
  • 四举措加强网站建设巨量关键词搜索查询
  • 国外做电商网站有哪些品牌网
  • 淄博做网站市场营销教材电子版
  • 北京网站开发网站建设2024年新闻摘抄
  • 会展网站建设情况线上销售怎么做推广