命令行创建项目
目录结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| |-- app gradle的名叫app的模块,可以添加其他模块 | |-- build.gradle 项目的主要配置文件,和pom.xml效果一样 | `-- src | |-- main | | |-- java 主要的java源码 | | | `-- ml | | | `-- lightly | | | `-- demo | | | `-- App.java | | |-- webapp 需要自己建,放前端文件 | | | |-- WEB-INF | | | | `-- web.xml | | | |-- index.html | | | |-- css | | | `-- js | | `-- resources 源码所需资源 | `-- test | |-- java 测试的java源码 | | `-- ml | | `-- lightly | | `-- demo | | `-- AppTest.java | `-- resources 测试时所需资源 |-- gradle gradlew脚本所依赖的一些工具 | `-- wrapper | |-- gradle-wrapper.jar | `-- gradle-wrapper.properties |-- gradlew linux如果没有安装gradle,可以使用这个脚本运行项目,效果和gradle命令一样 |-- gradlew.bat windows如果没有安装gradle,可以使用这个脚本运行项目,效果和gradle命令一样 `-- settings.gradle 用于各种模块的引入
|
修改build.gradle
文件在app/build.gradle,可以直接将里边的内容覆盖
GROOVY
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| apply plugin: "java"
apply plugin: "war"
group = 'ml.lightly.demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
dependencies { implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.47' compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1' testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1' }
tasks.withType(JavaCompile) { options.encoding = 'UTF-8' }
repositories { mavenLocal() maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' } maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' } mavenCentral() jcenter() }
tasks.named('test') { useJUnitPlatform() }
|
相关代码编写
index.html
HTML
1 2 3 4
| <form action="/projectName/test" method="get"> <input name="hello" value="world"> <input type="submit" value="submit it"> </form>
|
web.xml常用配置
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>dsj</display-name> <servlet> <servlet-name>HandleServer</servlet-name> <servlet-class>ml.lightly.demo.App</servlet-class> </servlet> <servlet-mapping> <servlet-name>HandleServer</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
|
App.java
JAVA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package ml.lightly.demo;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
public class App extends HttpServlet{ public void init() {}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ response.setCharacterEncoding("UTF-8"); response.setContentType("application/json"); JSONObject json = new JSONObject(); PrintWriter writer = response.getWriter(); try { json.put("key", "value"); } catch (Exception e) { e.printStackTrace(); } finally { writer.print(json); writer.flush(); writer.close(); } response.getWriter().print(json); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ doGet(request, response); } }
|
AppTest.java
JAVA
1 2 3 4 5 6 7 8 9 10 11 12 13
|
package ml.lightly.demo;
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*;
class AppTest { @Test void appHasAGreeting() { System.out.println("asdf"); } }
|
打包编译
BASH
1 2 3 4 5 6 7 8 9
| gradle build
gradle clean build
|