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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM