Skip to main content

Blade - Java Web Framework -Guide

Welcome to the new generation MVC framework(Web)
The Blade is based on Java8 + Netty4 to create a lightweight, high-performance, simple, and elegant Web framework. Blade Framework is based on the Apache2 License.
Features
  • A new generation MVC framework that doesn't depend on other libraries
  • Get rid of SSH's bloated, modular design
  • The 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 web jars resources
  • Tasks based on cron expressions
  • Built-in a variety of commonly used middleware
  • Built-in JSON output
  • JDK8 +
Advantages
  • 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.
Getting Started
Create a basic Maven or Gradle project.
Maven
<dependency>

    <groupId>com.bladejava</groupId>

    <artifactId>blade-mvc</artifactId>

    <version>2.0.14.RELEASE</version>

</dependency>
Gradle
compile 'com.bladejava:blade-mvc:2.0.14.RELEASE'
Running a Blade Application
Blade is based upon Netty, an amazing asynchronous event-driven network application framework. Therefore, to run our Blade-based application we don't need any external Application Server nor Servlet Container; the JRE will be enough
Write the main method and the Hello World:
public static void main(String[] args) {
    Blade.of().get("/", ctx -> ctx.text("Hello World")).start();
}
After that, the application will be accessible at the http://localhost:9000 URL.
Register Route By annotation
First of all, we need to create a Controller through the @Path annotation, which will be scanned by Blade during the startup.

We then need to use the route annotation related to the HTTP method we want to intercept:
@Path
public class IndexController {
    @GetRoute("signin")
    public String signin(){
        return "signin.html";
    }
    @PostRoute("signin")
    @JSON
    public RestResponse doSignin(RouteContext ctx){
        // do something
        return RestResponse.ok();
    }
}
Get Form Parameters By annotation
@PostRoute("/save")
public void savePerson(@Param String username, @Param Integer age){
    System.out.println("username is:" + username + ", age is:" + age)
}
Get Path Parameters By annotation
@PostRoute("/save")
public void savePerson(@Param String username, @Param Integer age){
    System.out.println("username is:" + username + ", age is:" + age)
}
Get Header By Annotation
@GetRoute("header")
public void getHeader(@HeaderParam String Host){
    System.out.println("Host => " + Host);
}
Get Cookie By Annotation
@GetRoute("cookie")
public void getCookie(@CookieParam String UID){
    System.out.println("Cookie UID => " + UID);
}
Upload File By Annotation
@PostRoute("upload")
public void upload(@MultipartParam FileItem fileItem){
    // Save to new path
    fileItem.moveTo(new File(fileItem.getFileName()));
}
Render JSON By annotation
@GetRoute("users/json")
@JSON
public User printJSON(){
    return new User("biezhi", 18);
}
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.
@Bean
public class GlobalExceptionHandler extends DefaultExceptionHandler {
    @Override
    public void handle(Exception e) {
        if (e instanceof CustomException) {
            CustomException customException = (CustomException) e;
            String code = customException.getCode();
            // do something
        } else {
            super.handle(e);
        }
    }}
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

For more details Visit Official Documentation

Comments

Popular posts from this blog

Spring boot video streaming example-HTML5

Hello everyone, Today we will learn how to stream MP4 video using Spring. You can download the source code of this sample application from our GitHub repository. Download Link is provided at the end of this post.
User Interface

Project Structure


Home Controller
@Controller
public class HomeController {
 @Autowired
 private MyResourceHttpRequestHandler handler;
 private final static File MP4_FILE = new File("D:\\videofiles\\video1.mp4");
 // supports byte-range requests
 @GetMapping("/index")
 public String home() {
  return "index";
 }
 // supports byte-range requests
 @GetMapping("/byterange")
 public void byterange( HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  request.setAttribute(MyResourceHttpRequestHandler.ATTR_FILE, MP4_FILE);
  handler.handleRequest(request, response);
 }}
Spring Boot 
@SpringBootApplication
@ComponentScan({ "com" })
public class KnowledgefactorydemoApplication {
 public static vo…

DataTable-Pagination example with Spring boot,jQuery and ajax free download

This article shows you DataTable-Pagination example with Spring boot, jQuery and ajax. 

Following technologies stack being used:
Spring Boot 2.1.1.RELEASESpring 5.1.3.RELEASE Maven 3DatatablejQueryFree Markerjavascriptajax Project Structure
View(index.ftl) <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Knf|Pagination-Datatables</title> <!-- Tell the browser to be responsive to screen width --> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="../../css/jquery.dataTables.min.css"> <body>  <section class="content">   <div class="container-fluid">    <table id="example" class="display" style="width: 100%">     <thead>      <tr>       <th>First name</th>       <th>La…

9 Best Job Posting Sites for Employee 2020-2022

One of the most common ways today’s job seekers uncover employment opportunities are by using online sources. There are hundreds of job boards, both generic and niche, as well as aggregators, social media channels, networking groups, and staffing company websites to choose from. The good news is they provide plenty of listings. The bad news is you can be easily overwhelmed by so many options. Here, knowledge factory offers 9 of what is generally considered the best job search websites around. There are many others, of course, but this list of best job search websites should give you a good starting point for your next job hunt: 1. LinkedIn
This top networking site allows you to find jobs through your extended network. User profiles serve as resumes. You can easily share career-related content and solicit or supply recommendations. Additionally, you can join groups, participate in conversations, and follow companies you find interesting and relevant to your job search. Premium pai…