手写springmvc

master
张海宁 2023-10-30 09:01:55 +08:00
commit 866800979c
23 changed files with 788 additions and 0 deletions

38
.gitignore vendored 100644
View File

@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
.idea/.gitignore vendored 100644
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/webapp" charset="UTF-8" />
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

View File

@ -0,0 +1,5 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
</profile>
</component>

17
.idea/misc.xml 100644
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
.idea/vcs.xml 100644
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

49
REDME.md 100644
View File

@ -0,0 +1,49 @@
是的Spring MVC 项目通常是Web项目用于构建Web应用程序。它基于MVCModel-View-Controller模式用于处理HTTP请求和响应。
下面是一个典型的Spring MVC项目的整体文件目录结构示例
```
- src
- main
- java
- com
- example
- controller
- HomeController.java # 控制器类
- model
- User.java # 模型类
- repository
- UserRepository.java # 数据访问接口
- service
- UserService.java # 服务接口
- UserServiceImpl.java # 服务实现类
- config
- WebConfig.java # Spring MVC配置类
- resources
- static # 静态资源文件夹如CSS、JavaScript等
- templates # 视图模板文件夹如HTML、JSP等
- application.properties # 应用程序配置文件
- webapp
- WEB-INF
- views # 视图文件夹如JSP、Thymeleaf模板等
- web.xml # Web应用配置文件
```
以上是一个基本的示例文件目录结构,你可以根据实际的需要进行调整和扩展。在该目录结构中:
- `src/main/java` 目录存放应用程序的Java源代码。
- `src/main/resources` 目录存放应用程序的配置文件和资源文件。
- `src/main/java/com/example/controller` 目录存放控制器Controller处理HTTP请求。
- `src/main/java/com/example/model` 目录存放模型Model定义业务实体。
- `src/main/java/com/example/repository` 目录存放数据访问接口。
- `src/main/java/com/example/service` 目录存放服务Service接口和实现类。
- `src/main/java/com/example/config` 目录存放Spring MVC的配置类。
- `src/main/resources/static` 目录存放静态资源文件如CSS、JavaScript等。
- `src/main/resources/templates` 目录存放视图模板文件如HTML、JSP等。
- `src/main/resources/application.properties` 是应用程序的配置文件,配置各种属性和参数。
- `webapp/WEB-INF` 目录是Web应用的根目录存放Web相关的配置和资源。
- `webapp/WEB-INF/views` 目录存放JSP等视图文件。
这只是一个基本的示例实际的Spring MVC项目的文件目录结构可能根据具体需求有所不同。你可以根据自己的项目要求和团队的约定进行调整。
希望能解答你的问题!如果还有其他疑问,请随时提问。

Binary file not shown.

View File

@ -0,0 +1,5 @@
Manifest-Version: 1.0
Created-By: IntelliJ IDEA
Built-By: Administrator
Build-Jdk: Oracle OpenJDK version 17.0.8

57
pom.xml 100644
View File

@ -0,0 +1,57 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhn</groupId>
<artifactId>springZHN</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>springZHN</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.78</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.14</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,15 @@
package com.zhn.annotation;
import java.lang.annotation.*;
//代表在成员变量上的注解
@Target ({ElementType.FIELD})
//代表在运行上可以通过反射机制获取对象
@Retention (RetentionPolicy.RUNTIME)
//javadoc
@Documented
public @interface ZhnAuto {
//加了这就注解后面就可以带值
String value () default "";
}

View File

@ -0,0 +1,18 @@
package com.zhn.annotation;
import java.lang.annotation.*;
/**
* ,
*/
//代表作用范围实在类上的
@Target ({ElementType.TYPE})
//代表在运行上可以通过反射机制获取对象
@Retention (RetentionPolicy.RUNTIME)
//javadoc
@Documented
public @interface ZhnContainer {
//加了这就注解后面就可以带值
String value () default "";
}

View File

@ -0,0 +1,14 @@
package com.zhn.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//路径匹配先设置类型
@Target ({ElementType.TYPE, ElementType.METHOD})
@Retention (RetentionPolicy.RUNTIME)
public @interface ZhnMapping {
String value () default "";
}

View File

@ -0,0 +1,11 @@
package com.zhn.annotation;
import java.lang.annotation.*;
@Target ({ElementType.PARAMETER})
@Retention (RetentionPolicy.RUNTIME)
@Documented
public @interface ZhnReqParam {
String value () default "";
}

View File

