SpringBoot 7.SpringBoot 结合 Thymeleaf

1、引入 Thymeleaf 依赖css

     <!-- Spring boot - thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- 引入此依赖,再设置 spring.thymeleaf.mode=LEGACYHTML5,能够解决 thymeleaf 严格模式问题 -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
        </dependency>

2、在 application.properties 中对 Thymeleaf 进行配置html

# Thymeleaf
spring.thymeleaf.prefix=/WEB-INF/views/
spring.thymeleaf.suffix=.html
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5java

  设置后台返回视图url的前缀、后缀、以及 contentTypeweb

  设置thymeleaf是否缓存以及其模式(HTML 和 LEGACYHTML5)。spring

当咱们使用 HTML 模式的时候,你在 thymeleaf 模板的 html 中若是使用了没有闭合的 dom 标签,那么他就会报错,所以须要加入上述所说的  nekohtml 依赖,以及设置对应的 mode为 LEGACYHTML5。缓存

3、建立 index.htmlmybatis

 

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="ChengRuan" />

<title>title</title>
</head>
<body>
    <h1>Hello RCDDUP!!!</h1>
</body>
</html>

4、建立 PageController.javaapp

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="ChengRuan" />

<title>title</title>
</head>
<body>
    <h1 th:text="'Hello RCDDUP!!!'"></h1>
</body>
</html>

5、启动 App.javadom

  因为我将 server.port设置为9090,因此启动的url为:http://localhost:9090/indexide

 

  好了,咱们成功使用了 Thymeleaf。

6、解决静态资源(css、js等)问题

  在 CustomWebMvcConfigurer.java 中重写 addResourceHandlers() 方法。

  能够简写为:registry.addResourceHandler("/images/**","/css/**","/js/**").addResourceLocations("/images/","/css/","/js/");

package org.rcddup.app.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@MapperScan(basePackages = { "org.rcddup.app.dao" })
public class CustomWebMvcConfigurer extends WebMvcConfigurerAdapter {

  // 静态资源处理 @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/images/**").addResourceLocations("/images/"); registry.addResourceHandler("/css/**").addResourceLocations("/css/"); registry.addResourceHandler("/js/**").addResourceLocations("/js/"); super.addResourceHandlers(registry); } }

  1.建立 main.css

 

  而后咱们访问:http://localhost:9090/css/main.css

  在 index.html 中引入 main.css

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="ChengRuan" />

<link href="/css/main.css" rel="stylesheet">

<title>title</title>
</head>
<body>
    <h1 th:text="'Hello RCDDUP!!!'"></h1>
</body>
</html>

  而后咱们访问 index 页面,http://localhost:9090/index

  这样咱们就解决了静态资源的问题,能够很好的在 html 中引入你的静态资源了。

相关文章
相关标签/搜索