目录
■相关知识
使用JDK中的 keytool【创建证书】・【查看】・【使用】_sun0322-CSDN博客
SpringBoot + MyBatis 之 Hello World_sun0322-CSDN博客
SpringBoot + MyBatis + Thymeleaf 之 HelloWorld_sun0322-CSDN博客
■构造
■代码
java
注意点:不能使用 @RestController
package com.sxz.test.one;
import java.time.LocalDateTime;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
// @RestController // 它是无效的。不支持模板引擎
@Controller // 要使用它配置该类
public class HelloThymeleaf {
@Autowired
private HttpServletRequest request;
@Autowired
private HttpServletResponse response;
@GetMapping("/hello2")
public String hello(ModelMap modelMap) {
String agentInfo = request.getHeader("user-agent");
String showInfo = agentInfo;
LocalDateTime ldt = LocalDateTime.now();
System.out.println("-------------Access Begin----------------");
System.out.println(ldt);
System.out.println(showInfo);
System.out.println("-------------Access End----------------");
modelMap.addAttribute("name",showInfo);
//return showInfo;
return "helloThymeleaf";
}
}
html
注意点:要在templates目录下面
---
<html xmlns:th="http://www.thymeleaf.org">
<html>
<head>
<meta charset="UTF-8">
<title>helloThymeleaf页面</title>
</head>
<body bgcolor="#FFFF99">
<h1 th:text="${name}" ></h1>
<p>Hello!</p>
</body>
</html>
pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sxz.com</groupId>
<artifactId>SpringBootStaduy001</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBootStaduy001</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.10.RELEASE</version>
</parent>
<dependencies>
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
yml
server.port=443
server.ssl.key-store: .keystore
server.ssl.key-store-password: tomcat
server.ssl.keyStoreType: JKS
server.ssl.keyAlias: myTomcat
spring.thymeleaf.enabled:true
spring.thymeleaf.cache: false
spring.thymeleaf.mode: html
■启动
■效果
访问
----
--