1. 程式人生 > 其它 >SpringBoot當中如何整合動態html模板:Thymeleaf

SpringBoot當中如何整合動態html模板:Thymeleaf

SpringBoot當中如何整合動態html模板:Thymeleaf

4.整合動態html模板:Thymeleaf:

馬克-to-win@馬克java社群:光是靜態html還不足夠,必須html還能顯示動態成分,這時我們考慮使用thymeleaf,就能完全達到springmvc的水平了,官方推薦thymeleaf。繼續在上一部分的專案中,在src/main目錄下,新增resources/templates/result.html:(參考目錄下:bootThymeleaf)


例4.1:

1)首先在pom.xml中新增:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>


注意:即使導了上面的包,也沒有辦法訪問到resources根目錄下的html。至於templates目錄下的html,直接或sendRedirect都不能訪問。唯有用下面的方法訪問。

package com.SpringbootMaven;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@SpringBootApplication
public class App {

@RequestMapping("/hello")
public String myhome(HttpServletResponse res,HttpSession session) throws IOException {
System.out.println("spring boot springMvc 馬克-to-win!");
session.setAttribute("user","馬克-to-win 馬克java社群創始人");
return "result";
/*下列不能再用了,在Thymeleaf框架中,sendRedirect不能跳到templates目錄裡的html*/
// res.sendRedirect("result.html");
}

public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}


index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
index1
<a href="/hello.do">test SpringMvc</a>
</body>
</html>





更多內容請見原文,文章轉載自:

https://blog.csdn.net/qq_44639795/article/details/94297587