GVKun编程网logo

使用thymeleaf的html表单验证无法在Spring Boot(spring boot thymeleaf 表单提交)

16

如果您对使用thymeleaf的html表单验证无法在SpringBoot和springbootthymeleaf表单提交感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解使用thymeleaf的

如果您对使用thymeleaf的html表单验证无法在Spring Bootspring boot thymeleaf 表单提交感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解使用thymeleaf的html表单验证无法在Spring Boot的各种细节,并对spring boot thymeleaf 表单提交进行深入的分析,此外还有关于html – 在spring mvc中用thymeleaf引用一个.css文件、java – Spring Boot和Thymeleaf – 删除严格的HTML错误检查、spriingboot使用thymeleaf、spring boot + thymeleaf 项目中html 文件不能使用#{}取值?的实用技巧。

本文目录一览:

使用thymeleaf的html表单验证无法在Spring Boot(spring boot thymeleaf 表单提交)

使用thymeleaf的html表单验证无法在Spring Boot(spring boot thymeleaf 表单提交)

我无法使用thymleaf模板以html形式捕获任何验证。

表单对象-

public class TestForm {....    @Pattern(regexp = "[a-zA-Z]*", message = "Only characters")    private String field1;....getter/setters}

html代码-

<div id="main">            <form action="#" th:action="@{/create}" th:object="${testForm}" method="post" >                <div>                    <label> Field1:</label>                    <input type="text" th:field="${testForm.field1}" name="field1" />                    <div th:if="${#fields.hasErrors(''field1'')}" th:errors="*{field1}">errors</div>                </div>                                  <input type="submit" value="Submit" />                      </form>         </div>

控制器-

@RequestMapping(method= RequestMethod.POST, value = "/create")public String createTest(@Valid @ModelAttribute("testForm") TestForm testForm, MultipartHttpServletRequest request, BindingResult bindingResult) throws IOException {    if (bindingResult.hasErrors())         return "createtestform";

不会引发任何错误,并且表单已成功提交。

答案1

小编典典

我觉得很麻烦,因为您放置BindingResult不正确。用MultipartHttpServletRequest替换它,它必须 验证参数之后:

@RequestMapping(method= RequestMethod.POST, value = "/create")public String createTest(@Valid @ModelAttribute("testForm") TestForm testForm,  BindingResult bindingResult, MultipartHttpServletRequest request) throws IOException {if (bindingResult.hasErrors())     return "createtestform";

html – 在spring mvc中用thymeleaf引用一个.css文件

html – 在spring mvc中用thymeleaf引用一个.css文件

我正在做一个春季MVC和Thymeleaf的项目.如果我有这个文件夹结构,我有一个关于如何引用我的CSS文件的问题:
src
  main
    webapp
     resources
       myCssFolder
         myCssFile.css
     web-inf
       spring
       views
         myViewFolder
           index.html

我的配置类是这样的:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/css/**").addResourceLocations("/css/**");
    registry.addResourceHandler("/img/**").addResourceLocations("/img/**");
    registry.addResourceHandler("/js/**").addResourceLocations("/js/**");
    registry.addResourceHandler("/sound/**").addResourceLocations("/sound/**");
    registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/**");
}

我在我的索引文件中调用href,如下所示:

href="resources/css/bootstrap.min.css"

但是有一些元素在我的页面中搞砸了,例如CSS无效.

解决方法

您将需要使用th:href属性来引用css文件.以下是百里叶教程的示例.如果百里香叶无法评估th:href值,则默认为href值.
<head>
    <title>Good Thymes Virtual Grocery</title>
    <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all"  
      href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
 </head>

java – Spring Boot和Thymeleaf – 删除严格的HTML错误检查

java – Spring Boot和Thymeleaf – 删除严格的HTML错误检查

我使用 Spring Boot作为MVC应用程序,我的视图技术是Thymeleaf.我需要做的一件事就是复制现有网站的HTML(不是我做的……)并使用Thymeleaf渲染它.但是,某些网站的源HTML包含未关闭的HTML标记(例如< Meta>,< link>,< input>)或HTML标记,其中的元素未被引号括起,例如:
<div id=1></div>

代替

<div id="1"></div>

当然在浏览器中这有效……但是Thymeleaf不允许这样做,也不会为页面提供服务.有没有办法让这个更宽松的规则?我搜索了Thymeleaf的文档和Spring Boot参考,但没有找到答案.

只是为了澄清 – 我甚至没有为Thyemeleaf配置我自己的bean,只是通过maven作为spring-boot-starters之一添加到类路径中.所以现在这些都是默认设置.

解决方法

正如@mussdroid所说,一切都需要有效的XML.以下是Thymeleaf文档的一部分,解释了此背景: http://www.thymeleaf.org/doc/articles/fromhtmltohtmlviahtml.html

此外,如果这是一个问题,我相信您可以打开遗留模式以允许非XML模板,但我希望尽可能使用有效的XML:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#what-kind-of-templates-can-thymeleaf-process

我不知道自己如何更改模式,但我确信DuckDuckGo可以或在此网站上有人.

spriingboot使用thymeleaf

spriingboot使用thymeleaf

1 添加jar包

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2 配置文件

server.port=8090
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

3 在templates下新建hello.html

 

4 新建测试TestController.java

@Controller
public class TestController {
    @RequestMapping("/hello")
    public String toHello() {
        return "hello";
    }
}

5 访问页面

 

 6 完成

 

spring boot + thymeleaf 项目中html 文件不能使用#{}取值?

spring boot + thymeleaf 项目中html 文件不能使用#{}取值?

spring boot + thymeleaf 项目中html 文件不能使用#{}取值?idea报错不能这样取么?有大佬知道为什么?

今天的关于使用thymeleaf的html表单验证无法在Spring Bootspring boot thymeleaf 表单提交的分享已经结束,谢谢您的关注,如果想了解更多关于html – 在spring mvc中用thymeleaf引用一个.css文件、java – Spring Boot和Thymeleaf – 删除严格的HTML错误检查、spriingboot使用thymeleaf、spring boot + thymeleaf 项目中html 文件不能使用#{}取值?的相关知识,请在本站进行查询。

本文标签: