这步加不加入仓库jar包的优先级 都可以
在GradelParent工程选择new model
此时新建完的工程 默认都是gradle Java工程 要改成web工程:
添加组件war
在main下面建立webapp 、webapp下面建立WEB-INF目录 、WEB-INF目录 下面添加web.xml文件、WEB-INF目录下面添加pages目录(这个可以自定义,看我们spring-mvc里面配置的是什么视图解析路径)、添加各种spring配置文件
把之前的gradel web工程里面的内容copy过来:
build.gradle(该文件 不必拷贝 在后面 会在父类的build.gradle文件中 引入jar包依赖 达到所有子模块公用的效果)
plugins {
id 'java'
/**
* 打成war包的形式
*/
id 'war'
}
group 'com.lz.web'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
}
/**
* maven:provided
* provided:在编译和测试的过程有效,最后生成war包时不会加入,诸如:servlet-api,
* 因为servlet-api,tomcat等web服务器已经存在了,如果再打包会冲突
* gradle:provided和compile的区别:
* 如果你的jar包/依赖代码 仅在编译的时候需要,但是在运行时不需要依赖,就用providedCompile
* 此属性 替代了maven中的provided
*/
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework', name: 'spring-context', version: '5.0.2.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version: '5.0.2.RELEASE'
compile group: 'org.springframework', name: 'spring-webmvc', version: '5.0.2.RELEASE'
compile group: 'org.springframework', name: 'spring-tx', version: '5.0.2.RELEASE'
compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version:'3.1.0'
compileOnly ('javax.servlet:jsp-api:2.0'){
exclude group: 'javax.servlet'
}
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.54'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-web', version: '2.11.1'
}
web.xml:
chinaTelecomLogAnalyzer
index.html
index.htm
index.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
log4jConfiguration
classpath:log4j2.xml
org.apache.logging.log4j.web.Log4jServletContextListener
log4jServletFilter
org.apache.logging.log4j.web.Log4jServletFilter
log4jServletFilter
/*
REQUEST
FORWARD
INCLUDE
ERROR
manager
org.springframework.web.servlet.DispatcherServlet
springmvc
contextConfigLocation
classpath:spring-mvc.xml
1
manager
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
characterEncodingFilter
*
applicationContext.xml:
log4j2.xml:
config.properties:
#待添加配置
spring-mvc.xml:
hello.jsp:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/12/31
Time: 22:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
hello jsp!
index.jsp:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/12/31
Time: 22:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
HelloController:
package com.lz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/toHelloJsp")
public String toHelloJsp(){
return "hello";
}
}
gradle-web model的目录结构:
接下来 再新建其他model模块(如gradle-service、gradle-dao、gradle-domain) 如果是web工程 接着如上将java gradle工程改造成web gradle工程 如果仅仅是java gradle工程 那么不用改造
建立完成后 工程结构如图:
此时位于父类工程GradleParent下面的settings.gradle文件 可以看到父子工程结构
此时在父类工程的build.gradle文件中 引入jar包依赖达到所有子模块 公用父类引入jar包的作用
首先需要将父类工程的build.gradle所有内容包裹在标签:subprojects{}中:
/**所有子项目的通用配置
* 插件 不支持这种写法:
* plugins {
* id 'java'
* }
*/
subprojects {
apply plugin: 'java'
group 'com.lz.gradle'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
}
/**
* maven:provided
* provided:在编译和测试的过程有效,最后生成war包时不会加入,诸如:servlet-api,
* 因为servlet-api,tomcat等web服务器已经存在了,如果再打包会冲突
* gradle:provided和compile的区别:
* 如果你的jar包/依赖代码 仅在编译的时候需要,但是在运行时不需要依赖,就用providedCompile
* 此属性 替代了maven中的provided
*/
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework', name: 'spring-context', version: '5.0.2.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version: '5.0.2.RELEASE'
compile group: 'org.springframework', name: 'spring-webmvc', version: '5.0.2.RELEASE'
compile group: 'org.springframework', name: 'spring-tx', version: '5.0.2.RELEASE'
compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version:'3.1.0'
compileOnly ('javax.servlet:jsp-api:2.0'){
exclude group: 'javax.servlet'
}
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.54'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-web', version: '2.11.1'
}
}
把所有子模块中的build.gradle文件重复的元素都清空,所以只有gadle的web工程的build.gradle文件 会留下 其余的非web工程的build.gradel文件 将都是清空
gradle-web-->build.gradle文件:
plugins {
/**
* 打成war包的形式
*/
id 'war'
}
gradle-dao-->build.gradle文件:
#清空了
gradle-service-->build.gradle文件:
#清空了
gradle-domain-->build.gradle文件:
#清空了
接下来 在各个子模块中的build.gradle文件 开始加入 model之间的引用关系,格式为:
dependencies {
compile project(":父类工程settings.gradle文件中模块名称")
}
各个模块的引用链关系为:web->service->dao->domain
gradle-domain-->build.gradle文件:
#清空了
gradle-domain-->build.gradle文件:
dependencies {
compile project(":gradle-domain")
}
gradle-service-->build.gradle文件:
dependencies {
compile project(":gradle-dao")
}
gradle-web-->build.gradle文件:
plugins {
/**
* 打成war包的形式
*/
id 'war'
}
dependencies {
compile project(":gradle-service")
}
在各个模块 编写dao service
Hello
package com.lz.pojo;
public class Hello {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
HelloDao:
package com.lz.dao;
import com.lz.pojo.Hello;
public interface HelloDao {
public Hello getHello();
}
HelloDaoImpl:
package com.lz.dao.impl;
import com.lz.dao.HelloDao;
import com.lz.pojo.Hello;
import org.springframework.stereotype.Component;
@Component("helloDao")
public class HelloDaoImpl implements HelloDao {
@Override
public Hello getHello() {
Hello hello = new Hello();
hello.setMsg("hahhah");
return hello;
}
}
HelloService:
package com.lz.service;
import com.lz.pojo.Hello;
public interface HelloService {
public Hello getHello();
}
HelloServiceImpl
package com.lz.service.impl;
import com.lz.dao.HelloDao;
import com.lz.pojo.Hello;
import com.lz.service.HelloService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("helloService")
public class HelloServiceImpl implements HelloService {
@Resource
private HelloDao helloDao;
@Override
public Hello getHello() {
return helloDao.getHello();
}
}
HelloController改造:
package com.lz.controller;
import com.lz.pojo.Hello;
import com.lz.service.HelloService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
@Controller
@RequestMapping("/hello")
public class HelloController {
@Resource
private HelloService helloService;
@RequestMapping("/toHelloJsp")
public String toHelloJsp(Model model){
Hello hello = helloService.getHello();
model.addAttribute("msg",hello);
return "hello";
}
}
hello.jsp改造:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/12/31
Time: 22:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
----${msg.msg}
启动项目运行效果:
工程的整体效果如图: