Thymeleaf 3是否支持多种三元条件,例如:
cond1 ? do1 : cond2 ? do2 : do3
以下代码适用于Thymeleaf 3:
<th:block th:replace="${toolbar == true} ? ~{common/fragments :: _$pageToolbar} : ~{app/fragments :: ${toolbar}}">
我通过true
获取默认工具栏或string
以获取自定义工具栏。
但是这段代码,如果在toolbar
参数中没有传递任何内容,它本质上应该删除该块会产生一个解析异常:
<th:block th:replace="${toolbar == true} ? ~{common/fragments :: _$pageToolbar} : ${toolbar != null} ? ~{app/fragments :: ${toolbar}} : ~{common/fragments :: _$empty}">
也许我在这里采取了错误的做法?
Thymeleaf解析异常:
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "${toolbar == true} ? ~{common/fragments :: _$pageToolbar} : ${toolbar != null} ? ~{app/fragments :: ${toolbar}} : ~{common/fragments :: _$empty}" (template: "common/fragments" - line 70, col 15)
at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:131) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:62) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:44) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.processor.AbstractStandardFragmentInsertionTagProcessor.computeFragment(AbstractStandardFragmentInsertionTagProcessor.java:387) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.processor.AbstractStandardFragmentInsertionTagProcessor.doProcess(AbstractStandardFragmentInsertionTagProcessor.java:111) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.Model.process(Model.java:282) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1587) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleOpenElementEnd(TemplateHandlerAdapterMarkupHandler.java:304) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:278) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleOpenElementEnd(OutputExpressionInlinePreProcessorHandler.java:186) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:124) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.attoparser.HtmlElement.handleOpenElementEnd(HtmlElement.java:109) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.HtmlMarkupHandler.handleOpenElementEnd(HtmlMarkupHandler.java:297) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.MarkupEventProcessorHandler.handleOpenElementEnd(MarkupEventProcessorHandler.java:402) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.ParsingElementMarkupUtil.parseOpenElement(ParsingElementMarkupUtil.java:159) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:710) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
... 79 common frames omitted
答案 0 :(得分:2)
Thymeleaf 3是否支持多种三元条件,例如:
cond1 ? do1 : cond2 ? do2 : do3
是的,确实如此。只需用括号括起第二个三元运算符即可摆脱解析异常。
cond1 ? do1 : (cond2 ? do2 : do3)
应用后,您的表达应该有效。