1.创建编写注解类
2.创建不编写beans.xml
3.配置pom.xml , 配置Servlet和dom4j
4.创建编写 监听器解析用来beans.xml
5.创建编写 Servlet类,用来处理请求路径、匹配请求调用对应方法
6.配置web.xml,将监听器类、 Servlet类配置进去
7.创建实际需要打注解的controller类
8.将步骤6的类配置到beans.xml
下面将RequestMapping写错了!!!
注意添加注解,分别是此注解作用在哪里和在什么时候生效
@Target({ElementType.TYPE,ElementType.METHOD})//可以将注解打在类和方法上面
@Retention(RetentionPolicy.RUNTIME)//在程序运行时依然生效
4.0.0
cn.dmc
framework1128
1.0-SNAPSHOT
war
framework1128 Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
junit
junit
4.11
test
javax.servlet
javax.servlet-api
3.0.1
provided
dom4j
dom4j
1.6.1
junit
junit
4.12
framework1128
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
* 1.创建SAXReader对象s
* 2.创建输入对象in,使用本类获取本路径下面的beans配置文件
* 3.使用s的read方法解析beans文件后得到domc对象
* 4.使用domc得到domc的节点对象elem
* 5.使用elem获得所有的节点后得到集合list
* 6.遍历集合list
* 7.遍历时每次得到的数据都进行o.attributeValue获得class和id字符串
* 8.使用得到的class字符串生成对象ins
* 9.将id和对象ins存入Map集合
package org.capcom.listener;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class LoadListener implements ServletContextListener{
public static Map map = new HashMap<>();
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
InputStream in = null;
try {
/**
* 1.创建SAXReader对象s
* 2.创建输入对象in,使用本类获取本路径下面的beans配置文件
* 3.使用s的read方法解析beans文件后得到domc对象
* 4.使用domc得到domc的节点对象elem
* 5.使用elem获得所有的节点后得到集合list
* 6.遍历集合list
* 7.遍历时每次得到的数据都进行o.attributeValue获得class和id字符串
* 8.使用得到的class字符串生成对象ins
* 9.将id和对象ins存入Map集合
* */
SAXReader reader = new SAXReader();
in = LoadListener.class.getClassLoader().getResourceAsStream("beans.xml");
//use two obj excute function
Document document = reader.read(in);
Element element = document.getRootElement();
List list = element.elements("bean");
for (Element o : list){
String aClass = o.attributeValue("class");
String id = o.attributeValue("id");
Object instance = Class.forName(aClass).newInstance();
map.put(id,instance);
}
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
package org.capcom.servlet;
import org.capcom.annotation.RequstMapping;
import org.capcom.listener.LoadListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;
public class DispatcherServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {
rp.setContentType("test/html;charset=utf-8");
String uri = rq.getRequestURI();
System.out.println("输入的uri"+uri);
String[] split = uri.split("/");
String s1 ="/" + split[1];
String s2 ="/" + split[2];
System.out.println("第一段"+s1);
System.out.println("第er段"+s2);
Object o = null;
Object controObj = null;
//得到另一个类的字段就是LoadListener的map字段
Set> entryListener = LoadListener.map.entrySet();
for(Map.Entry e : entryListener){
//得到map里面的每个数据的值和注解
//将在map中获取的值赋值给o
o = e.getValue();
RequstMapping annotation = o.getClass().getAnnotation(RequstMapping.class);
if(annotation != null){//为注解中的值自动添加/
//获得注解中的值,如@RequestMapping("/user")中的值是/user
String value = annotation.value();
value = value.startsWith("/")?value:"/"+value;
if(s1.equals(value)){
controObj = o;
//匹配到了请求后就停止
break;
}
}
}
if(controObj == null){
rp.setStatus(404);
rp.getWriter().write("没找到"+s1+"对应的controller类");
}
Method method = null;
//获得contoObj里面所有的方法,使用数组接收
Method[] methods = controObj.getClass().getDeclaredMethods();
//Method是数据类型,就是方法的数据类型
for(Method m :methods){
RequstMapping mapping = m.getAnnotation(RequstMapping.class);
if(mapping != null){
String value = mapping.value();
value = value.startsWith("/") ? value : "/" + value; //三目运算符
if(value.equals(s2)){
method = m;
break;
}
}
}
if(method == null){
rp.setStatus(404);
rp.getWriter().write("404:not find " + s2 + " 的Controller对象的方法");
}
try {
Object o1 = method.invoke(controObj);
String s = (String)o1;
rq.getRequestDispatcher(s).forward(rq,rp);
}catch (Exception e){
e.printStackTrace();
}
}
}
framework
org.capcom.listener.LoadListener
DispatcherServlet
org.capcom.servlet.DispatcherServlet
DispatcherServlet
*.do
index.html
index.jsp
package org.capcom.controller;
import org.capcom.annotation.Controller;
import org.capcom.annotation.RequstMapping;
@Controller
@RequstMapping("/user")
public class UserController {
@RequstMapping("/index.do")
public String index(){
System.out.println("UserController的index方法");
return "/index.jsp";
}
@RequstMapping("/test.do")
public String test(){
System.out.println("UserController的test方法");
return "/test.jsp";
}
}