Skip to content

Commit 682b553

Browse files
author
vison.cao
committed
first commit
0 parents  commit 682b553

File tree

10 files changed

+211
-0
lines changed

10 files changed

+211
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target
2+
tomcat.*
3+
nb-configuration.xml

pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.vison.webmvc</groupId>
6+
<artifactId>web-mvc</artifactId>
7+
<packaging>war</packaging>
8+
<version>1.0-SNAPSHOT</version>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
13+
<maven.compiler.source>11</maven.compiler.source>
14+
<maven.compiler.target>11</maven.compiler.target>
15+
<java.version>11</java.version>
16+
<tomcat.version>9.0.26</tomcat.version>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>javax.websocket</groupId>
22+
<artifactId>javax.websocket-api</artifactId>
23+
<version>1.1</version>
24+
<scope>provided</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.apache.tomcat.embed</groupId>
28+
<artifactId>tomcat-embed-core</artifactId>
29+
<version>${tomcat.version}</version>
30+
<scope>compile</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.apache.tomcat.embed</groupId>
34+
<artifactId>tomcat-embed-jasper</artifactId>
35+
<version>${tomcat.version}</version>
36+
<scope>compile</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.apache.logging.log4j</groupId>
40+
<artifactId>log4j-core</artifactId>
41+
<version>2.12.0</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.apache.logging.log4j</groupId>
45+
<artifactId>log4j-api</artifactId>
46+
<version>2.12.0</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.apache.tomcat</groupId>
50+
<artifactId>tomcat-websocket</artifactId>
51+
<version>${tomcat.version}</version>
52+
</dependency>
53+
</dependencies>
54+
55+
<build>
56+
<finalName>webmvc</finalName>
57+
</build>
58+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.vison.webmvc;
2+
3+
import java.io.File;
4+
import org.apache.catalina.Context;
5+
import org.apache.catalina.WebResourceRoot;
6+
import org.apache.catalina.startup.Tomcat;
7+
import org.apache.catalina.webresources.DirResourceSet;
8+
import org.apache.catalina.webresources.StandardRoot;
9+
import org.apache.logging.log4j.LogManager;
10+
import org.apache.logging.log4j.Logger;
11+
12+
/**
13+
*
14+
* @author vison.cao <visonforcoding@gmail.com>
15+
*/
16+
public class Application {
17+
18+
private static final Logger log = LogManager.getLogger(Application.class);
19+
20+
/**
21+
* @param args the command line arguments
22+
*/
23+
public static void main(String[] args) throws Exception {
24+
// 启动Tomcat:
25+
Tomcat tomcat = new Tomcat();
26+
tomcat.setPort(Integer.getInteger("port", 8081));
27+
log.info("hello");
28+
log.info("server run in http://localhost:8081");
29+
tomcat.getConnector();
30+
// 创建webapp:
31+
Context ctx = tomcat.addWebapp("", new File("src/main/webapp").getAbsolutePath());
32+
WebResourceRoot resources = new StandardRoot(ctx);
33+
resources.addPreResources(
34+
new DirResourceSet(resources, "/WEB-INF/classes", new File("target/classes").getAbsolutePath(), "/"));
35+
ctx.setResources(resources);
36+
tomcat.start();
37+
tomcat.getServer().await();
38+
}
39+
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.vison.webmvc.framework;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import javax.servlet.ServletException;
8+
import javax.servlet.annotation.WebServlet;
9+
import javax.servlet.http.HttpServlet;
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpServletResponse;
12+
13+
/**
14+
*
15+
* @author vison.cao <visonforcoding@gmail.com>
16+
*/
17+
@WebServlet(name = "DispatchServlet", urlPatterns = {"/"})
18+
public class DispatchServlet extends HttpServlet {
19+
20+
private Map<String, GetDispatcher> getMappings = new HashMap<>();
21+
private Map<String, PostDispatcher> postMappings = new HashMap<>();
22+
private ViewEngine viewEngine;
23+
24+
@Override
25+
public void init() throws ServletException {
26+
27+
}
28+
29+
@Override
30+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
31+
resp.setContentType("text/html");
32+
resp.setCharacterEncoding("UTF-8");
33+
String path = req.getRequestURI().substring(req.getContextPath().length());
34+
log(path);
35+
resp.getWriter().write(path);
36+
resp.getWriter().flush();
37+
// 根据路径查找GetDispatcher:
38+
// 调用Controller方法获得返回值:
39+
}
40+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.vison.webmvc.framework;
2+
3+
import java.lang.reflect.Method;
4+
import javax.servlet.http.HttpServletRequest;
5+
import javax.servlet.http.HttpServletResponse;
6+
import javax.servlet.http.HttpSession;
7+
8+
/**
9+
*
10+
* @author vison.cao <visonforcoding@gmail.com>
11+
*/
12+
class GetDispatcher {
13+
14+
Object instance; // Controller实例
15+
Method method; // Controller方法
16+
String[] parameterNames; // 方法参数名称
17+
Class<?>[] parameterClasses; // 方法参数类型
18+
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.vison.webmvc.framework;
7+
8+
/**
9+
*
10+
* @author vison.cao <visonforcoding@gmail.com>
11+
*/
12+
public class ModelAndView {
13+
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.vison.webmvc.framework;
2+
3+
/**
4+
*
5+
* @author vison.cao <visonforcoding@gmail.com>
6+
*/
7+
class PostDispatcher {
8+
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.vison.webmvc.framework;
2+
3+
import java.io.IOException;
4+
import java.io.Writer;
5+
6+
/**
7+
*
8+
* @author vison.cao <visonforcoding@gmail.com>
9+
*/
10+
class ViewEngine {
11+
12+
public void render(ModelAndView mv, Writer writer) throws IOException {
13+
14+
}
15+
16+
}

src/main/webapp/META-INF/context.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Context path="/webmvc"/>

src/main/webapp/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Start Page</title>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6+
</head>
7+
<body>
8+
<h1>Hello World!</h1>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)