Blade 是一个轻量级的MVC框架. 它拥有简洁的代码,优雅的设计。 如果你喜欢,欢迎 Star and Fork, 谢谢!
- 轻量级。代码简洁,结构清晰,更容易开发
- 模块化(你可以选择使用哪些组件)
- 插件扩展机制
- Restful风格的路由接口
- 多种配置文件支持(当前支持properties、json和硬编码)
- 内置Jetty服务,模板引擎支持
- 支持JDK1.6或者更高版本
- 简洁的:框架设计简单,容易理解,不依赖于更多第三方库。Blade框架目标让用户在一天内理解并使用。
- 优雅的:
blade
支持 REST 风格路由接口, 提供 DSL 语法编写,无侵入式的拦截器。
开始之前,首先 引入Blade的库文件 :
Maven
配置:
<dependency>
<groupId>com.bladejava</groupId>
<artifactId>blade-core</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.bladejava</groupId>
<artifactId>blade-startup</artifactId>
<version>1.0.1</version>
</dependency>
或者 Gradle
:
compile 'com.bladejava:blade-core:1.6.2'
compile 'com.bladejava:blade-startup:1.0.1'
编写 Main
函数:
public static void main(String[] args) {
Blade blade = me();
blade.get("/", (request, response) -> {
response.html("<h1>Hello blade!</h1>");
});
blade.listen(9001).start();
}
用浏览器打开 http://localhost:9001 这样就可以看到第一个Blade应用了!
public static void main(String[] args) {
Blade blade = me();
blade.get("/user/21", getxxx);
blade.post("/save", postxxx);
blade.delete("/del/21", deletexxx);
blade.put("/put", putxxx);
blade.listen(9001).start();
}
public static void main(String[] args) {
Blade blade = me();
blade.get("/user/:uid", (request, response) -> {
Integer uid = request.paramAsInt("uid");
response.text("uid : " + uid);
});
blade.get("/users/:uid/post/:pid", (request, response) -> {
Integer uid = request.paramAsInt("uid");
Integer pid = request.paramAsInt("pid");
String msg = "uid = " + uid + ", pid = " + pid;
response.text(msg);
});
blade.listen(9001).start();
}
public static void main(String[] args) {
Blade blade = me();
blade.get("/user", (request, response) -> {
Integer uid = request.queryAsInt("uid");
response.text("uid : " + uid);
});
blade.listen(9001).start();
}
public void upload_img(Request request, Response response){
FileItem[] fileItems = request.files();
if(null != fileItems && fileItems.length > 0){
FileItem fileItem = fileItems[0];
File file = fileItem.getFile();
String fileRealPath = "your upload file path!";
nioTransferCopy(file, fileRealPath);
}
}
route.conf
GET / IndexRoute.home
GET /signin IndexRoute.show_signin
POST /signin IndexRoute.signin
GET /signout IndexRoute.signout
POST /upload_img UploadRoute.upload_img
public static void main(String[] args) {
Blade blade = me();
blade.before("/.*", (request, response) -> {
System.out.println("before...");
});
blade.listen(9001).start();
}
// query
List<Post> posts =
AR.find("where title like ? order by id desc limit ?,?", title, page, count).list(Post.class);
// save
String insertSql = "insert into t_post (title, content, view_count, create_time) values (?,?,?,?)";
AR.update(insertSql, title, content, 0, new Date()).commit();
// update
AR.update("update t_post set title = ? and content = ? where id = ?",title, content, id).commit();
// delete
AR.update("delete from t_post where id = ?",id).commit()
OK,这一切看起来多么的简单,查阅使用指南更多现成的例子供你参考:
-
- 添加测试代码
-
- 优化基础代码
-
- 优化并发能力
- Blog:https://biezhi.me
- Mail: biezhi.me#gmail.com
- Java交流群: 1013565
非常感谢下面的开发者朋友对本项目的帮助,如果你也愿意提交PR,欢迎你!
请查看 Apache License