Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build Beetl environment in SpringBoot

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to build a Beetl environment in SpringBoot? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

i. Prepare 1. Dependence

First of all, we need a springboot project, and the basic pom structure is mostly similar.

Org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE UTF-8 UTF-8 Finchley.RELEASE 1.8 org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone false

In this project, we mainly need to introduce two dependency packages, one web and one official beetl-framework-starter. The latest version is 1.2.12.RELEASE.

Org.springframework.boot spring-boot-starter-web com.ibeetl beetl-framework-starter 1.2.12.RELEASE 2. Configuration parameters

Usually we can just use the default thymeleaf parameter configuration. Here are a few common configurations.

Beetl: enabled: true suffix: btlbeetl-beetlsql: dev: true # that is, automatically check for template changes II. Project construction demonstration 1. Project structure

Building a web project is a little different from our previous pure back-end project, where the front-end resources are placed and how to deal with dependent files are fastidious. Here is a regular project structure.

As shown in the figure above, the front-end resource files are placed in the resources directory by default, and there are two directories below

Templates: store the template file, which can be understood as the html written by us. Note that there can be no problem with the file name.

Static: stores static resource files, such as js,css,image

2. Rest service

We provide three interfaces here, mainly to demonstrate three different data binding methods (basically the same as the previous two blog posts)

@ Controllerpublic class IndexController {@ GetMapping (path = {"," / "," / index "}) public ModelAndView index () {Map data = new HashMap (2); data.put (" name "," YiHui Beetl "); data.put (" now ", LocalDateTime.now (). ToString ()); return new ModelAndView (" index.btl ", data) } private static String [] contents = ("the green ants are fragrant, and the yellow flowers are recommended together." \ nThe sky is clean and dust-free. \ nIt is appropriate to have Fu when climbing the mountain, and the play is written with pen. \ nThe garden of Kenai shakes down, and there is no one to discuss the meaning of sorrow and autumn. \ nSome new acquaintances in my eyes. \ nThe Longshan high meeting place, who will fall the hat. ") .split ("\ n"); private static Random random = new Random (); @ GetMapping (path = "show1") public String showOne (Model model) {model.addAttribute ("title", "Linjiang Xian"); model.addAttribute ("content", contents [random.nextInt (6)]); return "show1.btl" } @ GetMapping (path = "show2") public String showTow (Map data) {data.put ("name", "Show2---- >"); data.put ("now", LocalDateTime.now (). ToString ()); return "show2.btl";}}

Of the above three case

The first is the best to understand. When creating a ModelAndView, pass in viewName and data

The second is to set the data passed to view through the interface parameter Model

The third one directly uses Map to transfer data.

Be careful

If you compare it with the previous two blog posts, you will find a significant difference. The previous Freemaker and Thymeleaf do not need a suffix when specifying the view name, but here, you must put a suffix, otherwise it will cause a 500 error.

Three interfaces, corresponding to three btl files, as follows

Index.btl

YiHui's SpringBoot Beetl Demo hello world!

Welcome to ${name}

Current time ${now}

Ref 2 test, Ref 3 test.

Show1.btl

YiHui's SpringBoot Beetl Demo ${title} ${content}

Show2.btl

YiHui's SpringBoot Beetl Demo ${name} ${now}

In the above template file, you need to be careful to refer to the css style file. There is no static in front of the path, and our corresponding css file

Index.css

.title {color: # c00; font-weight: normal; font-size: 2em;} .title {color: darkblue; font-size: 1.2em;} .sign {color: lightgray; font-size: 0.8em; font-style: italic;} 3. Demo

After starting the project, you can see the switch of the three pages. The data in the template will be replaced according to the return of the backend, especially the time of the home page, and each refresh will change accordingly.

This is the answer to the question about how to build the Beetl environment in SpringBoot. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report