springboot整合視圖層之freemarker

整合freemarker要求必須將視圖文件放在 src/main/resources下的templates文件夾下,該文件夾是安全的不可直接訪問的,必須由controller之類的接受請求類去跳轉,由於若是直接訪問就意味着須要及時響應,而springboot須要給展現文件去渲染,這須要時間,因此他是不容許被直接訪問的。html

1.pom.xmljava

<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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
  </parent>
  <groupId>com.mr.li</groupId>
  <artifactId>springboot_004</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
      <java.version>1.7</java.version>
  </properties>
  
  <dependencies>
      <!-- 添加啓動器 -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
     
   <!-- freemarker啓動器:使用模板開發-->
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
  </dependencies>
</project>

2.實體類web

package com.mr.li.pojo;

public class User {

    private int id;
    
    private String name;
    
    private int age;

    public User(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    
    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

3.controllerspring

    package com.mr.li.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mr.li.pojo.User;

@Controller
public class MyController {

    @RequestMapping("/show")
    public String show(Model model) {
        //生成jsp頁面須要展現的數據源
        List<User> list = new ArrayList<User>();
        list.add(new User(1, "小明", 15));
        list.add(new User(2, "小紅", 16));
        list.add(new User(3, "小麗", 17));
        //這裏前面的list是在jsp中使用EL表達式所使用的名稱
        model.addAttribute("list", list);
        //這裏返回的是jsp頁面名稱,先後綴在配置文件中
        return "users";
    }
}

5.users.ftl  此文件名字叫users由於controller中show方法返回的名字叫users,裏面的EL表達式中的list是controller類中的model對象的addAttribute方法的key,參數名爲list.apache

<html>
    <head>
        <title>展現用戶數據</title>
        <meta charset="utf-9"></meta>
    </head>
    
    <body>
        
        <table border="1" align="center" width="50%">
            
            <tr>
                
                <th>ID</th>
                <th>Name</th>
                <th>Age</th>
            </tr>
            
            <#list list as user >
                <tr>
                    <td>${user.id}</td>
                    <td>${user.name}</td>
                    <td>${user.age}</td>
                </tr>
            </#list>    
        </table>
    </body>
</html>

訪問的url: http://localhost:8080/show安全

項目結構:springboot

相關文章
相關標籤/搜索