开发者社区> 开发与运维> 正文


简介: 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中安装的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:



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+. (

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/ 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 
             ├──                 // Dependency injection definitions via Guice (Optional)          ├──                 // Contains all routes of your application in one location         ├──          // Integration of arbitrary servlet filters and mappings (Optional)         ├──         // Customization of application startup (Optional)         ├── application.conf            // Configuration for test dev and production mode         ├──         // 18n messages         └──
          ├── controllers                     // Controllers will handle the actual request and do something         ├──
          ├── dao                             // Database access via DAO objects and not in the controller         ├──
          ├── db                              // Database migrations when dealing with RDBMS (Flyway)         └── migration
                 ├── V1__.sql
                 └── V2__.sql
          ├── ehcache.xml                     // Configuration for ehcache      ├── etc
             └──  // Argument extractors for controller methods      ├── filters
             └──           // Filter to filter the request in the controller      ├── logback.xml                     // Logging configuration via logback / slf4j      ├── models                          // Some models that map to your relational database         ├──
          └── 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
       ├── resources
       └── webapp
           └── WEB-INF
               └── web.xml                    // Needed for servlet containers to start up Ninja└── test
        ├── java
           └── controllers                    // Different tests for your application       ├──
        └── resources
            └── test_for_upload.txt


+ 订阅