温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot中怎么搭建Beetl环境

发布时间:2021-07-08 16:34:41 来源:亿速云 阅读:142 作者:Leah 栏目:大数据

SpringBoot中怎么搭建Beetl环境,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

I. 准备

1. 依赖

首先我们是需要一个springboot项目,基本的pom结构大都相似

<parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.0.4.RELEASE</version>
    <relativepath /> <!-- lookup parent from update -->
</parent>

<properties>
    <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
    <project.reporting.outputencoding>UTF-8</project.reporting.outputencoding>
    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    <java.version>1.8</java.version>
</properties>

<build>
    <pluginmanagement>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </pluginmanagement>
</build>
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

在这个项目中,我们主要需要引入两个依赖包,一个web,一个官方提供的beetl-framework-starter,当前最新的版本为 1.2.12.RELEASE

<dependencies>
    <dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
    <dependency>
        <groupid>com.ibeetl</groupid>
        <artifactid>beetl-framework-starter</artifactid>
        <version>1.2.12.RELEASE</version>
    </dependency>
</dependencies>

2. 配置参数

通常我们直接使用默认的thymeleaf参数配置即可,下面给出几个常用的配置

beetl:
  enabled: true
  suffix: btl
beetl-beetlsql:
  dev: true # 即自动检查模板变化

II. 项目搭建演示

1. 项目结构

搭建一个web项目和我们之前的纯后端项目有点不一样,前端资源放在什么地方,依赖文件怎么处理都是有讲究的,下面是一个常规的项目结构

SpringBoot中怎么搭建Beetl环境

如上图,前端资源文件默认放在resources目录下,下面有两个目录

  • templates:存放模板文件,可以理解为我们编写的html,注意这个文件名不能有问题

  • static: 存放静态资源文件,如js,css,image等

2. Rest服务

我们这里提供了三个接口,主要是为了演示三种不同的数据绑定方式(和前面两篇博文基本一样)

@Controller
public class IndexController {

    @GetMapping(path = {"", "/", "/index"})
    public ModelAndView index() {
        Map<string, object> data = new HashMap&lt;&gt;(2);
        data.put("name", "YiHui Beetl");
        data.put("now", LocalDateTime.now().toString());
        return new ModelAndView("index.btl", data);
    }

    private static String[] contents =
            ("绿蚁浮觞香泛泛,黄花共荐芳辰。\n清霜天宇净无尘。\n登高宜有赋,拈笔戏成文。\n可奈园林摇落尽,悲秋意与谁论。\n眼中相识几番新。\n龙山高会处,落帽定何人。").split("\n");
    private static Random random = new Random();

    @GetMapping(path = "show1")
    public String showOne(Model model) {
        model.addAttribute("title", "临江仙");
        model.addAttribute("content", contents[random.nextInt(6)]);
        return "show1.btl";
    }

    @GetMapping(path = "show2")
    public String showTow(Map<string, object> data) {
        data.put("name", "Show2----&gt;");
        data.put("now", LocalDateTime.now().toString());
        return "show2.btl";
    }
}

上面的三种case中

  • 第一个是最好理解的,在创建ModelAndView时,传入viewName和数据

  • 第二个是通过接口参数Model,设置传递给view的数据

  • 第三种则直接使用Map来传递数据

注意

如果和前面两篇博文进行对比,会发现一个显著的区别,之前的Freemaker, Thymeleaf指定视图名的时候,都不需要后缀,但是这里,必须带上后缀,否则会500错误


三个接口,对应的三个btl文件,如下

index.btl

<meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="SpringBoot Beetl">
    <meta name="author" content="YiHui">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>YiHui's SpringBoot Beetl Demo</title>
    <link rel="stylesheet" href="index.css">



<div>
    <div class="title">hello world!</div>
    <br>
    <div class="content">欢迎访问  ${name}</div>
    <br>
    <div class="sign">当前时间 ${now}</div>
    <br>
    <a href="show1">传参2测试</a> &nbsp;&nbsp;&nbsp;&nbsp;
    <a href="show2">传参3测试</a>
</div>

show1.btl

<meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="SpringBoot Beetl">
    <meta name="author" content="YiHui">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>YiHui's SpringBoot Beetl Demo</title>
    <link rel="stylesheet" href="index.css">



<div>
    <div class="title">${title}</div>
    <div class="content">${content}</div>
</div>

show2.btl

<meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="SpringBoot Beetl">
    <meta name="author" content="YiHui">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>YiHui's SpringBoot Beetl Demo</title>
    <link rel="stylesheet" href="index.css">



<div>
    <div class="title">${name}</div>
    <div class="content">${now}</div>
</div>

在上面的模板文件中,需要注意引用css样式文件,路径前面并没有static,我们对应的css文件

index.css

.title {
    color: #c00;
    font-weight: normal;
    font-size: 2em;
}

.content {
    color: darkblue;
    font-size: 1.2em;
}

.sign {
    color: lightgray;
    font-size: 0.8em;
    font-style: italic;
}

3. 演示

启动项目后,可以看到三个页面的切换,模板中的数据根据后端的返回替换,特别是主页的时间,每次刷新都会随之改变

SpringBoot中怎么搭建Beetl环境

关于SpringBoot中怎么搭建Beetl环境问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI