上一篇文章讲了,创建了第一个springboot的项目,现在讲一下springboot的web+freemarker的项目;
1、首先引入依赖
<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、创建一个Application的类,用以启动web
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by LK on 2016/5/7. */ @SpringBootApplication public class SampleWebFreeMarkerApplication { public static void main(String[] args) { SpringApplication.run(SampleWebFreeMarkerApplication.class,args); } }
3、创建一个Controller类,用来定向到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. */ @Controller public 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";//返回的内容就是templetes下面文件的名称 } }
4、在resources目录下创建templates文件夹,文件夹里面存放两个模板文件
4.1 error.ftl
<!DOCTYPE html> <html lang="en"> <body> Something wrong: ${status} ${error} </body> </html>
4.2 web.ftl
<!DOCTYPE html> <html lang="en"> <body> Date: ${time?date} <br> Time: ${time?time} <br> Message: ${message} </body> </html>
5、启动 SampleWebFreeMarkerApplication ,在浏览器上面输入 http://127.0.0.1:8080/ 即可输出页面如下:
时间: 2024-10-10 07:26:34