Skip to content

Nineee/blade

Repository files navigation

Based on Java8 + Netty4 to create a lightweight, high-performance, simple and elegant Web framework 😋

Spend 1 hour to learn it to do something interesting, a tool in addition to the other available frameworks.

🐾 Quick Start | 🎬 Video Tutorial | 🌚 Documentation | 📗 Guidebook | 💰 Donate | 🇨🇳 简体中文


What Is Blade?

Blade is a pursuit of simple, efficient Web framework, so that JavaWeb development becomes even more powerful, both in performance and flexibility. If you like to try something interesting, I believe you will love it. If you think it's good, you can support it with a star or by donating 😊

Features

  • A new generation MVC framework that doesn't depend on other libraries
  • Get rid of SSH's bloated, modular design
  • Source is less than 500kb, learning it is also simple
  • RESTful-style routing design
  • Template engine support, view development more flexible
  • High performance, 100 concurrent qps 20w/s
  • Run the JAR package to open the web service
  • Streams-style API
  • CSRF and XSS defense
  • Basic Auth and Authorization
  • Supports plug-in extensions
  • Support webjars resources
  • Tasks based on cron expressions
  • Built-in a variety of commonly used middleware
  • Built-in JSON output
  • JDK8 +

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 a DSL grammar.
» Easy deploy: supports maven package jar file running.

Quick Start

Create a basic Maven or Gradle project.

Do not create a webapp project, Blade does not require much trouble.

Run with Maven:

<dependency> <groupId>com.bladejava</groupId> <artifactId>blade-mvc</artifactId> <version>2.0.15.RELEASE</version> </dependency>

or Gradle:

compile 'com.bladejava:blade-mvc:2.0.15.RELEASE'

Write the main method and the Hello World:

publicstaticvoidmain(String[] args){Blade.of().get("/", ctx -> ctx.text("Hello Blade")).start()}

Open http://localhost:9000 in your browser to see your first Blade application!

Contents

Register Route

HardCode

