Browse Source

spring boot 集成 Beetl 模板引擎

v-1.5.x
Yangkai.Shen 6 years ago
parent
commit
b6d5c69d76
9 changed files with 340 additions and 0 deletions
  1. +1
    -0
      spring-boot-demo-parent/pom.xml
  2. +145
    -0
      spring-boot-demo-template-beetl/README.md
  3. +32
    -0
      spring-boot-demo-template-beetl/pom.xml
  4. +12
    -0
      spring-boot-demo-template-beetl/src/main/java/com/xkcoding/springbootdemotemplatebeetl/SpringBootDemoTemplateBeetlApplication.java
  5. +37
    -0
      spring-boot-demo-template-beetl/src/main/java/com/xkcoding/springbootdemotemplatebeetl/controller/HomeController.java
  6. +30
    -0
      spring-boot-demo-template-beetl/src/main/java/com/xkcoding/springbootdemotemplatebeetl/model/User.java
  7. +5
    -0
      spring-boot-demo-template-beetl/src/main/resources/application.yml
  8. +62
    -0
      spring-boot-demo-template-beetl/src/main/resources/templates/index.btl
  9. +16
    -0
      spring-boot-demo-template-beetl/src/test/java/com/xkcoding/springbootdemotemplatebeetl/SpringBootDemoTemplateBeetlApplicationTests.java

+ 1
- 0
spring-boot-demo-parent/pom.xml View File

@@ -30,6 +30,7 @@
<module>../spring-boot-demo-war</module>
<module>../spring-boot-demo-util</module>
<module>../spring-boot-demo-elasticsearch</module>
<module>../spring-boot-demo-template-beetl</module>
</modules>

<parent>


+ 145
- 0
spring-boot-demo-template-beetl/README.md View File

@@ -0,0 +1,145 @@
# spring-boot-demo-template-beetl

依赖 [spring-boot-demo-parent](../spring-boot-demo-parent) 、`beetl-framework-starter`

## pom.xml

```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-demo-template-beetl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-demo-template-beetl</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>com.xkcoding</groupId>
<artifactId>spring-boot-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../spring-boot-demo-parent/pom.xml</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-framework-starter</artifactId>
<version>1.1.46.RELEASE</version>
</dependency>
</dependencies>

<build>
<finalName>spring-boot-demo-template-beetl</finalName>
</build>

</project>
```

## HomeController.java

```java
/**
* <p>
* 首页 Controller
* </p>
*
* @package: com.xkcoding.springbootdemotemplatebeetl.controller
* @description: 首页 Controller
* @author: yangkai.shen
* @date: Created in 2018/4/26 下午4:36
* @copyright: Copyright (c) 2018
* @version: V1.0
* @modified: yangkai.shen
*/
@Controller
public class HomeController {

@GetMapping({"", "/", "/index"})
public ModelAndView index() {
ModelAndView view = new ModelAndView("/index.btl");
User admin = new User(0, "admin", "phone0",true);
List<User> userList = Lists.newArrayList(new User(1, "user1", "phone1",false), new User(2, "user2", "phone2",true), new User(3, "user3", "phone3",true),new User(4, "user4", "phone4",false));

view.addObject("admin",admin);
view.addObject("userList",userList);
return view;
}
}
```

## index.btl

```html
<!doctype html>
<html lang="en">
<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>Beetl demo</title>
</head>
<body>
this is index.btl to show some beetl demos!<br>

<!--demo01-->
<%
/* demo01 */
var a = 1;
var b = 2;
var result = a + b;
%>
1+2=${result}<br>

<!--demo02-->
<%
/* demo02 */
print(111);
%>

<!--demo03-->
欢迎登录,${admin.admin?"管理员":"员工"} ${admin.name}<br>

<!--demo04-->
<table>
<thead>
<tr>
<th>序号</th>
<th>编号</th>
<th>姓名</th>
<th>手机</th>
<th>职位</th>
<th>注册日期</th>
</tr>
</thead>
<tbody>
<%for(u in userList){%>
<tr>
<td>${uLP.index}</td>
<td>${u.id}</td>
<td>${u.name}</td>
<td>${u.tel}</td>
<td>${u.admin?"管理员":"员工"}</td>
<%if(uLP.even){%>
<td><%var today = date();%> ${today,dateFormat="yyyy-MM-dd HH:mm:ss"}</td>
<%}else{%>
<td><%var today = date();%> ${today,dateFormat="yyyy-MM-dd"}</td>
<%}%>
</tr>
<%}%>
</tbody>
</table>

</body>
</html>
```

## Beetl 语法

请查看 Beetl 官网:

http://ibeetl.com/guide/#beetl

