Teach you how to develop dynamic web projects (2)

This chapter focuses on the structure of the project.

1. The project is managed by gradle. If you are familiar with it, you can skip this section. Here is a brief introduction. Gradle is a project management build tool similar to maven. The configuration file is build.gradle under the project root directory. You can configure the third-party dependencies of the project here.

dependencies {
    def springFrameworkVersion = "4.2.5.RELEASE"
compile "org.springframework:spring-context:${springFrameworkVersion}"
compile "org.springframework:spring-core:${springFrameworkVersion}"
compile "org.springframework:spring-aop:${springFrameworkVersion}"
compile "org.springframework:spring-beans:${springFrameworkVersion}"
compile "org.springframework:spring-expression:${springFrameworkVersion}"
compile "org.springframework:spring-web:${springFrameworkVersion}"
compile "org.springframework:spring-webmvc:${springFrameworkVersion}"
compile "org.springframework:spring-aspects:${springFrameworkVersion}"
compile "org.springframework:spring-orm:${springFrameworkVersion}"
def springSecurityVersion = "4.0.4.RELEASE"
compile "org.springframework.security:spring-security-web:${springSecurityVersion}"
compile "org.springframework.security:spring-security-config:${springSecurityVersion}"
compile "org.codehaus.groovy:groovy-all:2.3.11"
compile 

"org.hibernate:hibernate-core:5.1.0.Final"
compile "com.typesafe.scala-logging:scala-logging-slf4j_2.10:2.1.2"
compile "com.typesafe:config:1.3.0"
compile 'ch.qos.logback:logback-classic:1.1.6'
compile "javax.servlet:jstl:1.2"
compile 'org.apache.commons:commons-dbcp2:2.1.1'
compile 'javax.transaction:jta:1.1'
compile 'mysql:mysql-connector-java:5.1.27'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.2'

providedCompile "javax.servlet:javax.servlet-api:3.1.0"

testCompile "org.springframework.boot:spring-boot-starter-web:1.3.2.RELEASE"
testCompile "org.springframework:spring-test:${springFrameworkVersion}"
testCompile group: "junit", name: "junit", version: "4.12"
testCompile 'com.h2database:h2:1.4.191'
def tomcatVersion = '7.0.59'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
            "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}",
            "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"

}

It can be seen that it is actually a dynamic script, based on groovy syntax, so you can flexibly add code in it to achieve the management functions you need

 

2. Code structure



 As shown in the figure, src is divided into main and test, the core code of the framework is written under src/main/scala, web static resources are stored in the webapp directory, and the focus is placed under resources, as shown in the figure



 
 You can see that there is a dev directory under resources, which stores the application.conf file. This file defines some configurations that need to be used in the project, such as database link, hibernate configuration, etc. Here you can also see that there is also an application.conf under the resources directory. The framework will first read the following configuration, and then read the application.conf in the corresponding environment, such as the above dev, so in dev you can override Configuration information of application.conf under resources. Besides dev, you can also configure test, uat, staging and production. You need to create a directory for it accordingly.

 

db.sql provides the sql for creating all data tables. Currently, it provides the most important tables, User, Role, UserRole and RoleAuthority tables, which are used by spring security.

 

logback.xml is used to configure the log of the application

 

All business-related development codes are stored in the groovy directory. Our main concern is this directory, and all dynamic implementations are here. You need to create groovy files here, but you don't need to use groovy syntax, because groovy also supports native java syntax, so you only need to write java code. But it is recommended that you learn groovy, you will like its dynamic features and concise codelaughing out loud

Here is a little explanation why the groovy directory is placed under resources. There is a groovy configuration in application.conf, which requires you to set the root directory of the groovy file. The purpose is to let the framework know the location of the groovy file and read the source code for dynamic class loading. If it is placed in src Under the hood, the IDE will automatically compile the groovy classes, making it impossible to dynamically read the source files. And when you need to deploy your code to the official environment, you need to deploy your groovy files to a file directory separately, and re-specify the directory in the application.conf file, because war will not process these groovy files. Pack.

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327064274&siteId=291194637