初始化
commit
5aae4f6970
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CompilerConfiguration">
|
||||||
|
<annotationProcessing>
|
||||||
|
<profile name="Maven default annotation processors profile" enabled="true">
|
||||||
|
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||||
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||||
|
<outputRelativeToContentRoot value="true" />
|
||||||
|
<module name="SpringMvc" />
|
||||||
|
</profile>
|
||||||
|
</annotationProcessing>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="RemoteRepositoriesConfiguration">
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Maven Central repository" />
|
||||||
|
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Central Repository" />
|
||||||
|
<option name="url" value="http://maven.aliyun.com/nexus/content/groups/public/" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="jboss.community" />
|
||||||
|
<option name="name" value="JBoss Community repository" />
|
||||||
|
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||||
|
</remote-repository>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="FrameworkDetectionExcludesConfiguration">
|
||||||
|
<file type="web" url="file://$PROJECT_DIR$" />
|
||||||
|
</component>
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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.png" 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>
|
|
@ -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>
|
|
@ -0,0 +1,2 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4" />
|
|
@ -0,0 +1,87 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
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.tong</groupId>
|
||||||
|
<artifactId>SpringMvc</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
|
||||||
|
<name>SpringMvc Maven Webapp</name>
|
||||||
|
<!-- FIXME change it to the project's website -->
|
||||||
|
<url>http://www.example.com</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</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>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>SpringMvc</finalName>
|
||||||
|
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-clean-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
</plugin>
|
||||||
|
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
|
<version>3.3.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.10.1</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.22.2</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<version>3.4.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-install-plugin</artifactId>
|
||||||
|
<version>3.0.1</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-deploy-plugin</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
|
</build>
|
||||||
|
</project>
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.tong.annaotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Target({ElementType.FIELD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface EnjoyAutowired {
|
||||||
|
String value() default "";
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.tong.annaotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Target({ElementType.TYPE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface EnjoyController {
|
||||||
|
String value() default "";
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.tong.annaotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface EnjoyRequestMapping {
|
||||||
|
String value() default "";
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.tong.annaotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Target({ElementType.PARAMETER})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface EnjoyRequestParam {
|
||||||
|
String value() default "";
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.tong.annaotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Target({ElementType.TYPE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface EnjoyService {
|
||||||
|
String value() default "";
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.tong.controller;
|
||||||
|
|
||||||
|
import com.tong.annaotation.EnjoyAutowired;
|
||||||
|
import com.tong.annaotation.EnjoyController;
|
||||||
|
import com.tong.annaotation.EnjoyRequestMapping;
|
||||||
|
import com.tong.annaotation.EnjoyRequestParam;
|
||||||
|
import com.tong.service.TongService;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
@EnjoyController
|
||||||
|
@EnjoyRequestMapping("/tong")
|
||||||
|
public class TongController {
|
||||||
|
|
||||||
|
@EnjoyAutowired("TongServiceImpl")
|
||||||
|
private TongService tongService;
|
||||||
|
|
||||||
|
@EnjoyRequestMapping("/query")
|
||||||
|
public void query(HttpServletRequest request, HttpServletResponse response, @EnjoyRequestParam("name") String name,
|
||||||
|
@EnjoyRequestParam("age") String age) {
|
||||||
|
try {
|
||||||
|
PrintWriter pw = response.getWriter();
|
||||||
|
String result = tongService.query(name, age);
|
||||||
|
pw.write(result);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.tong.service;
|
||||||
|
|
||||||
|
public interface TongService {
|
||||||
|
String query(String name, String age);
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.tong.service.impl;
|
||||||
|
|
||||||
|
import com.tong.annaotation.EnjoyService;
|
||||||
|
import com.tong.service.TongService;
|
||||||
|
|
||||||
|
@EnjoyService("TongServiceImpl") //map.put(TongServiceImpl, new TongServiceImpl())
|
||||||
|
public class TongServiceImpl implements TongService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String query(String name, String age) {
|
||||||
|
return "name==="+name+"; age==="+age;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,216 @@
|
||||||
|
package com.tong.servlet;
|
||||||
|
|
||||||
|
import com.tong.annaotation.*;
|
||||||
|
import com.tong.controller.TongController;
|
||||||
|
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 DispatcherServlet 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;
|
||||||
|
|
||||||
|
public void init(ServletConfig config) throws ServletException {
|
||||||
|
//把所有的bean扫描 ----扫描所有的class文件
|
||||||
|
scanPackage("com.tong");
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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(EnjoyController.class)) {
|
||||||
|
Object instance = clazz.newInstance();//创建控制类
|
||||||
|
EnjoyRequestMapping requestMapping = clazz.getAnnotation(EnjoyRequestMapping.class);
|
||||||
|
String rmvalue = requestMapping.value();
|
||||||
|
beans.put(rmvalue, instance);
|
||||||
|
}else if (clazz.isAnnotationPresent(EnjoyService.class)) {
|
||||||
|
EnjoyService service = clazz.getAnnotation(EnjoyService.class);
|
||||||
|
Object instance = clazz.newInstance();
|
||||||
|
beans.put(service.value(), instance);
|
||||||
|
}else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (InstantiationException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalAccessException 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(EnjoyController.class)) {
|
||||||
|
Field[] fields = clazz.getDeclaredFields();
|
||||||
|
for (Field field : fields) {
|
||||||
|
if (field.isAnnotationPresent(EnjoyAutowired.class)) {
|
||||||
|
EnjoyAutowired auto = field.getAnnotation(EnjoyAutowired.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(EnjoyController.class)) {
|
||||||
|
EnjoyRequestMapping requestMapping = clazz.getAnnotation(EnjoyRequestMapping.class);
|
||||||
|
String classPath = requestMapping.value();
|
||||||
|
|
||||||
|
Method[] methods = clazz.getMethods();
|
||||||
|
for (Method method : methods) {
|
||||||
|
if (method.isAnnotationPresent(EnjoyRequestMapping.class)) {
|
||||||
|
EnjoyRequestMapping methodMapping = method.getAnnotation(EnjoyRequestMapping.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);
|
||||||
|
|
||||||
|
TongController instance = (TongController) beans.get("/"+path.split("/")[1]);
|
||||||
|
|
||||||
|
Object[] args = hand(req, resp, method);
|
||||||
|
try {
|
||||||
|
method.invoke(instance, args);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Object[] hand(HttpServletRequest request, HttpServletResponse response, Method method) {
|
||||||
|
//拿到待执行的方法有哪些参数
|
||||||
|
Class<?>[] paramClazzs = method.getParameterTypes();
|
||||||
|
|
||||||
|
//根据参数的个数,new 一个参数的数组,将方法的所有参数赋值到args
|
||||||
|
Object[] args = new Object[paramClazzs.length];
|
||||||
|
|
||||||
|
int args_i = 0;
|
||||||
|
int index = 0;
|
||||||
|
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];
|
||||||
|
if (paramAns.length > 0) {
|
||||||
|
for (Annotation paramAn : paramAns) {
|
||||||
|
if (EnjoyRequestParam.class.isAssignableFrom(paramAn.getClass())) {
|
||||||
|
EnjoyRequestParam rp = (EnjoyRequestParam) paramAn;
|
||||||
|
|
||||||
|
args[args_i++] = request.getParameter(rp.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
<!DOCTYPE web-app PUBLIC
|
||||||
|
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
|
||||||
|
"http://java.sun.com/dtd/web-app_2_3.dtd" >
|
||||||
|
|
||||||
|
<web-app>
|
||||||
|
<display-name>Archetype Created Web Application</display-name>
|
||||||
|
|
||||||
|
<!-- SpringMvc的核心控制器 -->
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>DispatcherServlet</servlet-name>
|
||||||
|
<servlet-class>com.tong.servlet.DispatcherServlet</servlet-class>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>DispatcherServlet</servlet-name>
|
||||||
|
<url-pattern>/</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
</web-app>
|
|
@ -0,0 +1,5 @@
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h2>Hello World!</h2>
|
||||||
|
</body>
|
||||||
|
</html>
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,18 @@
|
||||||
|
<!DOCTYPE web-app PUBLIC
|
||||||
|
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
|
||||||
|
"http://java.sun.com/dtd/web-app_2_3.dtd" >
|
||||||
|
|
||||||
|
<web-app>
|
||||||
|
<display-name>Archetype Created Web Application</display-name>
|
||||||
|
|
||||||
|
<!-- SpringMvc的核心控制器 -->
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>DispatcherServlet</servlet-name>
|
||||||
|
<servlet-class>com.tong.servlet.DispatcherServlet</servlet-class>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>DispatcherServlet</servlet-name>
|
||||||
|
<url-pattern>/</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
</web-app>
|
|
@ -0,0 +1,5 @@
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h2>Hello World!</h2>
|
||||||
|
</body>
|
||||||
|
</html>
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,3 @@
|
||||||
|
artifactId=SpringMvc
|
||||||
|
groupId=com.tong
|
||||||
|
version=1.0-SNAPSHOT
|
|
@ -0,0 +1,9 @@
|
||||||
|
com\tong\servlet\DispatcherServlet.class
|
||||||
|
com\tong\annaotation\EnjoyController.class
|
||||||
|
com\tong\service\TongService.class
|
||||||
|
com\tong\annaotation\EnjoyRequestParam.class
|
||||||
|
com\tong\service\impl\TongServiceImpl.class
|
||||||
|
com\tong\annaotation\EnjoyRequestMapping.class
|
||||||
|
com\tong\annaotation\EnjoyService.class
|
||||||
|
com\tong\controller\TongController.class
|
||||||
|
com\tong\annaotation\EnjoyAutowired.class
|
|
@ -0,0 +1,9 @@
|
||||||
|
D:\tong\SpringMvc\src\main\java\com\tong\annaotation\EnjoyService.java
|
||||||
|
D:\tong\SpringMvc\src\main\java\com\tong\annaotation\EnjoyRequestMapping.java
|
||||||
|
D:\tong\SpringMvc\src\main\java\com\tong\annaotation\EnjoyAutowired.java
|
||||||
|
D:\tong\SpringMvc\src\main\java\com\tong\annaotation\EnjoyController.java
|
||||||
|
D:\tong\SpringMvc\src\main\java\com\tong\servlet\DispatcherServlet.java
|
||||||
|
D:\tong\SpringMvc\src\main\java\com\tong\controller\TongController.java
|
||||||
|
D:\tong\SpringMvc\src\main\java\com\tong\service\impl\TongServiceImpl.java
|
||||||
|
D:\tong\SpringMvc\src\main\java\com\tong\annaotation\EnjoyRequestParam.java
|
||||||
|
D:\tong\SpringMvc\src\main\java\com\tong\service\TongService.java
|
Loading…
Reference in New Issue