In a few simple steps, implement the use of the velocity or freemarker template to construct the page in spring boot and return to the front end:
1. Introduce dependencies and create templates directory:
velocity:http://blog.csdn.net/clementad/article/details/51819647
freemarker:http://blog.csdn.net/clementad/article/details/51942629
2. Create template file:
Create two files, one corresponding to the normal page, file name:
Velocity:welcome.vm
FreeMarker:welcome.ftl
<! DOCTYPE html>
A page that corresponds to the error (automatically outputs this page when ULR input error, FileName: ERROR.VM or ERROR.FTL):
<! DOCTYPE html>
3, CONTROLLER code:
Package Com.xjj.web.controller;
Import Java.util.Map;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Com.xjj.util.XDateUtils;
@Controller
@RequestMapping ("/web") public
class Webcontroller {
@RequestMapping ("/hi")
Public String hi (map<string, object> model) {
model.put ("Time", xdateutils.nowtostring ());
Model.put ("message", "This is the content of the test ...) ");
Model.put ("Tousername", "John");
Model.put ("Fromusername", "Old Xu");
return "Welcome"; Automatically look for resources/templates in the name of WELCOME.VM file as a template, assembled after the return
}
}
4, testing:
Enter Url:http://localhost:8082/web/hi in Browser
Results:
If you enter a URL that does not exist, such as Http://localhost:8082/web/hello, the result is as follows:
Source Code Reference: Https://github.com/xujijun/my-spring-boot
attached:
Velocity Official Website: http://velocity.apache.org/
Velocity Grammar Reference: http://velocity.apache.org/engine/devel/vtl-reference.html
Freemarker Official website: http://freemarker.org/