首页 > 其他分享 >spring-boot-starter-thymeleaf 避坑指南

spring-boot-starter-thymeleaf 避坑指南

时间:2023-11-20 16:24:12浏览次数:27  
标签:spring boot 避坑 thymeleaf html 标签 LEGACYHTML5

spring-boot-starter-thymeleaf 避坑指南

第一步:pom配置环境 先不要管包是做什么的 总之必须要有 否则进坑

1 2 3 4 5 6 7 8 9 10 11 <!--避坑包-->       <dependency>           <groupId>net.sourceforge.nekohtml</groupId>           <artifactId>nekohtml</artifactId>           <version>1.9.22</version>       </dependency>        <!--解析html包-->       <dependency>           <groupId>org.springframework.boot</groupId>           <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

 

第二步:配置application.properties 

       注意  1.结尾一定要有------ #thymeleaf end ---------  否则掉坑

                2.#模板编码 spring.thymeleaf.mode=LEGACYHTML5

                要想使用LEGACYHTML5这个编码格式必须引入 上面pom中‘避坑包’  否则用不了

               肯定有人要问为什么不用HTML5 ,你可以试试

                因为你可能会发现在默认配置下,thymeleaf对.html的内容要求很严格,比如<meta charset=”UTF-8″ />,

                如果少最后的标签封闭符号/,就会报错而转到错误页。也比如你在使用Vue.js这样的库,然后有<div v-cloak></div>这样的html代码,

               也会被thymeleaf认为不符合要求而抛出错误。因此,建议增加下面这段:

                 spring.thymeleaf.mode = LEGACYHTML5
                 spring.thymeleaf.mode的默认值是HTML5,其实是一个很严格的检查,改为LEGACYHTML5可以得到一个可能更友好亲切的格式要求。

               需要注意的是,LEGACYHTML5需要搭配一个额外的库NekoHTML才可用  也就时上文的避坑包

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #<!-- 关闭thymeleaf缓存 开发时使用 否则没有实时画面--> spring.thymeleaf.cache=false ## 检查模板是否存在,然后再呈现 spring.thymeleaf.check-template-location=true #Content-Type值 spring.thymeleaf.content-type=text/html #启用MVC Thymeleaf视图分辨率 spring.thymeleaf.enabled=true ## 应该从解决方案中排除的视图名称的逗号分隔列表 ##spring.thymeleaf.excluded-view-names= #模板编码 spring.thymeleaf.mode=LEGACYHTML5 # 在构建URL时预先查看名称的前缀 spring.thymeleaf.prefix=classpath:/templates/ # 构建URL时附加查看名称的后缀. spring.thymeleaf.suffix=.html # 链中模板解析器的顺序 #spring.thymeleaf.template-resolver-order= o # 可以解析的视图名称的逗号分隔列表 #spring.thymeleaf.view-names= #thymeleaf end

 这是我的静态网页结构

 

   第三步:controller层

   注入的时候一定要是Controller 不要是RestController 因为它是rest接口(json格式) 是解析不到html

    

1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Controller  注意不要是RestController @RequestMapping(value = "/") public class MainController {         @Autowired     MainService mainService;             @GetMapping(value = "/home")           public String homePage(){                 return "test";           } }

实在没有那么多需求 就用原生态 随便玩想跳哪里跳哪里

1 2 3 4 5   @GetMapping(value = "/home")           public void homePage(HttpServletResponse response)throws IOException{               response.sendRedirect("index.html"); //              return "index";           }

 

需要传值的话,java界面使用正常的方法就可 

  model.addAttribute("yu","Hello world Thymeleaf");

在html 界面需在html 标签里引用地址 。thymeleaf 使用的是OGNL 标签,和jstl 标签差不多个人感觉,例如:

<html lang="en" xmlns:th="http://www.thymeleaf.org">

<!--/*@thymesVar id="yu" type="java"*/-->
<p th:text="${yu}"></p>

我是用的是IDEA 编译 使用标签的话 会报错 只有添加<!--/*@thymesVar id="yu" type="java"*/--> 注释才不会报错

标签的话不需要自己写,点击左边报错的小红点会提示标签注释,自动生成

 

  最后收工,这么简单的东西也是走过腥风血雨过来的!!!

标签:spring,boot,避坑,thymeleaf,html,标签,LEGACYHTML5
From: https://www.cnblogs.com/zhangsai/p/17844235.html

相关文章

  • 基于springboot vue的宿舍管理系统
    项目源码免费获取方式放在文章末尾处项目技术数据库:Mysql5.7数据表:7张开发语言:Java(jdk1.8)开发工具:idea前端技术:Vue后端技术:SpringBoot 功能简介该项目是一个宿舍管理系统,分为管理员端,宿管员端。 管理员端:       登录        宿管模块            ......
  • springboot使用拦截器实现登录拦截
    参考文档https://blog.csdn.net/qq_50652600/article/details/127250413HandlerInterceptor作用:自定义拦截器如何创建:这个接口我们通常会自定义一个类。加上@Component注解,并且使它实现HandlerInterceptor接口(根据需求重写里面的三个方法)publicinterfaceHandlerInterceptor......
  • SpringBoot 策略模式 切换上传文件模式
    策略模式策略模式是指有一定行动内容的相对稳定的策略名称。我们定义一个接口(就比如接下来要实现的文件上传接口)我们定义所需要实现的策略实现类A、B、C、D(也就是项目中所使用的四种策略阿里云Oss上传、腾讯云Cos上传、七牛云Kodo上传、本地上传)我们通过策略上下文来调用策......
  • Linux reboot命令
    Linuxreboot命令用于用来重新启动计算机。若系统的runlevel为0或6,则重新开机,否则以shutdown指令(加上-r参数)来取代语法reboot[-n][-w][-d][-f][-i]参数:-n:在重开机前不做将记忆体资料写回硬盘的动作-w:并不会真的重开机,只是把记录写到/var/log/wtmp档......
  • Spring_2023_11_20_1
    Spring基础依赖pom依赖<!--Spring基础包括:Spring-core/Spring-beans/Spring-app/Spring-expression--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId&......
  • 高效开发与设计:提效Spring应用的运行效率和生产力
    引言现状和背景Spring框架是广泛使用的Java开发框架之一,它提供了强大的功能和灵活性,但在大型应用中,由于Spring框架的复杂性和依赖关系,应用的启动时间和性能可能会受到影响。这可能导致开发过程中的迟缓和开发效率低下。优化Spring应用程序的启动速度和性能是一个重要的任务,通过......
  • springboot 控制序列化反序列化示例(接口返回数据处理/接口接收数据处理)
    1.返回Long转JSONpackagecom.mingx.drone.config;importcom.fasterxml.jackson.core.JsonGenerator;importcom.fasterxml.jackson.databind.JsonSerializer;importcom.fasterxml.jackson.databind.SerializerProvider;importjava.io.IOException;/***@Descript......
  • 新建springboot项目,访问前端界面
    直接在IDEA中下载依赖会比较慢,将常用依赖下载到本地,然后从本地加载依赖会比较快。(方法可以搜,很多) pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quo......
  • SpringMVC异常处理
    自定义异常类SysException.javapackagecom.zjw.exception;importlombok.AllArgsConstructor;importlombok.Getter;importlombok.Setter;/***自定义异常类*/@Getter@Setter@AllArgsConstructorpublicclassSysExceptionextendsException{/***......
  • SpringMVC返回值
    字符串/***测试返回字符串*@parammodelmodel*@return返回的字符串,通过视图解析器调整到jsp页面*/@RequestMapping("/testString")publicStringtestString(Modelmodel){System.out.println("testString方法执行了。。。"......