guice 整合ninja framework(七)
ninja是一个优秀的,轻量级的mvc框架,它与google guice整合比较好。下面看一下例子:
我们在web.xml 配置一下:
<listener>
<listener-class>ninja.servlet.NinjaServletListener</listener-class>
</listener> <filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
然后看一下ninja提供的目录结构:

其中conf是它的配置文件夹。
application.conf 是一个配置常量的文件,类似与我们用的属性文件。它用于配置数据库连接,支付回调等常量连接。它支持不同环境下的常量读取,比如你可以设置测试环境的常量,开发环境的常量 messages.properties 用于做国际化的 Module.java 这个文件是用与注册module的,就是google guice用的
Routes.java 这个文件是配置路由的,也就是我们访问路径的。
具体如下:
package conf; import ninja.AssetsController;
import ninja.Router;
import ninja.application.ApplicationRoutes;
import controllers.ApplicationController; public class Routes implements ApplicationRoutes { @Override
public void init(Router router) { router.GET().route("/").with(ApplicationController.class, "index"); //支持不同的请求方式,链式编程,后面helloWorldJson指向ApplicationController的具体实现的一个方法
router.GET().route("/hello_world.json").with(ApplicationController.class, "helloWorldJson");
router.POST().route("/hello_world.json").with(ApplicationController.class, "helloWorldJson"); ///////////////////////////////////////////////////////////////////////
// Assets (pictures / javascript)
///////////////////////////////////////////////////////////////////////
router.GET().route("/assets/webjars/{fileName: .*}").with(AssetsController.class, "serveWebJars");
router.GET().route("/assets/{fileName: .*}").with(AssetsController.class, "serveStatic"); ///////////////////////////////////////////////////////////////////////
// Index / Catchall shows index page
///////////////////////////////////////////////////////////////////////
router.GET().route("/.*").with(ApplicationController.class, "index");
}
controllers这个文件夹用于写控制层代码了。
如:
package controllers; import ninja.Result;
import ninja.Results; import com.google.inject.Singleton;
import ninja.params.Param; @Singleton
public class ApplicationController { public Result index() { return Results.html(); }
public Result helloWorldJson(@Param("aa")String aa) {//接收aa参数 SimplePojo simplePojo = new SimplePojo();
simplePojo.content = "Hello World! Hello Json!"; return Results.json().render(simplePojo); } public static class SimplePojo { public String content; }
}
views文件夹就是存各种html页面了。
基本一个结构就是如此。实际开发中,我们需要建更多的文件,如dao,service等等
ninja很轻量,不如struts2臃肿,是比较灵活的微框架。如果你想你的程序运行更快,用它吧...
例子下载:http://pan.baidu.com/s/1czb7rC
ninja官网:http://www.ninjaframework.org/
guice 整合ninja framework(七)的更多相关文章
- guice整合struts2,guice的使用(八)
平时我们习惯用了spring整合struts2,今天我们就来见识一下guice整合struts2吧. 看web.xml配置: <?xml version="1.0" enco ...
- Maven 整合 robot framework 进行测试
1. 在maven pom.xml中先配置robot framework的plugin: <plugin> <!-- integration test runner (robot-f ...
- guice整合struts2与jpa,guice的使用(九)
传统我们开发一般使用ssh,但是有些微服务应用的项目我们不需要这么臃肿的框架做开发,于是采用了guice+struts2+guice作为框架组合进行了开发. 先看我们项目引用的jar包: 使用的时候一 ...
- guice基本使用,guice整合guice-servlet,web scope注解(六)
guice servlet提供了几个比较有用的web scope,类似与传统servlet 的session,request这些提供的范围等. guice servlet 提供的web scope 如 ...
- guice基本使用,guice整合guice-servlet,web开发(五)
介绍 Guice Servlet 为使用web应用程序和Servlet容器提供了一个完整的模式.. Guice's servlet 扩展允许从你的servlet应用中完全淘汰web.xml,并且具有类 ...
- 【转】iOS静态库 【.a 和framework】【超详细】
原文网址:https://my.oschina.net/kaqijiang/blog/649632 一.什么是库? 库是共享程序代码的方式. 库从本质上来说是一种可执行代码的二进制格式,可以被载入内存 ...
- IOS静态库和Framework区别
一.什么是库? 库是共享程序代码的方式,一般分为静态库和动态库. 二.静态库与动态库的区别? 静态库:链接时完整地拷贝至可执行文件中,被多次使用就有多份冗余拷贝. 动态库:链接时不复制,程序运行时由系 ...
- 【转】iOS库 .a与.framework区别
转自:http://blog.csdn.net/lvxiangan/article/details/43115131 一.什么是库? 库是共享程序代码的方式,一般分为静态库和动态库. 二.静态库与动态 ...
- iOS库--.a与.framework
一.什么是库? 库是共享程序代码的方式,一般分为静态库和动态库. 二.静态库与动态库的差别? 静态库:链接时完整地拷贝至可运行文件里.被多次使用就有多份冗余拷贝. 动态库:链接时不复制.程序执行时由系 ...
随机推荐
- eas之去掉关闭eas页面时校验是否修改的提示
EditUI-------> public boolean checkBeforeWindowClosing() { boolean b = super.checkBefo ...
- 【双系统】windows 和 Ubuntu 双系统安装
本博客主要讲述如何在已安装windows系统的计算机上安装Ubuntu双系统,涉及系统安装和相应磁盘空间分配等问题. 所需环境: 电脑已安装windows系统 下载Ubuntu16.04系统镜像 ...
- PKU 1019 Number Sequence(模拟,思维)
题目 以下思路参考自discuss:http://poj.org/showmessage?message_id=176353 /*我的思路: 1.将长串数分成一个个部分,每个部分是从1到x的无重复的数 ...
- Python-Pandas数据处理
- 自学python 第三天
#!/usr/bin/env python# -*- coding:utf-8 -*- # name = "***"# if "*" in name:# pri ...
- BZOJ 4244 邮戳拉力赛 (DP)
手动博客搬家: 本文发表于20181211 18:01:21, 原地址https://blog.csdn.net/suncongbo/article/details/84957907 为了防止我的博客 ...
- 【hihocoder 1499】A Box of Coins
[题目链接]:http://hihocoder.com/problemset/problem/1499 [题意] [题解] 贪心,模拟; 从左往右对于每一列; 如果上下两个格子; ① 有一个格子超过了 ...
- Spring Cloud-hystrix使用例子(七)
继承方式 HystrixCommand public class UserSelectAllCommand extends HystrixCommand<List<User>> ...
- orcale 多表连接
多表连接:
- 使用MySQL自身复制来恢复binlog
如果需要恢复的二进制日志较多,较复杂,强烈建议使用MySQL自身复制来恢复binlog,而不要使用mysqlbinlog. 目录 [hide] 1. 如何操作 1.1 将binlog作为relay l ...