publicstaticvoidmain(String[] args){// Create Blade,using GET、POST、PUT、DELETEBlade.of() .get("/user/21", getting) .post("/save", posting) .delete("/remove", deleting) .put("/putValue", putting) .start()}

Controller

@PathpublicclassIndexController{@GetRoute("signin") publicStringsignin(){return"signin.html"} @PostRoute("signin") @JSONpublicRestResponsedoSignin(RouteContextctx){// do somethingreturnRestResponse.ok()} }

Get Request Parameters

Form Parameters

Here is an example:

By Context

publicstaticvoidmain(String[] args){Blade.of().get("/user", ctx ->{Integerage = ctx.fromInt("age"); System.out.println("age is:" + age)}).start()}

By Annotation

@PostRoute("/save") publicvoidsavePerson(@ParamStringusername, @ParamIntegerage){System.out.println("username is:" + username + ", age is:" + age) }

Test it with sample data from the terminal

curl -X GET http://127.0.0.1:9000/user?age=25
curl -X POST http://127.0.0.1:9000/save -F username=jack -F age=16

Path Parameters

By RouteContext

publicstaticvoidmain(String[] args){Bladeblade = Blade.of(); // Create a route: /user/:uidblade.get("/user/:uid", ctx ->{Integeruid = ctx.pathInt("uid"); ctx.text("uid : " + uid)}); // Create two parameters routeblade.get("/users/:uid/post/:pid", ctx ->{Integeruid = ctx.pathInt("uid"); Integerpid = ctx.pathInt("pid"); Stringmsg = "uid = " + uid + ", pid = " + pid; ctx.text(msg)}); // Start bladeblade.start()}

By Annotation

@GetRoute("/users/:username/:page") publicvoiduserTopics(@PathParamStringusername, @PathParamIntegerpage){System.out.println("username is:" + usernam + ", page is:" + page) }

Test it with sample data from the terminal

curl -X GET http://127.0.0.1:9000/users/biezhi/2

Body Parameters

publicstaticvoidmain(String[] args){Blade.of().post("/body", ctx ->{System.out.println("body string is:" + ctx.bodyToString()) }).start()}

Test it with sample data from the terminal

curl -X POST http://127.0.0.1:9000/body -d '{"username":"biezhi","age":22}'

Parse To Model

This is the User model.

publicclassUser{privateStringusername; privateIntegerage; // getter and setter }

By Annotation

@PostRoute("/users") publicvoidsaveUser(@ParamUseruser){System.out.println("user => " + user)}

Test it with sample data from the terminal

curl -X POST http://127.0.0.1:9000/users -F username=jack -F age=16

Custom model identification

@PostRoute("/users") publicvoidsaveUser(@Param(name="u") Useruser){System.out.println("user => " + user)}

Test it with sample data from the terminal

curl -X POST http://127.0.0.1:9000/users -F u[username]=jack -F u[age]=16

Body Parameter To Model

publicvoidgetUser(@BodyParamUseruser){System.out.println("user => " + user)}

Test it with sample data from the terminal

curl -X POST http://127.0.0.1:9000/body -d '{"username":"biezhi","age":22}'

Get Environment

Environmentenvironment = WebContext.blade().environment(); Stringversion = environment.get("app.version", "0.0.1");

Get Header

By Context

@GetRoute("header") publicvoidgetHeader(RouteContextctx){System.out.println("Host => " + ctx.header("Host")); // get useragentSystem.out.println("UserAgent => " + ctx.userAgent()); // get client ipSystem.out.println("Client Address => " + ctx.address())}

By Annotation

@GetRoute("header") publicvoidgetHeader(@HeaderParamStringHost){System.out.println("Host => " + Host)}

Get Cookie

By Context

@GetRoute("cookie") publicvoidgetCookie(RouteContextctx){System.out.println("UID => " + ctx.cookie("UID"))}

By Annotation

@GetRoute("cookie") publicvoidgetCookie(@CookieParamStringUID){System.out.println("Cookie UID => " + UID)}

Static Resource

Blade builds a few static resource catalog, as long as you will save the resource file in the static directory under the classpath, and then browse http://127.0.0.1:9000/static/style.css

If you want to customize the static resource URL

Blade.of().addStatics("/mydir");

Of course you can also specify it in the configuration file. application.properties (location in classpath)

mvc.statics=/mydir

Upload File

By Request

@PostRoute("upload") publicvoidupload(Requestrequest){request.fileItem("img").ifPresent(fileItem ->{fileItem.moveTo(newFile(fileItem.getFileName()))})}

By Annotation

@PostRoute("upload") publicvoidupload(@MultipartParamFileItemfileItem){// Save to new pathfileItem.moveTo(newFile(fileItem.getFileName()))}

Set Session

publicvoidlogin(Sessionsession){// if login successsession.attribute("login_key", SOME_MODEL)}

Render To Browser

Render JSON

By Context

@GetRoute("users/json") publicvoidprintJSON(RouteContextctx){Useruser = newUser("biezhi", 18); ctx.json(user)}

By Annotation

This form looks more concise 😶

@GetRoute("users/json") @JSONpublicUserprintJSON(){returnnewUser("biezhi", 18)}

Render Text

@GetRoute("text") publicvoidprintText(RouteContextctx){ctx.text("I Love Blade!")}

Render Html

@GetRoute("html") publicvoidprintHtml(RouteContextctx){ctx.html("<center><h1>I Love Blade!</h1></center>")}

Render Template

By default all template files are in the templates directory; in most of the cases you do not need to change it.

Default Template

By default, Blade uses the built-in template engine, which is very simple. In a real-world web project, you can try several other extensions.

publicstaticvoidmain(String[] args){Blade.of().get("/hello", ctx ->{ctx.attribute("name", "biezhi"); ctx.render("hello.html")}).start(Hello.class, args)}

The hello.html template

<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Hello Page</title></head><body><h1>Hello, ${name}</h1></body></html>

Jetbrick Template

Config Jetbrick Template

Create a BladeLoader class and load some config

@BeanpublicclassTemplateConfigimplementsBladeLoader{@Overridepublicvoidload(Bladeblade){blade.templateEngine(newJetbrickTemplateEngine())} }

Write some data for the template engine to render

publicstaticvoidmain(String[] args){Blade.of().get("/hello", ctx ->{Useruser = newUser("biezhi", 50); ctx.attribute("user", user); ctx.render("hello.html")}).start(Hello.class, args)}

The hello.html template

<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Hello Page</title></head><body><h1>Hello, ${user.username}</h1> #if(user.age > 18) <p>Good Boy!</p> #else <p>Gooood Baby!</p> #end </body></html>

Render API

Redirects

@GetRoute("redirect") publicvoidredirectToGithub(RouteContextctx){ctx.redirect("https://github.com/biezhi")}

Redirect API

Write Cookie

@GetRoute("write-cookie") publicvoidwriteCookie(RouteContextctx){ctx.cookie("hello", "world"); ctx.cookie("UID", "22", 3600)}

Cookie API

Web Hook

WebHook is the interface in the Blade framework that can be intercepted before and after the execution of the route.

publicstaticvoidmain(String[] args){// All requests are exported before execution beforeBlade.of().before("/*", ctx ->{System.out.println("before...")}).start()}

Logging

Blade uses slf4j-api as logging interface, the default implementation of a simple log package (modified from simple-logger); if you need complex logging you can also use a custom library, you only need to exclude the blade-log from the dependencies.

privatestaticfinalLoggerlog = LoggerFactory.getLogger(Hello.class); publicstaticvoidmain(String[] args){log.info("Hello Info,{}", "2017"); log.warn("Hello Warn"); log.debug("Hello Debug"); log.error("Hello Error")}

Basic Auth

Blade includes a few middleware, like Basic Authentication; of course, it can also be customized to achieve more complex goals.

publicstaticvoidmain(String[] args){Blade.of().use(newBasicAuthMiddleware()).start()}

Specify the user name and password in the application.properties configuration file.

http.auth.username=admin http.auth.password=123456

Change Server Port

There are three ways to modify the port: hard coding it, in a configuration file, and through a command line parameter.

Hard Coding

Blade.of().listen(9001).start();

Configuration For application.properties

server.port=9001

Command Line

java -jar blade-app.jar --server.port=9001

Configuration SSL

Configuration For application.properties

server.ssl.enable=true server.ssl.cert-path=cert.pem server.ssl.private-key-path=private_key.pem server.ssl.private-key-pass=123456

Custom Exception Handler

Blade has an exception handler already implemented by default; if you need to deal with custom exceptions, you can do it like follows.

@BeanpublicclassGlobalExceptionHandlerextendsDefaultExceptionHandler{@Overridepublicvoidhandle(Exceptione){if (einstanceofCustomException){CustomExceptioncustomException = (CustomException) e; Stringcode = customException.getCode(); // do something } else{super.handle(e)} } }

Besides looking easy, the features above are only the tip of the iceberg, and there are more surprises to see in the documentation and sample projects:

Change Logs

See Here

Contact

Contributors

Thanks goes to these wonderful people

contributors.svg

Contributions of any kind are welcome!

Licenses

Please see Apache License

About

🚀 Lightning fast and elegant mvc framework for Java8

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java99.8%
  • HTML0.2%