@ -0,0 +1,23 @@
package com.zhn.controller;
import com.zhn.annotation.ZhnAuto;
import com.zhn.annotation.ZhnContainer;
import com.zhn.annotation.ZhnMapping;
import com.zhn.service.ZhnService;
@ZhnContainer
@ZhnMapping ("/ni")
public class ZhnController {
@ZhnAuto
ZhnService zhnService;
@ZhnMapping ("/aa")
public String aa (String name, String age) {
System.out.println ("chia");
return zhnService.initReq (name, age);
}
}

View File

@ -0,0 +1,36 @@
package com.zhn.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.regex.Pattern;
/**
* @author : [Administrator]
* @version : [v1.0]
* @description : [URL ]
* @createTime : [2023/10/29 15:30]
* @updateUser : [Administrator]
* @updateTime : [2023/10/29 15:30]
* @updateRemark : []
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Handler {
//属性表示处理器对象,通常是一个控制器类的实例。
private Object controller;
//属性表示处理器方法,即处理请求的方法.
private Method method;
//属性表示 URL 请求的正则表达式模式,用于匹配请求的 URL。
private Pattern pattern;
//属性是一个 Map用于存储请求参数和参数在方法参数列表中的索引位置的映射关系。键为参数名值为参数在方法参数列表中的索引
private Map<String, Integer> paramIndexMapping;
}

View File

@ -0,0 +1,16 @@
package com.zhn.service;
/**
* @author : [Administrator]
* @version : [v1.0]
* @description : []
* @createTime : [2023/10/29 10:55]
* @updateUser : [Administrator]
* @updateTime : [2023/10/29 10:55]
* @updateRemark : []
*/
public interface ZhnService {
String initReq (String name, String age);
}

View File

@ -0,0 +1,21 @@
package com.zhn.service.impl;
import com.zhn.annotation.ZhnContainer;
import com.zhn.service.ZhnService;
/**
* @author : [Administrator]
* @version : [v1.0]
* @description : [Zhn]
* @createTime : [2023/10/29 10:55]
* @updateUser : [Administrator]
* @updateTime : [2023/10/29 10:55]
* @updateRemark : []
*/
@ZhnContainer
public class ZhnServiceImpl implements ZhnService {
@Override
public String initReq (String name, String age) {
return "name===" + name + "; age===" + age;
}
}

View File