+ 32
- 0
spring-boot-demo-template-beetl/pom.xml View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-demo-template-beetl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-demo-template-beetl</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>com.xkcoding</groupId>
<artifactId>spring-boot-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../spring-boot-demo-parent/pom.xml</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-framework-starter</artifactId>
<version>1.1.46.RELEASE</version>
</dependency>
</dependencies>

<build>
<finalName>spring-boot-demo-template-beetl</finalName>
</build>

</project>

+ 12
- 0
spring-boot-demo-template-beetl/src/main/java/com/xkcoding/springbootdemotemplatebeetl/SpringBootDemoTemplateBeetlApplication.java View File

@@ -0,0 +1,12 @@
package com.xkcoding.springbootdemotemplatebeetl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootDemoTemplateBeetlApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootDemoTemplateBeetlApplication.class, args);
}
}

+ 37
- 0
spring-boot-demo-template-beetl/src/main/java/com/xkcoding/springbootdemotemplatebeetl/controller/HomeController.java View File

@@ -0,0 +1,37 @@
package com.xkcoding.springbootdemotemplatebeetl.controller;

import com.google.common.collect.Lists;
import com.xkcoding.springbootdemotemplatebeetl.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
* <p>
* 首页 Controller
* </p>
*
* @package: com.xkcoding.springbootdemotemplatebeetl.controller
* @description: 首页 Controller
* @author: yangkai.shen
* @date: Created in 2018/4/26 下午4:36
* @copyright: Copyright (c) 2018
* @version: V1.0
* @modified: yangkai.shen
*/
@Controller
public class HomeController {

@GetMapping({"", "/", "/index"})
public ModelAndView index() {
ModelAndView view = new ModelAndView("/index.btl");
User admin = new User(0, "admin", "phone0",true);
List<User> userList = Lists.newArrayList(new User(1, "user1", "phone1",false), new User(2, "user2", "phone2",true), new User(3, "user3", "phone3",true),new User(4, "user4", "phone4",false));

view.addObject("admin",admin);
view.addObject("userList",userList);
return view;
}
}

+ 30
- 0
spring-boot-demo-template-beetl/src/main/java/com/xkcoding/springbootdemotemplatebeetl/model/User.java View File

@@ -0,0 +1,30 @@
package com.xkcoding.springbootdemotemplatebeetl.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
* <p>
* 用户实体类
* </p>
*
* @package: com.xkcoding.springbootdemotemplatebeetl.model
* @description: 用户实体类
* @author: yangkai.shen
* @date: Created in 2018/4/26 下午4:46
* @copyright: Copyright (c) 2018
* @version: V1.0
* @modified: yangkai.shen
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private Integer id;
private String name;
private String tel;
private Boolean admin;
}

+ 5
- 0
spring-boot-demo-template-beetl/src/main/resources/application.yml View File

@@ -0,0 +1,5 @@
server:
port: 8080
context-path: /demo
beetlSql:
enabled: false

+ 62
- 0
spring-boot-demo-template-beetl/src/main/resources/templates/index.btl View File

@@ -0,0 +1,62 @@
<!doctype html>
<html lang="en">
<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>Beetl demo</title>
</head>
<body>
this is index.btl to show some beetl demos!<br>

<!--demo01-->
<%
/* demo01 */
var a = 1;
var b = 2;
var result = a + b;
%>
1+2=${result}<br>

<!--demo02-->
<%
/* demo02 */
print(111);
%>

<!--demo03-->
欢迎登录,${admin.admin?"管理员":"员工"} ${admin.name}<br>

<!--demo04-->
<table>
<thead>
<tr>
<th>序号</th>
<th>编号</th>
<th>姓名</th>
<th>手机</th>
<th>职位</th>
<th>注册日期</th>
</tr>
</thead>
<tbody>
<%for(u in userList){%>
<tr>
<td>${uLP.index}</td>
<td>${u.id}</td>
<td>${u.name}</td>
<td>${u.tel}</td>
<td>${u.admin?"管理员":"员工"}</td>
<%if(uLP.even){%>
<td><%var today = date();%> ${today,dateFormat="yyyy-MM-dd HH:mm:ss"}</td>
<%}else{%>
<td><%var today = date();%> ${today,dateFormat="yyyy-MM-dd"}</td>
<%}%>
</tr>
<%}%>
</tbody>
</table>

</body>
</html>

+ 16
- 0
spring-boot-demo-template-beetl/src/test/java/com/xkcoding/springbootdemotemplatebeetl/SpringBootDemoTemplateBeetlApplicationTests.java View File

@@ -0,0 +1,16 @@
package com.xkcoding.springbootdemotemplatebeetl;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDemoTemplateBeetlApplicationTests {

@Test
public void contextLoads() {
}

}

Loading…
Cancel
Save