java_Ninja实战过程
使用Ninja马上两年了,之前多多少少的都是跟着项目模仿着写,今年上半年准备从一个小项目开始从始至终走一遍;
首先官网:http://www.ninjaframework.org;
github: https://github.com/ninjaframework/ninja
微信公众号:
---------------------------------------------------------------------------------------------------
1:准备:jdk1.8(环境变量配置)等;IDEA(开发工具);GIT(版本控制器);maven(项目构建工具);PostgreSQL(数据库);
安装过程略(如果感兴趣可以加Ninja的QQ新群:262296156或者2群:560927314)
访问ninja官网实在太慢了,于是我把整个网站抓下来了,已经分享到群里了,需要的可以直接下载
--------------------------------------------------------------------------------------------------
2:直接创建一个Ninja项目官网位置:www.ninjaframework.org/documentation/getting_started/create_your_first_application.html
------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------
直接进行maven构建;
1 | mvn archetype:generate -DarchetypeGroupId=org.ninjaframework -DarchetypeArtifactId=ninja-servlet-archetype-simple |
解释一下这句话:我们使用maven构建项目,那就用它构建一个ninja原型--maven原型构建;
在你的某个目录下使用git窗口执行上面的代码,项目自动构建完毕(交互部分窝用中文解释一下);
1 | $ mvn archetype:generate -DarchetypeGroupId=org.ninjaframework -DarchetypeArtifactId=ninja-servlet-archetype-simple[INFO] Scanning for projects...[INFO][INFO] ------------------------------------------------------------------------[INFO] Building Maven Stub Project (No POM) 1[INFO] ------------------------------------------------------------------------[INFO][INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources@ standalone-pom >>>[INFO][INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources@ standalone-pom <<<[INFO][INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom ---[INFO] Generating project in Interactive mode[INFO] Archetype [org.ninjaframework:ninja-servlet-archetype-simple:6.0.0-rc1] found in catalog remoteDownloading: https: //repo .maven.apache.org /maven2/org/ninjaframework/ninja-servlet-archetype-simple/6 .0.0-rc1 /ninja-servlet-archetype-simple-6 .0.0-rc1.jarDownloaded: https: //repo .maven.apache.org /maven2/org/ninjaframework/ninja-servlet-archetype-simple/6 .0.0-rc1 /ninja-servlet-archetype-simple-6 .0.0-rc1.jar (13 KB at 2.8 KB /sec )Downloading: https: //repo .maven.apache.org /maven2/org/ninjaframework/ninja-servlet-archetype-simple/6 .0.0-rc1 /ninja-servlet-archetype-simple-6 .0.0-rc1.pomDownloaded: https: //repo .maven.apache.org /maven2/org/ninjaframework/ninja-servlet-archetype-simple/6 .0.0-rc1 /ninja-servlet-archetype-simple-6 .0.0-rc1.pom (3 KBat 5.1 KB /sec )Define value for property 'groupId' : :Define value for property 'artifactId' : : Define value for property 'version' :1.0-SNAPSHOT: : Define value for property 'package' : : : [WARNING] Archetype is not fully configuredDefine value for property 'groupId' : : Define value for property 'artifactId' : : [INFO] Using property: version = 1.0-SNAPSHOTDefine value for property 'package' : : : cmy // 这里是packeage的名字,你可以自己定义[WARNING] Archetype is not fully configuredDefine value for property 'groupId' : : cmy <!-- 构件的标识符,它和group ID一起唯一标识一个构件。换句话说,你不能有两个不同的项目拥有同样的artifact ID和groupID;在某个 特定的group ID下,artifact ID也必须是唯一的。构件是项目产生的或使用的一个东西,Maven为项目产生的构件包括:JARs,源 码,二进制发布和WARs等。--> Define value for property 'artifactId' : : cmy [INFO] Using property: version = 1.0-SNAPSHOT // 这里是 打包后的版本[INFO] Using property: package = cmy // 这里是packeage的名字,你可以自己定义Confirm properties configuration:groupId: cmyartifactId: cmyversion: 1.0-SNAPSHOTpackage: cmy Y: : y[INFO] ----------------------------------------------------------------------------[INFO] Using following parameters for creating project from Archetype: ninja-servlet-archetype-simple:6.0.0-rc1[INFO] ----------------------------------------------------------------------------[INFO] Parameter: groupId, Value: cmy[INFO] Parameter: artifactId, Value: cmy[INFO] Parameter: version, Value: 1.0-SNAPSHOT[INFO] Parameter: package, Value: cmy[INFO] Parameter: packageInPathFormat, Value: cmy[INFO] Parameter: package, Value: cmy[INFO] Parameter: version, Value: 1.0-SNAPSHOT[INFO] Parameter: groupId, Value: cmy[INFO] Parameter: artifactId, Value: cmy[INFO] project created from Archetype in dir : e:\_My_File_____\_work\MyCode\myCode\javaworkspace\A_myproject\cmy[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time : 09:57 min[INFO] Finished at: 2017-03-13T15:44:03+08:00[INFO] Final Memory: 16M /273M [INFO] ------------------------------------------------------------------------ |
进入项目,安装一下maven依赖包:
1 2 3 | cd cmy mvn clean install // to generate the compiled classes the first time mvn ninja:run // to start Ninja's SuperDevMode 运行项目 |
构建完毕:
我接手新项目习惯性研究一下包目录结构,那Ninja也有介绍,官方地址http://www.ninjaframework.org/documentation/getting_started/the_anatomy_of_a_ninja_application.html
如果有必要等抽空解释一下目录结构,暂时先放在这里如下:

1 ├── pom.xml // Instructions about dependencies and the build (Maven) 2 └── src 3 ├── main 4 │ ├── java 5 │ │ ├── META-INF 6 │ │ │ └── persistence.xml // Contains informations how to access databases via JPA 7 │ │ ├── assets // Static assets of your application 8 │ │ │ └── css 9 │ │ │ └── custom.css 10 │ │ ├── conf 11 │ │ │ ├── Module.java // Dependency injection definitions via Guice (Optional) 12 │ │ │ ├── Routes.java // Contains all routes of your application in one location 13 │ │ │ ├── ServletModule.java // Integration of arbitrary servlet filters and mappings (Optional) 14 │ │ │ ├── StartupActions.java // Customization of application startup (Optional) 15 │ │ │ ├── application.conf // Configuration for test dev and production mode 16 │ │ │ ├── messages.properties // 18n messages 17 │ │ │ └── messages_de.properties 18 │ │ ├── controllers // Controllers will handle the actual request and do something 19 │ │ │ ├── ApiController.java 20 │ │ │ ├── ApplicationController.java 21 │ │ │ ├── ArticleController.java 22 │ │ │ └── LoginLogoutController.java 23 │ │ ├── dao // Database access via DAO objects and not in the controller 24 │ │ │ ├── ArticleDao.java 25 │ │ │ ├── SetupDao.java 26 │ │ │ └── UserDao.java 27 │ │ ├── db // Database migrations when dealing with RDBMS (Flyway) 28 │ │ │ └── migration 29 │ │ │ ├── V1__.sql 30 │ │ │ └── V2__.sql 31 │ │ ├── ehcache.xml // Configuration for ehcache 32 │ │ ├── etc 33 │ │ │ ├── LoggedInUser.java 34 │ │ │ └── LoggedInUserExtractor.java // Argument extractors for controller methods 35 │ │ ├── filters 36 │ │ │ └── LoggerFilter.java // Filter to filter the request in the controller 37 │ │ ├── logback.xml // Logging configuration via logback / slf4j 38 │ │ ├── models // Some models that map to your relational database 39 │ │ │ ├── Article.java 40 │ │ │ ├── ArticleDto.java 41 │ │ │ ├── ArticlesDto.java 42 │ │ │ └── User.java 43 │ │ └── views // html views - always map to a controller and a method 44 │ │ ├── ApplicationController 45 │ │ │ ├── index.ftl.html // Maps to controller "ApplicationController" and method "index" 46 │ │ │ └── setup.ftl.html 47 │ │ ├── ArticleController 48 │ │ │ ├── articleNew.ftl.html 49 │ │ │ └── articleShow.ftl.html 50 │ │ ├── LoginLogoutController 51 │ │ │ ├── login.ftl.html 52 │ │ │ └── logout.ftl.html 53 │ │ ├── layout 54 │ │ │ ├── defaultLayout.ftl.html 55 │ │ │ ├── footer.ftl.html 56 │ │ │ └── header.ftl.html 57 │ │ └── system // Error html views. Can be customized to output custom error pages 58 │ │ ├── 403forbidden.ftl.html 59 │ │ └── 404notFound.ftl.html 60 │ ├── resources 61 │ └── webapp 62 │ └── WEB-INF 63 │ └── web.xml // Needed for servlet containers to start up Ninja 64 └── test 65 ├── java 66 │ └── controllers // Different tests for your application 67 │ ├── ApiControllerDocTest.java 68 │ ├── ApiControllerDocTesterTest.java 69 │ ├── ApiControllerMockTest.java 70 │ ├── ApiControllerTest.java 71 │ ├── ApplicationControllerFluentLeniumTest.java 72 │ ├── ApplicationControllerTest.java 73 │ ├── LoginLogoutControllerTest.java 74 │ └── RoutesTest.java 75 └── resources 76 └── test_for_upload.txt
好了,接下来就开始配置项目了:
我要开发的是一个简单的博客系统,运行在互联网上的项目,因此一个配置或者布局构架等等和业务系统有所区别;
项目分析:
前台:需要文章列表就可以,我希望最好有个主页进入
后台:管理页面(我初步设置两三种权限,系统管理员,博主后台,运营后台)
--------------------------------------------------------------------------
开始配置application.conf 文件:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了