Ninja初步-阿里云开发者社区

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

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


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章