Ninja初步

简介: use Ninja frame create your first application. Ninja 是依赖于maven的项目,开始要先安装配置maven,下载mavend的bin,在apache官方网站可以下载. 下载下来之后,解压,找个路径放进去, 把bin的位置设在环境变量里,新建环境变量MAVEN_HOME:在PATH里加入maven的bin的路径 由于Maven依赖Java运行环境,因此使用Maven之前需要配置Java的运行环境。

use Ninja frame create your first application.

Ninja 是依赖于maven的项目,开始要先安装配置maven,下载mavend的bin,在apache官方网站可以下载.


下载下来之后,解压,找个路径放进去, 把bin的位置设在环境变量里,新建环境变量MAVEN_HOME:在PATH里加入maven的bin的路径



由于Maven依赖Java运行环境,因此使用Maven之前需要配置Java的运行环境。下载并安装JDK,配置JDK的环境变量JAVA_HOME,否则maven将无法使用,配置完毕后,在Windows命令提示符下,输入mvn -v测试一下,配置成功显示如图:


Eclipse中配置Maven,点击eclipse菜单栏Help->Eclipse Marketplace搜索关键字maven到插件Maven Integration for Eclipse 并点击安装即可,如下图:



安装完毕后,点击重启eclipse

重启后,为了使得Eclipse中安装的Maven插件,同windows中安装的那个相同,需要让eclipse中的maven重新定位一下,点击Window -> Preference -> Maven -> Installation -> Add进行设置。



至此环境已经搭建完毕!已经有搭建过的同学请跳过。

下来开始建立项目工程,

mvn archetype:generate -DarchetypeGroupId=org.ninjaframework -DarchetypeArtifactId=ninja-servlet-archetype-simple

Please enter sensible values for “groupId” and “artifactId” and let Maven generate your first Ninja project.

After finishing the generation change into your project directory and execute:

groupId:包名

artifactId:项目名

cd MY_INSTALLED_PROJECT
mvn clean install     // to generate the compiled classes the first time

// to generate the compiled classes the first time
 


mvn ninja:run         // to start Ninja's SuperDevMode

This starts Ninja’s SuperDevMode. Simply open  http://localhost:8080  in your browser. You’ll see Ninja demo project ready to work on. That’s it basically. You just created your first Ninja application!

The Ninja application you created via the archetype is just a simple plain old Maven project. This means you can import the project into any modern IDE. Eclipse, NetBeans, IntelliJ and many more.

Ninja’s SuperDevMode relies on an external compiler to compile your sources. In essence the flow will be:

  • You run Ninja’s SuperDevMode via “mvn ninja:run”
  • You change a Java file in your IDE
  • Your IDE picks up the changes and recompiles the results to a .class file
  • Ninja’s SuperDevMode detects the recompiled file and immediately shows the latest version of your application at http://localhost:8080.

Nice. But the catch is that your IDE has to recompile the files. If your IDE does not recompile the file Ninja’s SuperDevMode will not work.

