add: 添加starter-beetl,并添加对应的demo

change: 把nutzboot-demo-simple统统放到一个文件夹内
This commit is contained in:
Wendal Chen 2017-11-23 14:42:14 +08:00
parent 7aa549fbf5
commit 42187fa158
31 changed files with 209 additions and 167 deletions

View File

@ -76,7 +76,7 @@ public class MainLauncher {
- [ ] 计划任务
- [x] [Quartz](http://www.quartz-scheduler.org)
- [ ] 模板引擎
- [ ] [Beetl](http://ibeetl.com/)
- [x] [Beetl](http://ibeetl.com/)
- [ ] [jetbrick-template](https://github.com/subchen/jetbrick-template-2x)
- [ ] Vecloity
- [ ] FreeMarker

View File

@ -1,49 +0,0 @@
package io.nutz.demo.simple;
import java.util.List;
import org.nutz.boot.NbApp;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import io.nutz.demo.simple.bean.User;
@IocBean(create="init")
public class MainLauncher {
@Inject
Dao dao;
@Ok("raw")
@At("/user/count")
public long count() {
return dao.count(User.class);
}
@Ok("json:full")
@At("/user/query")
public List<User> query(@Param("..")Pager pager) {
return dao.query(User.class, Cnd.orderBy().asc("age"), pager);
}
public void init() {
dao.create(User.class, true);
dao.insert(new User("apple", 40, "北京"));
dao.insert(new User("ball", 30, "未知"));
dao.insert(new User("cat", 50, "温哥华"));
dao.insert(new User("fox", 51, "纽约"));
dao.insert(new User("bra", 25, "济南"));
dao.insert(new User("lina", 50, "深圳"));
}
public static void main(String[] args) throws Exception {
new NbApp(MainLauncher.class).run();
}
}

View File

@ -1,51 +0,0 @@
package io.nutz.demo.simple.bean;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Name;
import org.nutz.dao.entity.annotation.Table;
@Table("t_user")
public class User {
@Id
private long id;
@Name
private String name;
private int age;
private String location;
public User() {
}
public User(String name, int age, String location) {
super();
this.name = name;
this.age = age;
this.location = location;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}

View File

@ -3,10 +3,10 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-demo</artifactId>
<artifactId>nutzboot-demo-simple</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<artifactId>nutzboot-demo-simple-weixin</artifactId>
<artifactId>nutzboot-demo-simple-beetl</artifactId>
<dependencies>
<dependency>
<groupId>org.nutz</groupId>
@ -15,7 +15,12 @@
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-weixin</artifactId>
<artifactId>nutzboot-starter-jetty</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-beetl</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
@ -28,15 +33,5 @@
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-jetty</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,24 @@
package io.nutz.demo.simple;
import org.nutz.boot.NbApp;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
@IocBean
public class MainLauncher {
@At({"/", "/index"})
@Ok("beetl:/index.html")
public NutMap index() {
NutMap obj = new NutMap();
obj.setv("name", "NB").setv("age", 18);
return obj;
}
public static void main(String[] args) throws Exception {
new NbApp(MainLauncher.class).run();
}
}

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>NB demo for Beetl</title>
</head>
<body>
<div>
<h2>Context Path = '${base}'</h2>
</div>
<div>
<h2>From Action name=${obj.name}, age=${obj.age}</h2>
</div>
</body>
</html>

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-demo</artifactId>
<artifactId>nutzboot-demo-simple</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<artifactId>nutzboot-demo-simple-dao</artifactId>

View File

@ -0,0 +1,37 @@
<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>
<parent>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-demo-simple</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<artifactId>nutzboot-demo-simple-mvc</artifactId>
<dependencies>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-nutz-mvc</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-jetty</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-demo</artifactId>
<artifactId>nutzboot-demo-simple</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<artifactId>nutzboot-demo-simple-redis</artifactId>
@ -28,15 +28,15 @@
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>

View File

@ -1,37 +1,25 @@
<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>
<parent>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-demo</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<artifactId>nutzboot-demo-simple</artifactId>
<dependencies>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-nutz-mvc</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-jetty</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
<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>
<parent>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-demo</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<artifactId>nutzboot-demo-simple</artifactId>
<packaging>pom</packaging>
<modules>
<module>nutzboot-demo-simple-mvc</module>
<module>nutzboot-demo-simple-dao</module>
<module>nutzboot-demo-simple-redis</module>
</modules>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -1,10 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello, So NB!</title>
</head>
<body>
Hello, So NB!
</body>
</html>

View File

@ -0,0 +1,22 @@
<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>
<parent>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-parent</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<artifactId>nutzboot-starter-beetl</artifactId>
<dependencies>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.7.23</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,68 @@
package org.nutz.boot.starter.beetl;
import java.io.IOException;
import java.util.Properties;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.nutz.BeetlViewMaker;
import org.beetl.ext.nutz.LogErrorHandler;
import org.beetl.ext.web.WebRender;
import org.nutz.boot.AppContext;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
@IocBean(name="$views_beetl", create="init")
public class BeetlViewMakerStarter extends BeetlViewMaker {
private static final Log log = Logs.get();
@Inject
protected PropertiesProxy conf;
@Inject
protected AppContext appContext;
public BeetlViewMakerStarter() throws IOException {
super();
}
public void init() throws IOException {
if (conf == null)
return;
log.debug("beetl init ....");
Configuration cfg = Configuration.defaultConfiguration();
Properties prop = new Properties();
for (String key : conf.keySet()) {
if (key.startsWith("beetl.")) {
prop.put(key.substring("beetl.".length()), conf.get(key));
}
}
if (!prop.containsKey(Configuration.DIRECT_BYTE_OUTPUT)) {
// 默认启用DIRECT_BYTE_OUTPUT,除非用户自定义, 一般不会.
log.debug("no custom DIRECT_BYTE_OUTPUT found , set to true");
// 当DIRECT_BYTE_OUTPUT为真时, beetl渲染会通过getOutputStream获取输出流
// 而BeetlView会使用LazyResponseWrapper代理getOutputStream方法
// 从而实现在模板输出之前,避免真正调用getOutputStream
// 这样@Fail视图就能正常工作了
cfg.setDirectByteOutput(true);
}
if (!prop.containsKey(Configuration.ERROR_HANDLER)) {
// 没有自定义ERROR_HANDLER,用定制的
cfg.setErrorHandlerClass(LogErrorHandler.class.getName());
}
groupTemplate = new GroupTemplate(cfg);
if (!prop.containsKey(Configuration.RESOURCE_LOADER)) {
// 默认选用WebAppResourceLoader,除非用户自定义了RESOURCE_LOADER
log.debug("no custom RESOURCE_LOADER found , select ClasspathResourceLoader");
groupTemplate.setResourceLoader(new ClasspathResourceLoader(appContext.getClassLoader(), prop.getProperty("root", "template/")));
}
render = new WebRender(groupTemplate);
log.debug("beetl init complete");
}
}

View File

@ -0,0 +1 @@
org.nutz.boot.starter.beetl.BeetlViewMakerStarter

View File

@ -61,6 +61,7 @@
<module>nutzboot-starter-wkcache</module>
<module>nutzboot-starter-redis</module>
<module>nutzboot-starter-disque</module>
<module>nutzboot-starter-beetl</module>
</modules>
<repositories>
<repository>