2018年7月16日 星期一

整合 JSP、Freemarker、Thymeleaf (SpringBoot 2.x 四)

※JSP

※build.gradle

dependencies {
    compile 'org.springframework.boot:spring-boot-starter'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'javax.servlet:jstl'
    compile 'org.apache.tomcat.embed:tomcat-embed-jasper'
}

※增加下面兩個


※java bean

public class Animal {
    private Integer id;
    private String name;
    
    public Animal(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    
    public Integer getId() {
        return id;
    }
    
    public String getName() {
        return name;
    }
}




※controller

@Controller
@RequestMapping("/zoo")
public class AnimalAction {
    @RequestMapping("/animalInfo")
    public String displayAnimal(Model model) {
        List<Animal> list = new ArrayList<>();
        list.add(new Animal(1, "tiger"));
        list.add(new Animal(2, "horse"));
        list.add(new Animal(3, "lion"));
    
        model.addAttribute("animalList", list);
        return "zoo"; // "/WEB-INF/jsp/zoo.jsp"
    }
}




※zoo.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<table border="1" align="center" width="30%">
    <tr>
        <th>id</th>
        <th>name</th>
    </tr>
    
    <c:forEach items="${animalList}" var="a">
        <tr>
            <td align="center"><c:out value="${a.id}" /></td>
            <td align="center">${a.name}</td>
        </tr>
    </c:forEach>
</table>

※注意 taglib 的uri 有 jsp 的才是 jsp 2.x的,才有支援 EL

※c:forEach 屬性不能空格,items 必需用 EL


※application.properties

server.port=9000
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

※增加兩個 viewResolver,如果不加,那 controller 回傳可用註解的部分


※測試

@SpringBootApplication
public class Test {
    public static void main(String[] args) {
        SpringApplication.run(Test.class, args);
    }
}





※Freemarker

和 整合 JSP 差不多,官網手冊

※build.gradle

compile 'org.springframework.boot:spring-boot-starter-freemarker'




※zoo.ftl

<html>
    <body>
        <table border="1" align="center" width="50%">
            <tr>
                <th>id</th>
                <th>name</th>
            </tr>
    
            <#list animalList as a>
                <tr>
                    <td>${a.id}</td>
                    <td>${a.name}</td>
                </tr>
            </#list>    
        </table>
    </body>
</html>




※application.properties

spring.freemarker.prefix=
spring.freemarker.suffix=.ftl

※測試類和 controller 和 整合 JSP 一樣

官網有說明,在倒數第二個圓點,ftl 檔必需放在 classpath:/templates/下,所以上面兩個都是預設值,還有很多 freemarker 的設定,看官網,下面有 FREEMARKER



※Thymeleaf

※thyme 和 time 發音相同,百里香的意思,thymeleaf 為百里香的葉子
語法可參考官網

※build.gradle

buildscript {
    ext {
        springBootVersion = '2.0.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
    
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
    
group = 'sbt'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
    
repositories {
    mavenCentral()
}
    
dependencies {
    compile 'org.springframework.boot:spring-boot-starter'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
    compile 'org.springframework.boot:spring-boot-starter-web'
    // compile 'javax.servlet:jstl'
    // compile 'org.apache.tomcat.embed:tomcat-embed-jasper'
    // compile 'org.springframework.boot:spring-boot-starter-freemarker'
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
}




※java bean 和 controller

public class Animal {
    private Integer id;
    private String name;
    // getter...
    
    public Animal(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
}
    
    
@Controller
@RequestMapping("/zoo")
public class AnimalAction {
    @RequestMapping("/animalInfo")
    public String displayAnimal(Model model) {
        List<Animal> list = new ArrayList<>();
        list.add(new Animal(1, "tiger"));
        list.add(new Animal(2, "horse"));
        list.add(new Animal(3, "lion"));
        model.addAttribute("animalList", list);
    
        model.addAttribute("name", "leopard");
        return "zoo";
    }
}




※zoo.html

<div th:text="可以寫死"></div>
<div th:text="${name}"></div>
<input th:value="${name}" />
    
<table border="1" align="center" width="50%">
    <tr>
        <th>id</th>
        <th>name</th>
    </tr>
    
    <tr th:each="a : ${animalList}">
        <td th:text="${a.id}" />
        <td th:text="${a.name}" />
    </tr>
</table>

※th 會有警告,不想看到可在 html 標籤加屬性,如 <html xmlns:th="http://www.thymeleaf.org">


※測試類

@SpringBootApplication
public class Test {
    public static void main(String[] args) {
        SpringApplication.run(Test.class, args);
    }
}

※可參考上面的 PDF 連結,3~7章為基礎語法,公用方法在19章

※還有取得 request、session…等作用域的,要看這裡

沒有留言:

張貼留言