SpringBoot當(dāng)中如何整合動(dòng)態(tài)html模板:Thymeleaf

整合動(dòng)態(tài)html模板:Thymeleaf:
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
馬克-to-win@馬克java社區(qū):光是靜態(tài)html還不足夠,必須html還能顯示動(dòng)態(tài)成分,這時(shí)我們考慮使用thymeleaf,就能完全達(dá)到 springmvc的水平了,官方推薦thymeleaf。繼續(xù)在上一部分的項(xiàng)目中,在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>


注意:即使導(dǎo)了上面的包,也沒有辦法訪問到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社區(qū)創(chuàng)始人");
        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>


result.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Title</title>
</head>

<body>
result <span th:text="${session.user}"> 您好</span>
</body>
</html>


下面做個(gè)例子,有form。(參考項(xiàng)目:bootThymeleafForm)





例4.2:
index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
馬克-to-win@馬克java社區(qū)

<form  action="login" method="post">
       姓名:<input type="text" name="markname" placeholder="用戶名" /><br />
    <button type="submit" >提交</button>
</form>
</body>
</html>

app.java:

package com.SpringbootMaven;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@SpringBootApplication
public class App {

    @RequestMapping("/login")
    public String login(@RequestParam String markname,Model model){
        System.out.println("name is"+markname);
        model.addAttribute("message", markname);
        return "result";
    }
  
    public static void main(String[] args) throws Exception {
        SpringApplication.run(App.class, args);
    }
result.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Title</title>
</head>

<body>
result <span th:text="${message}"> 您好</span>
<div th:text="${message}">用戶名</div>
</body>
</html>

測(cè)試: http://localhost:8080/index.html