spring boot 整合freemaker
前端最好使用vue.js
这里是freemaker 整合spring boot
1.编写pom文件:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
2.编写cotroller:
( 由于要返回模板页面文件,所以我们只能使用@Controller 而不可以使用@RestController)
@RequestMapping("/b")
public String b( Map<String,Object> map) {
Student s1=new Student("a","f",1,"aa");
Student s2=new Student("b","m",2,"bb");
Student s3=new Student("c","m",3,"cc");
Student s4=new Student("d","f",4,"dd");
ArrayList list= new ArrayList();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
map.put("stu",list);
map.put("name","yangtao");
return "views/b";
}
3.编写页面(使用了bootstrap)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Owsome</title>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<div class="row">
<h1>Hello,${name}.这是另外一个界面</h1>
<div class="col-md-6" >
<img style="height: 500px " src="/imags/original.jpg" class=".img-responsive img-circle " alt="error">
</div>
<div class="col-md-6">
<div style="height: 100px"></div>
<table >
<table border="1" class="table" >
<tr>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>地址</td>
</tr>
<#list stu as item>
<tr>
<td>${item.name}</td>
<td>${item.gender}</td>
<td>${item.age}</td>
<td>${item.address}</td>
</tr>
</#list>
</table>
<a href="http://127.0.0.1:8080/c">第三个页面</a>
</div>
</div>
</div>
</body>
</html>
4.编写properties
(也可以使用jsp文件,就是不知它怎么渲染了,先解释成java文件,然后渲染到html 上吗)
spring.freemarker.template-loader-path=classpath:/templates
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.html
5.编写一个实体类
(必须要有get set 方法)
package com.git.springboot.fontandbacktest.demo.contrllor;
/**
* * @description: TODO
* * @param ${tags}
* * @return ${return_type}
* * @throws
* * @author yangtao
* * @date $date$ $time$
*/
public class Student {
String name;
String gender;
String age;
String address;
public Student( String name, String gender, String age, String address ) {
this.name = name;
this.gender = gender;
this.age = age;
this.address = address;
}
。。。。。
get and set 方法
}
出现过的错误
Tip: It's the step after the last dot that caused this error, not those before it.
----
Tip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----
----
FTL stack trace ("~" means nesting-related):
- Failed at: ${item.name} [in template "views/login.html" at line 39, column 29]
2019-09-04 10:08:55.867 ERROR 1406 --- [nio-8080-exec-1] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/a] and exception [The following has evaluated to null or missing:
==> item.name [in template "views/login.html" at line 39, column 31]
----
Tip: It's the step after the last dot that caused this error, not those before it.
----
Tip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----
----
FTL stack trace ("~" means nesting-related):
- Failed at: ${item.name} [in template "views/login.html" at line 39, column 29]
----] as the response has already been committed. As a result, the response may have the wrong status code.
解决办法,必须使用get and set 方法