一.配置阶段:配置web.xml (DispatcherServlet) —>设定init-param(contextConfigLocation=classpath:application.xml)—>设定url-pattern(/*)—>配置Annotation(@Controller @Service @Autowired @RequestMapping)
二.初始化阶段:调用init()方法(加载配置文件)—>IOC容器初始化(Map
三.运行阶段:调用doPost()/doGet() (Web容器调用doPost/doGet方法,获取request/response对象)—>匹配HandlerMapping (从request对象中获得用户输入的url,找到其对应的Method)—>反射调用method.invoker()(利用反射调用方法并返回结果)—>response.getWrite().write()(将返回结果输出到浏览器)
配置application.properties文件:
scanPackage=com.spring
Web Application
zjhmvc
com.spring.mvcframework.v1.servlet.ZJHDispatcherServlet
contextConfigLocation
application.properties
1
zjhmvc
/*
@ZJHService 注解:
package annotation;
import java.lang.annotation.*;
/**
* Created by Lenovo on 2019-8-10.
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ZJHService {
String value() default "";
}
@ZJHAutowired
package annotation;
import java.lang.annotation.*;
/**
* Created by Lenovo on 2019-8-10.
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ZJHAutowired {
String value() default "";
}
@ZJHController
package annotation;
import java.lang.annotation.*;
/**
* Created by Lenovo on 2019-8-10.
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ZJHController {
String value() default "";
}
@ZJHRequestMapping
package annotation;
import java.lang.annotation.*;
/**
* Created by Lenovo on 2019-8-10.
*/
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ZJHRequestMapping {
String value() default "";
}
@ZJHRequestParam
package annotation;
import java.lang.annotation.*;
/**
* Created by Lenovo on 2019-8-10.
*/
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ZJHRequestParam {
String value() default "";
}
service 接口
package service;
/**
* Created by Lenovo on 2019-8-10.
*/
public interface IDemoService {
public String get(String name);
}
service实现类
package service;
import annotation.ZJHService;
/**
* Created by Lenovo on 2019-8-10.
*/
@ZJHService
public class DemoService implements IDemoService {
public String get(String name) {
return "My name is"+name;
}
}
配置入口类DemoAction:
package action;
import annotation.ZJHAutowired;
import annotation.ZJHRequestMapping;
import annotation.ZJHRequestParam;
import service.IDemoService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Created by Lenovo on 2019-8-10.
*/
public class DemoAction {
@ZJHAutowired
private IDemoService demoService;
@ZJHRequestMapping("/query")
public void query(HttpServletRequest req,HttpServletResponse resp,@ZJHRequestParam("name") String name){
String result =demoService.get(name);
try{
resp.getWriter().write(result);
}catch (Exception e){
e.printStackTrace();
}
}
@ZJHRequestMapping("/add")
public void add(HttpServletRequest req,HttpServletResponse resp,@ZJHRequestParam("a") Integer a,@ZJHRequestParam("b") Integer b){
try{
resp.getWriter().write(a+"+"+b+"="+(a+b));
}catch (Exception e){
e.printStackTrace();
}
}
@ZJHRequestMapping("/remove")
public void remove(HttpServletRequest req,HttpServletResponse resp,@ZJHRequestParam("id") Integer id){
}
}
package action;
import annotation.ZJHAutowired;
import annotation.ZJHRequestMapping;
import annotation.ZJHRequestParam;
import service.IDemoService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Created by Lenovo on 2019-8-10.
*/
public class DemoAction {
@ZJHAutowired
private IDemoService demoService;
@ZJHRequestMapping("/query")
public void query(HttpServletRequest req,HttpServletResponse resp,@ZJHRequestParam("name") String name){
String result =demoService.get(name);
try{
resp.getWriter().write(result);
}catch (Exception e){
e.printStackTrace();
}
}
@ZJHRequestMapping("/add")
public void add(HttpServletRequest req,HttpServletResponse resp,@ZJHRequestParam("a") Integer a,@ZJHRequestParam("b") Integer b){
try{
resp.getWriter().write(a+"+"+b+"="+(a+b));
}catch (Exception e){
e.printStackTrace();
}
}
@ZJHRequestMapping("/remove")
public void remove(HttpServletRequest req,HttpServletResponse resp,@ZJHRequestParam("id") Integer id){
}
}