@ -0,0 +1,243 @@
package com.zhn.servlet;
import com.zhn.annotation.ZhnAuto;
import com.zhn.annotation.ZhnContainer;
import com.zhn.annotation.ZhnMapping;
import com.zhn.annotation.ZhnReqParam;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ZhnServlet extends HttpServlet {
List<String> classNames = new ArrayList<String> ();
Map<String, Object> beans = new HashMap<String, Object> ();
Map<String, Object> handlerMap = new HashMap<String, Object> ();
// private static final long serialVersionUID = 1L;
@Override
public void init (ServletConfig config) throws ServletException {
//把所有的bean扫描 ----扫描所有的class文件
scanPackage ("com.zhn");
doInstance (); //根据全类名创建bean
doIoc (); //根据bean进行依赖注入
buildUrlMapping (); //建立映射关系
}
private void scanPackage (String basePackage) {
URL url = this.getClass ().getClassLoader ().getResource ("/" + basePackage.replaceAll ("\\.", "/"));
String fileStr = url.getFile ();
File file = new File (fileStr);
String[] filesStr = file.list ();
if (filesStr != null) {
for (String path : filesStr) {
File filePath = new File (fileStr + path);
if (filePath.isDirectory ()) {
scanPackage (basePackage + "." + path);
}
else {
//加入list
classNames.add (basePackage + "." + filePath.getName ());
}
}
}
}
//根据扫描的list全类名进行实例化
private void doInstance () {
if (classNames.size () <= 0) {
System.out.println ("包扫描失败.....");
return;
}
//遍历list的所有Class类
for (String className : classNames) {
String cn = className.replace (".class", "");
try {
Class<?> clazz = Class.forName (cn);
if (!clazz.isAnnotationPresent (ZhnContainer.class)) {
if (clazz.isAnnotationPresent (ZhnContainer.class)) {
ZhnContainer service = clazz.getAnnotation (ZhnContainer.class);
Object instance = clazz.newInstance ();
beans.put (service.value (), instance);
}
else {
continue;
}
}
else {
Object instance = clazz.newInstance ();//创建控制类
ZhnMapping requestMapping = clazz.getAnnotation (ZhnMapping.class);
String rmvalue = requestMapping.value ();
beans.put (rmvalue, instance);
}
}
catch (ClassNotFoundException | NullPointerException | IllegalAccessException | InstantiationException e) {
e.printStackTrace ();
}
}
}
//把service注入到controller
public void doIoc () {
if (beans.entrySet ().size () <= 0) {
System.out.println ("没有一个实例化类");
}
//把map里所有的实例化遍历出来
for (Map.Entry<String, Object> entry : beans.entrySet ()) {
Object instance = entry.getValue ();
Class<?> clazz = instance.getClass ();
if (clazz.isAnnotationPresent (ZhnContainer.class)) {
Field[] fields = clazz.getDeclaredFields ();
for (Field field : fields) {
if (field.isAnnotationPresent (ZhnAuto.class)) {
ZhnAuto auto = field.getAnnotation (ZhnAuto.class);
String key = auto.value ();
field.setAccessible (true);
try {
field.set (instance, beans.get (key));
}
catch (IllegalAccessException e) {
e.printStackTrace ();
}
}
else {
continue;
}
}
}
else {
continue;
}
}
}
private void buildUrlMapping () {
if (beans.entrySet ().size () <= 0) {
System.out.println ("没有实例化____");
return;
}
for (Map.Entry<String, Object> entry : beans.entrySet ()) {
Object instance = entry.getValue ();
Class<?> clazz = instance.getClass ();
if (clazz.isAnnotationPresent (ZhnContainer.class)) {
ZhnMapping requestMapping = clazz.getAnnotation (ZhnMapping.class);
String classPath = requestMapping.value ();
Method[] methods = clazz.getMethods ();
for (Method method : methods) {
if (method.isAnnotationPresent (ZhnMapping.class)) {
ZhnMapping methodMapping = method.getAnnotation (ZhnMapping.class);
String methodPath = methodMapping.value ();
handlerMap.put (classPath + methodPath, method);
}
else {
continue;
}
}
}
else {
continue;
}
}
}
@Override
protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost (req, resp);
}
@Override
protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取请求路径
String uri = req.getRequestURI ();
String context = req.getContextPath ();
String path = uri.replace (context, "");
Method method = (Method) handlerMap.get (path);
ZhnContainer instance = null;
try {
instance = (ZhnContainer) beans.get ("/" + path.split ("/")[1]);
}
catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace ();
}
Object[] args = hand (req, resp, method);
try {
method.invoke (instance, args);
}
catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace ();
}
}
private static Object[] hand (HttpServletRequest request, HttpServletResponse response, Method method) {
//拿到待执行的方法有哪些参数
Class<?>[] paramClazzs = null;
try {
paramClazzs = method.getParameterTypes ();
}
catch (NullPointerException e) {
e.printStackTrace ();
}
//根据参数的个数new 一个参数的数组将方法的所有参数赋值到args
Object[] args = new Object[0];
if (paramClazzs != null) {
args = new Object[paramClazzs.length];
}
int args_i = 0;
int index = 0;
if (paramClazzs != null) {
for (Class<?> paramClazz : paramClazzs) {
if (ServletRequest.class.isAssignableFrom (paramClazz)) {
args[args_i++] = request;
}
if (ServletResponse.class.isAssignableFrom (paramClazz)) {
args[args_i++] = response;
}
Annotation[] paramAns = method.getParameterAnnotations ()[index];
for (Annotation paramAn : paramAns) {
if (ZhnReqParam.class.isAssignableFrom (paramAn.getClass ())) {
ZhnReqParam rp = (ZhnReqParam) paramAn;
args[args_i++] = request.getParameter (rp.value ());
}
}
index++;
}
}
return args;
}
}

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>springZHN</display-name>
<!--扫描注册servlet-->
<servlet>
<servlet-name>ZhnServlet</servlet-name>
<servlet-class>com.zhn.servlet.ZhnServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<!--请求路径拦截-->
<servlet-mapping>
<servlet-name>ZhnServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

View File

@ -0,0 +1,16 @@
<!doctype html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html lang="en">
<head>
<meta charset="UTF- 8">
<meta content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
name="viewport">
<meta content="ie=edge" http-equiv="X-UA-Compatible">
<title>Document</title>
</head>
<body>
<h2>hash????</h2>
</body>
</html>

View File

@ -0,0 +1,34 @@
package com.zhn;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase {
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest (String testName) {
super (testName);
}
/**
* @return the suite of tests being tested
*/
public static Test suite () {
return new TestSuite (AppTest.class);
}
/**
* Rigourous Test :-)
*/
public void testApp () {
assertTrue (true);
}
}