【问题标题】:How can I correctly set the required HTML attribute to an input tag into a TymeLeaf page?如何正确地将所需的 HTML 属性设置为 TymeLeaf 页面的输入标记?
【发布时间】:2016-01-02 04:44:32
【问题描述】:

我是 Spring MVC 中的新手,在 ThymeLeaf 中绝对是新手。

所以我正在开发一个使用 TymeLeaf 作为视图技术的 Web 应用程序,我必须使用这个 Jquery Validation Pluginhttp://jqueryvalidation.org/

所以我过去在 JSP 页面中使用过它。

如文档所示:http://jqueryvalidation.org/documentation/

我必须将 required 属性添加到必须由用户填写的输入标签中,如下所示:

<input id="cemail" type="email" name="email" required>

我尝试在我的 TymeLeaf HTML 页面中这样做:

<input id="nome" name="nome" type="text" th:value="*{nome}" required></input>

但问题是现在我在堆栈跟踪控制台中收到此错误消息并且页面未呈现:

15:36:47,180 ERROR [org.thymeleaf.templateparser.ErrorHandler] (http-localhost/127.0.0.1:8080-3) [THYMELEAF][http-localhost/127.0.0.1:8080-3] Fatal error during parsing: org.xml.sax.SAXParseException; lineNumber: 88; columnNumber: 78; Attribute name "required" associated with an element type "input" must be followed by the ' = ' character.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:196)
    at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:175)  

为什么?怎么了?如何正确地将 required HTML 属性设置为声明到 ThymeLeaf 页面中的输入标记?

【问题讨论】:

    标签: html spring-mvc jakarta-ee model-view-controller thymeleaf


    【解决方案1】:

    在 HTML5 中用 thymeleaf 声明 required 属性的正确方法是th:required="required"

    试试:

    <input id="nome" name="nome" type="text" th:value="*{nome}" th:required="required"></input>
    

    【讨论】:

    • 如果有人需要它:th:required="${is_required} ? 'required' : null"
    【解决方案2】:

    使用它:

    <input id="cemail" type="email" name="email" required="true" />
    

    Thymeleaf 标准方言可以在任何模式下处理模板,但 特别适用于面向 Web 的模板模式(XHTML 和 HTML5 那些)。除了 HTML5,它还专门支持和验证 遵循 XHTML 规范:XHTML 1.0 过渡,XHTML 1.0 严格、XHTML 1.0 框架集和 XHTML 1.1。

    查看更多信息:http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#what-is-thymeleaf

    【讨论】:

      猜你喜欢
      • 2018-04-04
      • 1970-01-01
      • 2013-04-24
      • 2011-02-04
      • 1970-01-01
      • 2019-01-23
      • 2013-03-08
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多