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!
Ninja’s SuperDevMode relies on an external compiler to compile your sources. In essence the flow will be:
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).
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