1. Beetle
介绍
Beetl
是Bee Template Language
的缩写,作者李家智。
声称性能五倍于Freemarker
。
采用类似于JavaScript
的语法与习俗,使得简单易用。
2. Beetle
使用
2.1 安装
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-framework-starter</artifactId>
<version>1.1.63.RELEASE</version>
</dependency>
在application.xml
和启动类中无需添加额外的配置与注解。
2.2 Controller
在Controller
中返回视图时,指定对应模板就好了。
@Controller
@Slf4j
public class IndexController {
@GetMapping(value = {"", "/"})
public ModelAndView index(HttpServletRequest request) {
ModelAndView mv = new ModelAndView();
User user = (User) request.getSession().getAttribute("user");
if (ObjectUtil.isNull(user)) {
mv.setViewName("redirect:/user/login");
} else {
mv.setViewName("page/index.btl");
mv.addObject(user);
}
return mv;
}
}
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {
@PostMapping("/login")
public ModelAndView login(User user, HttpServletRequest request) {
ModelAndView mv = new ModelAndView();
mv.addObject(user);
mv.setViewName("redirect:/");
request.getSession().setAttribute("user", user);
return mv;
}
@GetMapping("/login")
public ModelAndView login() {
return new ModelAndView("page/login.btl");
}
}
2.3 模板
src\main\resources\templates\common\head.html
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>spring-boot-demo-template-beetl</title>
</head>
src\main\resources\templates\page\login.btl
<!doctype html>
<html lang="en">
<% include("/common/head.html"){} %>
<body>
<div id="app" style="margin: 20px 20%">
<form action="/demo/user/login" method="post">
用户名<input type="text" name="name" placeholder="用户名"/>
密码<input type="password" name="password" placeholder="密码"/>
<input type="submit" value="登录">
</form>
</div>
</body>
</html>
src\main\resources\templates\page\index.btl
<!doctype html>
<html lang="en">
<% include("/common/head.html"){} %>
<body>
<div id="app" style="margin: 20px 20%">
欢迎登录,${user.name}!
</div>
</body>
</html>
2.4 Beetle语法
更多语法请参考:Beetle中文文档