Spring boot uses velocity, freemarker template to create HTML page to return to front end __html

Source: Internet
Author: User
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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.