使用idea新建一个gradle工程。
创建完成后效果如下
将原始文件内容修改为
//配置所有项目公共内容
allprojects {
//指定需要的插件
//指定语言
apply plugin: 'java'
//指定编辑器
apply plugin: 'idea'
//配置项目信息
group 'org.example'
version '1.0-SNAPSHOT'
//配置仓库
repositories{
mavenLocal()
mavenCentral()
}
}
//配置子工程
subprojects {
//指定编译版本
sourceCompatibility = 1.8
targetCompatibility = 1.8
//配置字符编码
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
//配置全局依赖版本信息
ext {
junitVersion = '4.12'
}
//配置子模块依赖
dependencies {
testCompile group: 'junit', name: 'junit', version:"${junitVersion}"
}
}
删除重复部分,添加私有部分,本示例中需要在web子模块中导入另一子模块,并添加servlet依赖,文件内容如下,另一子项目直接清空文件内容
plugins {
id 'war'
}
dependencies {
//添加子模块
compile project(":gradledemo_1")
//引入servlet
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
}
添加内容
package com.test.service;
/**
* @program: gradledemo
* @description:
* @author: Mr.Nie
* @create: 2020-07-11 10:49
**/
public class TestService {
public String test(){
return "测试成功";
}
}
添加内容
package com.test.controller;
import com.test.service.TestService;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
/**
* @program: gradledemo
* @description:
* @author: Mr.Nie
* @create: 2020-07-11 10:51
**/
@WebServlet("/user")
public class TestController extends HttpServlet {
TestService testService = new TestService();
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
res.setCharacterEncoding("utf-8");
String result = testService.test();
req.setAttribute("result",result);
req.getRequestDispatcher("list.jsp").forward(req,res);
}
}
内容如下
<%--
Created by IntelliJ IDEA.
User: nnweb
Date: 2020/7/11
Time: 10:53
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${result}
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: nnweb
Date: 2020/7/11
Time: 10:47
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="user">test</a>
</body>
</html>
查看浏览器页面
点击测试
本文地址:https://blog.csdn.net/a1103313049/article/details/107281038
如对本文有疑问, 点击进行留言回复!!
网友评论