首页 > 编程语言 > 详细

我的第二个springboot项目 web+freemarker

时间:2016-05-13 02:55:17      阅读:131      评论:0      收藏:0      [点我收藏+]

上一篇文章讲了,创建了第一个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/ 即可输出页面如下:

技术分享






我的第二个springboot项目 web+freemarker

原文:http://blog.csdn.net/lk10207160511/article/details/51336791

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!