springboot2.0 集成freemark

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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>

	<groupId>com.freemark</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>freemarkDemo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- 使用log4j2 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-log4j2</artifactId>
		</dependency>


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

controller测试类:

package com.freemark.demo;


import com.freemark.bean.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

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

/**
 * Created by  lpw'ASUS on 2018/5/24.
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    @RequestMapping("show")
    public String show(Model model){
        model.addAttribute("str11","hello springboot2.0 -freemark");
        UserInfo userInfo =new UserInfo(12,"李四");
        UserInfo userInfo1 =new UserInfo(18,"王五");

//        model.addAttribute("user",userInfo);
        List<UserInfo> list =new ArrayList<UserInfo>();
        list.add(userInfo);
        list.add(userInfo1);
        model.addAttribute("list",list);
        return "template";
    }
}

html页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>freemark template</title>
</head>
<body>
    <h3> hello freemark</h3>
    <p>
        ${str11}
    </p>
    <hr/>
    <#if user??>
    用户为:${user.id}<br/>
    ${user.name}
        <#else>
            user不存在
    </#if>
    <hr/>
    <hr/>
    <#list list as user>
        id为:${user.id}<br/>
        ${user.name}<br/>
        ==============
    </#list>
</body>
</html>
其中,#if中的??代表是否为空,不为空执行里面内容,else嵌套在if里面。




猜你喜欢

转载自blog.csdn.net/m0_38044453/article/details/80445202