Skip to content

Latest commit

 

History

History
220 lines (164 loc) · 5.79 KB

README_CN.md

File metadata and controls

220 lines (164 loc) · 5.79 KB

Build Status maven-central License @biezhi on weibo

English

Blade是什么?

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应用了!

API示例

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();
}

REST URL参数获取

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();
}

DSL数据库操作

// 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,这一切看起来多么的简单,查阅使用指南更多现成的例子供你参考:

计划

    1. 添加测试代码
    1. 优化基础代码
    1. 优化并发能力

更新日志

更新日志

联系我

贡献

非常感谢下面的开发者朋友对本项目的帮助,如果你也愿意提交PR,欢迎你!

开源协议

请查看 Apache License