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


目录
相关文章
|
6月前
Clion-2023.1.4配置CMake-3.25.3
Clion-2023.1.4配置CMake-3.25.3
|
6月前
|
Unix
深入理解 CMake 的 `cmake --build` 命令
深入理解 CMake 的 `cmake --build` 命令
742 1
|
7月前
|
IDE Unix 测试技术
CMake基础(10)使用ninja构建
CMake基础(10)使用ninja构建
1125 1
|
7月前
|
Rust 开发工具 开发者
如何通过cargo install安装我们的crate?
我在安装tauri的开发工具时,产生一个疑问,为什么可以通过cargo安装全局命令,顺着这个线索我找到了如下方案。原理是在项目开发者指定可执行文件,也就是`[[bin]]`,然后发布到crates.io,之后就可以通过install安装到可执行目录下,我们就能正常使用了。并不是所有crate都可以执行,而是只有在源码中声明bin的才能使用。
172 2
|
编译器
[√]cmake 编译选项
[√]cmake 编译选项
76 0
|
编译器 Android开发 iOS开发
[√]关于cmake的kits
[√]关于cmake的kits
140 0
|
Linux 异构计算
|
Python
【解决方案】解决CMake must be installed to build the following extensions: pyltp
【解决方案】解决CMake must be installed to build the following extensions: pyltp
229 0
CMake教程4:最简单的CMake Library
CMake教程4:最简单的CMake Library
99 0
|
iOS开发
XCode编译:pngcrush caught libpng error解决方法
XCode编译:pngcrush caught libpng error解决方法
135 0
XCode编译:pngcrush caught libpng error解决方法