zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    fastjson1.2.22-1.2.24 反序列化命令执行实践测试
    Spring boot JdbcTemplate sql注入测试
    java反序列化命令执行测试实践
    SpringBoot 整合mybatis SQL注入漏洞实践
    SpringBoot整合mybatis入门
    python函数默认参数为可变对象的理解
    python笔记
    python
    python面向对象
    ICMP
  • 原文地址:https://www.cnblogs.com/huzi007/p/5803020.html
Copyright © 2011-2022 走看看