Skip to content

ziyecheng/blade

 
 

Repository files navigation

Build Status maven-central License Gitter

中文说明

What Is Blade?

Blade is a lightweight MVC framework. It is based on the principles of simplicity and elegance. If you like it, please star and fork it. Thank you!

Features

  • Lightweight: the code is simple and the structure is clear
  • Modular (you can choose which components to use)
  • Supports plug-in extension mechanism
  • RESTful style routing interface
  • Supports multiple configuration files (currently properties, json and coding)
  • Embedded jetty server and template engine support
  • Supports JDK 1.6 and up

Overview

  • Simplicity: The design is simple, easy to understand and doesn't introduce many layers between you and the standard library. The goal of this project is that the users should be able to understand the whole framework in a single day.
  • Elegance: blade supports the RESTful style routing interface, has no invasive interceptors and provides the writing of DSL grammar.

Get Start

To get started, first include the Blade library :

Grab via 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>

or Gradle:

compile 'com.bladejava:blade-core:1.6.2'
compile 'com.bladejava:blade-startup:1.0.1'

Create Main method like this:

public class App {
	
	public static void main(String[] args) {
		Blade blade = me();
		blade.get("/", (request, response) -> {
			response.html("<h1>Hello blade!</h1>");
		});
		blade.listen(9001).start();
	}
}

Run it and point your browser to http://localhost:9001. There you go, you've just created your first Blade app!

API Example

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 Parameters

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

Form URL Parameters

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

Upload File

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 Config File

route.conf

GET		/					IndexRoute.home
GET		/signin				IndexRoute.show_signin
POST	/signin				IndexRoute.signin
GET		/signout			IndexRoute.signout
POST	/upload_img			UploadRoute.upload_img

Route Intercept

public static void main(String[] args) {
	Blade blade = me();
	blade.before("/.*", (request, response) -> {
		System.out.println("before...");
	});
	blade.listen(9001).start();
}

DSL DB Operation

// 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()

You may refer to these examples for additional guidance:

Plan

    1. Add the test code
    1. Optimize the code base
    1. Optimization of concurrent ability

Update

update log

Contact

Contributor

Thank you very much for the developers to help in the project, if you are willing to contribute, welcome!

Licenses

Please see Apache License

About

🚀 A Simple, Elegant Java Web Framework! website →

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%