用了springMVC之后感觉好方便,好奇心驱使我自己造了个springMVC。
构造MVC主要是了解它的注解的作用与怎么去通过反射来实例化对象,并得到对象方法。
我的构造图
一、注解代码
package cn.lyj.annotation;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
@Documented
public @interface LAnnotation {
String value() default "";
}
二、测试类
(1)UserController
package cn.lyj.controller;
import cn.lyj.annotation.LAnnotation;
@LAnnotation("/user")
public class UserController {
@LAnnotation("/add")
public String add(){
return "添加了一个用户!";
}
@LAnnotation("/delete")
public String delete(){
return "删除了一个用户!";
}
}
(2)DogController
package cn.lyj.controller;
import cn.lyj.annotation.LAnnotation;
@LAnnotation("/dog")
public class DogController {
@LAnnotation("/add")
public String add(){
return "添加了一个狗!";
}
@LAnnotation("/delete")
public String delete(){
return "删除了一个狗!";
}
}
三、代码核心DispatcherServlet
package cn.lyj.servlet;
import cn.lyj.annotation.LAnnotation;
import javax.servlet.ServletException;
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.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 LyjDispatcherServlet extends HttpServlet {
public LyjDispatcherServlet(){
super();
}
private static String pacKage="cn.lyj.controller"; //存放包路劲
private static Map<String,Method> methodMap=new HashMap<>(); //存放路劲加方法对象
private static Map<String,Object> objectMap=new HashMap<>(); //存放路劲对象实体
private static List<String> ClassName=new ArrayList<>(); //存放包内的文件名总路径(所有类文件)
private void doPackage(){ //扫包
String u=pacKage.replaceAll("\\.","/");
URL url =this.getClass().getClassLoader().getResource(u); //该类的根路径
File file=new File(url.getPath());
String[] fileName=file.list();
for (String s:fileName){
s=s.replaceAll("\\.class","");
ClassName.add(pacKage+"."+s); //把包内的类对象丢进集合内
}
}
@Override
public void init() {
doPackage();
for (String s:ClassName){ //实例化对象,得到对象实体与对象方法数组
Class<?> cla=null;
try {
cla=Class.forName(s);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Boolean flag=cla.isAnnotationPresent(LAnnotation.class); //判断该类是否有该标签
if (flag){
Object obj=null; //实例化对象
try {
obj=cla.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
String url="";
if (cla.getAnnotation(LAnnotation.class)!=null){ // 得到类上面的标签内容
url=cla.getAnnotation(LAnnotation.class).value();
}
Method [] methods=cla.getDeclaredMethods(); //遍历该类的所有方法
for (Method m:methods){
if (m.getAnnotation(LAnnotation.class)!=null){ //如果方法中存在该标签
String methodName=m.getAnnotation(LAnnotation.class).value();
methodMap.put(url+methodName,m); //存放方法,url+methodName为该方法的总路劲名
objectMap.put(url+methodName,obj); //存放对象
}
}
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8"); //防止页面中文乱码
String url=request.getServletPath(); //得到浏览器的请求路劲
Method method = methodMap.get(url);
Object obj = objectMap.get(url);
if (method==null||obj==null){
response.getWriter().write("404 --NOTMETHOD!");
return;
}
try {
response.getWriter().write((String) method.invoke(obj));//存在就把结果打印到网页上
return;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
四、web里的XML代码
<!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>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>cn.lyj.servlet.LyjDispatcherServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
五、测试效果
学完了SpringMVC再自己去模拟一个SpringMVC出来,这种感觉真的不一样,这中间就好像一个质的飞越,能让你学会SpringMVC后再拥有SpringMVC。