Fortunately IDE based compilation works out of the box for Eclipse (“Automatically build project”), NetBeans (“Compile on Save”) and IntelliJ 12+. (http://stackoverflow.com/questions/12744303/intellij-idea-java-classes-not-auto-compiling-on-save).

Sometimes Ninja's SuperDevMode does no longer pick up changes you made. In many cases automatic compilation by the IDE is broken. If you run into that problem it helps to "clean and build" the project from within your IDE.

mvn ninja:run 用的是jetty.


 

  Ninja does a lot of things via convention over configuration. If things are in the right place it will automatically work.

For instance the routes for your application should always at conf/Routes.java. And HTML views always map to a certain controller method via their directory structure.

The conventions are explained in detail in the later sections of the documentation.


官网给出的项目例子:


The following tree shows an example project with some explanations what is going on. The project is a comprehensive project with database access via JPA, database migrations and advanced features like filters and argument extractors.

├── pom.xml                                     // Instructions about dependencies and the build (Maven)└── src
    ├── main
       ├── java
          ├── META-INF
             └── persistence.xml             // Contains informations how to access databases via JPA      ├── assets                          // Static assets of your application         └── css
                 └── custom.css
          ├── conf 
             ├──Module.java                 // Dependency injection definitions via Guice (Optional)          ├──Routes.java                 // Contains all routes of your application in one location         ├──ServletModule.java          // Integration of arbitrary servlet filters and mappings (Optional)         ├──StartupActions.java         // Customization of application startup (Optional)         ├── application.conf            // Configuration for test dev and production mode         ├── messages.properties         // 18n messages         └── messages_de.properties
          ├── controllers                     // Controllers will handle the actual request and do something         ├──ApiController.java
             ├──ApplicationController.java
             ├──ArticleController.java
             └──LoginLogoutController.java
          ├── dao                             // Database access via DAO objects and not in the controller         ├──ArticleDao.java
             ├──SetupDao.java
             └──UserDao.java
          ├── db                              // Database migrations when dealing with RDBMS (Flyway)         └── migration
                 ├── V1__.sql
                 └── V2__.sql
          ├── ehcache.xml                     // Configuration for ehcache      ├── etc
             ├──LoggedInUser.java
             └──LoggedInUserExtractor.java  // Argument extractors for controller methods      ├── filters
             └──LoggerFilter.java           // Filter to filter the request in the controller      ├── logback.xml                     // Logging configuration via logback / slf4j      ├── models                          // Some models that map to your relational database         ├──Article.java
             ├──ArticleDto.java
             ├──ArticlesDto.java
             └──User.java
          └── views                           // html views - always map to a controller and a method          ├──ApplicationController             ├── index.ftl.html          // Maps to controller "ApplicationController" and method "index"             └── setup.ftl.html
              ├──ArticleController             ├── articleNew.ftl.html
                 └── articleShow.ftl.html
              ├──LoginLogoutController             ├── login.ftl.html
                 └── logout.ftl.html
              ├── layout
                 ├── defaultLayout.ftl.html
                 ├── footer.ftl.html
                 └── header.ftl.html
              └── system                      // Error html views. Can be customized to output custom error pages              ├──403forbidden.ftl.html
                  └──404notFound.ftl.html
       ├── resources
       └── webapp
           └── WEB-INF
               └── web.xml                    // Needed for servlet containers to start up Ninja└── test
        ├── java
           └── controllers                    // Different tests for your application       ├──ApiControllerDocTest.java
               ├──ApiControllerDocTesterTest.java
               ├──ApiControllerMockTest.java
               ├──ApiControllerTest.java
               ├──ApplicationControllerFluentLeniumTest.java
               ├──ApplicationControllerTest.java
               ├──LoginLogoutControllerTest.java
               └──RoutesTest.java
        └── resources
            └── test_for_upload.txt


目录
相关文章
|
程序员 API 数据库
【Cmake工程 库相关教程 】深入理解CMake工程C/C++ 库管理技巧
【Cmake工程 库相关教程 】深入理解CMake工程C/C++ 库管理技巧
276 1
|
C++
一文弄懂C++的内部类
内部类概念 如果一个类定义在另一个类的内部,这个内部的类,叫做内部类。 内部类是一个独立的类,它不属于外部类,更不能通过外部类的对象去访问内部类的成员。外部类对内部类没有任何优越的访问权限。
396 0
|
安全 虚拟化 Docker
解决:VMware Workstation 与 Device/Credential Guard 不兼容
因为在官网下载了win版的docker,而会自带下载虚拟机Hyper-V,这个和我之前下载的vmware虚拟机造成冲突了,导致后者不能使用,所以打开vmware报错如下:
6075 0
解决:VMware Workstation 与 Device/Credential Guard 不兼容
|
6月前
|
监控 安全 测试技术
选择Postman免费版还是付费版,进行 API 测试呢?
深入了解 Postman 免费版和付费版的细节,看看哪一个更适合您的 API 需求。
wslconfig 配置
wslconfig 配置
225 0
|
12月前
|
数据可视化 项目管理
什么是关键工作?如何识别和管理项目中的关键工作?
项目管理中的关键工作是指那些一旦延迟便会影响整个项目进度的任务。本文从实战角度探讨了关键工作的定义、识别方法及高效管理策略,强调了资源优先配置、预警机制、应急方案及频繁沟通的重要性,并介绍了几款有助于关键任务管理的项目管理工具。
521 1
|
安全 算法 网络安全
什么是 SSL 加密?
【8月更文挑战第31天】
1847 0
|
监控 Java jenkins
Java中的持续集成与持续部署(CI/CD)
Java中的持续集成与持续部署(CI/CD)
|
存储 边缘计算 安全
阿里云全球基础设施展示,公共云地域、边缘节点、超级数据中心分布图
本文为大家介绍了阿里云在2024年的全球基础设施布局,包括公共云地域、边缘节点、超级数据中心等各个阶段和方面。阿里云基础设施已覆盖全球四大洲,拥有30个公共云地域和89个可用区,以及超过3200个边缘节点,为其用户提供了广泛且深入的服务覆盖。
阿里云全球基础设施展示,公共云地域、边缘节点、超级数据中心分布图