Java輕量級MVC框架:Blade
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.5.0</version> </dependency> <dependency> <groupId>com.bladejava</groupId> <artifactId>blade-startup</artifactId> <version>1.0.1</version> </dependency>
編寫Main函數:
public static void main(String[] args) { Blade 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 = 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 = 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參數獲取
public static void main(String[] args) { Blade 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){ JsonObject jsonObject = new JsonObject(); 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 = Blade.me(); blade.before("/.*", (request, response) -> { System.out.println("before..."); }); blade.listen(9001).start(); }
DSL數據庫操作
// 保存操作 public boolean save(Integer cid, Integer tid, Integer fuid, Integer tuid) { return model.insert().param("cid", cid) .param("tid", tid) .param("fuid", fuid) .param("tuid", tuid) .param("addtime", new Date()) .param("ntype", 0).executeAndCommit() > 0; } // 登錄操作 public User signin(String username, String password) { String pwd = EncrypKit.md5(username + password); return model.select().eq("username", username) .eq("password", pwd).fetchOne(); } // 查詢條數 public Long getUserCount(String email){ return model.count().eq("email", email).fetchCount(); }
OK,這一切看起來多么的簡單,查閱使用指南更多現成的例子供你參考:
計劃
- 1. 編寫英文文檔
- 2. 添加測試代碼
- 3. 優化基礎代碼
- 4. 優化并發能力
項目主頁:http://www.baiduhome.net/lib/view/home/1450792639933
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!