简介

在动态HTML实现上Spring Boot依然可以完美胜任,并且提供了多种模板引擎的默认配置支持,所以在推荐的模板引擎下,我们可以很快的上手开发动态网站。

Spring Boot提供了默认配置的模板引擎主要有以下几种:

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

Spring Boot建议使用这些模板引擎,避免使用JSP,若一定要使用JSP将无法实现Spring Boot的多种特性,具体可见后文:支持JSP的配置

看完本章你将会知道

如何处理动态html模板和引用静态资源

框架搭建步骤

image.png image.png image.png image.png image.png image.png

如图所示,我们最终生成了一个main方法,类上面有一个注解@SpringBootApplication该注解启用了三个功能

  • @EnableAutoConfiguration:启用Spring Boot的自动配置机制
  • @ComponentScan@Component在应用程序所在的程序包上启用扫描(请参阅最佳做法
  • @Configuration:允许在上下文中注册额外的bean或导入其他配置类

配置文件

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.boot</groupId>
	<artifactId>demo-freemark</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>demo-freemark</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.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>

		<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>

application.yml

server:
  port: 1111
spring:
  mvc:
    static-path-pattern: /static/**

HelloFreemark

项目结构

image.png

DemoFreemarkApplication

package com.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoFreemarkApplication {

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


FreemarkController

package com.boot.controller;

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

@Controller
public class FreemarkController {

    @RequestMapping("hello")
    public String hello(Model model){
        model.addAttribute("msg","Freemark");
        return "index";
    }

}

simple.css

.simple{
    color: #ff0000;
}

simple.js

alert('我是一个js的alert');

index.ftl

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/simple.css">
</head>
<body>
<span class="simple">${msg}</span>
<script src="/simple.js"></script>
</body>
</html>

浏览器中输入http://localhost:1111/hello,我们的静态资源被引入成功而且生效 image.png

git地址

gitee

Last Updated: 6/13/2019, 7:10:41 PM