SpringBOot不提倡使用jsp
而是使用模板
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
application.properties:
spring.freemarker.allow-request-override=false
spring.freemarker.request-context-attribute=req
#后缀
spring.freemarker.suffix=.ftl
spring.freemarker.content-type=text/html;charset=utf-8
spring.freemarker.enabled=true
spring.freemarker.cache=false
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.charset=UTF-8
spring.freemarker.number_format=0.##
Controller:
package com.hc.tempboot.controller;
import com.hc.tempboot.model.User;
import com.hc.tempboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("run")
public String run(Model model){
model.addAttribute("users",userService.allUser());
return "run";
}
}
run.ftl:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<#list users as user>
${user.userId}--
${user.userName}
</#list>
</body>
</html>
ftl:
html的el仍然可用。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<#include "head.ftl">
<h1>freemarker</h1>
${uname}
<#-- 模板指令 判断 if-->
<#if sex==0>
男
<#elseif sex==1>
女
<#else>
其他
</#if>
<#list arr as li>
${li}
</#list>
<#list list as li>
${li.uname}----${li.upwd}
</#list>
<hr/>
<a href="find">aaaa</a>
${uname!'admin'}
<#if name??>
${uname}
<#else>
${uname}
</#if>
<input type="button" onclick="javascript:alert('aaa')">
<#include "admin/foot.ftl">
</body>
</html>
<#if>:判断
<#if name??>:不为空
${uname!'admin'}:不等于
其他与普通无异
<#include>:包含另一个页面
ftl之间不能直接跳转