The previous article said, created the first springboot project, now talk about Springboot's Web+freemarker project;
1, first introduce the dependence
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-web</artifactid> <version>${spring.boot.version}</version> </ dependency> <dependency> <groupId>org.springframework.boot</groupId> < Artifactid>spring-boot-starter-freemarker</artifactid> <version>${spring.boot.version}</ Version> </dependency>
2. Create a application class to start the Web
Import Org.springframework.boot.springapplication;import org.springframework.boot.autoconfigure.springbootapplication;/** * Created by LK on 2016/5/7. */@SpringBootApplicationpublic class Samplewebfreemarkerapplication {public static void Main (string[] args) { Springapplication.run (Samplewebfreemarkerapplication.class,args); }}
3. Create a controller class to direct to the view
Import Org.springframework.beans.factory.annotation.value;import Org.springframework.stereotype.Controller; Import Org.springframework.web.bind.annotation.requestmapping;import java.util.date;import java.util.Map;/** * Created by LK on 2016/5/7. */@Controllerpublic class Webcontrpller { @Value ("${application.message:1234556677}") private String message = "Hi,hello world ..."; @RequestMapping ("/") Public String Web (map<string,object> model) { model.put ("Time", New Date ()); Model.put ("message", this.message); Return "web";//the content returned is templetes the name of the following file }}
4, in the resources directory to create the Templates folder, the folder contains two template files
4.1 ERROR.FTL
<! DOCTYPE html>
4.2 WEB.FTL<! DOCTYPE html>
5, start Samplewebfreemarkerapplication, the browser above the input http://127.0.0.1:8080/can output the page as follows:
My second Springboot project Web+freemarker