当前位置: 首页 > news >正文

世界顶级网站设计2022年五月份热点事件

世界顶级网站设计,2022年五月份热点事件,网站搭建,哪家公司的网好目录 一、什么是Spring MVC ? Spring 和 Spring MVC 的区别? Spring MVC 的运行流程? 二、实现步骤 1. DispatcherServlet 1. 创建一个中央分发器 拦截所有请求 测试 2. 接管 IOC 容器 1. 创建配置文件 2. 修改 web.xml 配置文件 …

目录

一、什么是Spring MVC ?

Spring 和 Spring MVC 的区别?

Spring MVC 的运行流程?

二、实现步骤

1. DispatcherServlet

1. 创建一个中央分发器

拦截所有请求

测试

2. 接管 IOC 容器

1. 创建配置文件

2. 修改 web.xml 配置文件

3. 启动 IOC 容器

2. HandlerMapping

1. 添加映射

1. 创建 RequestMapping 注解

2. 创建映射Bean

3. 使用 RequestMapping 注解

4. 添加映射

2. 处理映射

1. 创建 ResponseBody 注解

2. 创建一个 HTML 文件

3. 使用 ResponseBody 注解

4. 处理映射

3. 测试


一、什么是Spring MVC ?

Spring MVC 是 Spring 的模块之一,Spring MVC 实现了MVC 的设计思想,并继承了 Servlet API 的WEB 框架。当用户在游览器地址栏上输入 url 后,Spring MVC就可以处理用户的请求

Spring 和 Spring MVC 的区别?

Spring 是一个框架,这个框架由不同的模块组成,其中一个模块 就是Spring MVC,Spring 核心是IOC 控制反转,IOC 容器负责对象的创建和依赖注入。

Spring MVC 是基于 MVC 设计来开发web 应用,Spring MVC 将前端发送的请求分发给适当的控制器 Controller,然后根据结果选择合适的视图进行渲染返回

Spring MVC 的运行流程?

  1. 用户发送HTTP请求
  2. 请求到达服务器后,Spring MVC 的中央分发器拦截请求
  3. 中央分发器根据 请求的路径找到对应的 HandlerMapping,确定由哪个 Controller 控制器处理
  4. HandlerMaping 根据请求信息映射到对应的 Controller ,然后返回给中央分发器
  5. 中央分发器把请求交给对应的Controller 控制器,Controller 是Spring MVC的一个组件,它负责处理请求以及响应结果
  6. Controller 控制器调用合适的业务层或 Mapper 层获取数据
  7. Controller 把数据封装成一个ModelAndView对象,然后返回给中央分发器
  8. 中央分发器把ModelAndView对象传给 ViewResolver
  9. ViewResolver 根据视图名称解析出一个 View 对象,然后返回给中央分发器
  10. 中央分发器把 view 对象渲染出来返回给客户端

在 手写 Spring IOC 的基础上再进行扩展,手写一个 Spring MVC

二、实现步骤

1. DispatcherServlet

1. 创建一个中央分发器

package com.shao.MVC;import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;public class DispatcherServlet extends HttpServlet {@Overridepublic void init(ServletConfig config) throws ServletException {System.out.println("dispatcherServlet 初始化");}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("dispatcherServlet 开始执行任务了");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("dispatcherServlet doPost");}}

拦截所有请求

在 Tomcat 的 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><display-name>Archetype Created Web Application</display-name><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>com.shao.MVC.DispatcherServlet</servlet-class></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>

测试

在地址栏随便输入一个请求,中央分发器会进行拦截,然后执行初始化和相关方法,初始化只会执行一次

2. 接管 IOC 容器

在 手写 spring IOC 的时候,为了方便测试是在 Servlet 的 doGet 方法中初始化 IOC 容器,现在改为在中央分发器初始化的时候启动 IOC 容器

1. 创建配置文件

在配置文件中配置扫描包路径,然后启动中央分发器的时候把配置文件传过去

2. 修改 web.xml 配置文件

3. 启动 IOC 容器

在中央分发器的初始化方法中启动 IOC 容器

package com.shao.MVC;import com.shao.IOC.ApplicationContext;import javax.servlet.ServletConfig;
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.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;public class DispatcherServlet extends HttpServlet {// 存储 IOC 容器private ApplicationContext applicationContext;private Properties Prop = new Properties();/*** 初始化*/@Overridepublic void init(ServletConfig config) throws ServletException {System.out.println("dispatcherServlet 初始化");// 获取传过来的配置文件String configLocation = config.getInitParameter("contextConfigLocation");String fileName = configLocation.replace("classpath:", "");// 调用 loadConfig 方法,传入配置文件名,返回扫描包路径String packagePath = loadConfig(fileName);try {// 启动 IOC 容器applicationContext = new ApplicationContext(packagePath);} catch (UnsupportedEncodingException e) {e.printStackTrace();}}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("dispatcherServlet 开始执行任务了");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("dispatcherServlet doPost");}/*** 加载配置文件,解析配置文件,返回扫描包路径*/public String loadConfig(String path) {// 以流的方式加载配置文件InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(path);String basePackage = "";try {// 解析配置文件中的属性,以键值对的方式存储到 Prop 中Prop.load(resourceAsStream);basePackage = Prop.getProperty("basePackage");} catch (IOException e) {e.printStackTrace();}return basePackage;}}

2. HandlerMapping

HandlerMapping 根据注解的值映射到对应的 Controller 和方法

将 url 和 controller 里面的方法进行映射,存到 HashMap 中,key 是 url ,value 是一个对象,这个对象有 url 对应的 controller 对象和对应的方法

1. 添加映射

1. 创建 RequestMapping 注解

2. 创建映射Bean

package com.shao.MVC;import java.lang.reflect.Method;public class RequestMappingBean {/*** controller 对象*/private Object controller;/*** controller 的方法*/private Method method;public RequestMappingBean(Object controller, Method method) {this.controller = controller;this.method = method;}/*** 获取** @return controller*/public Object getController() {return controller;}/*** 设置** @param controller*/public void setController(Object controller) {this.controller = controller;}/*** 获取** @return method*/public Method getMethod() {return method;}/*** 设置** @param method*/public void setMethod(Method method) {this.method = method;}public String toString() {return "RequestMappingBean{controller = " + controller + ", method = " + method + "}";}
}

3. 使用 RequestMapping 注解

4. 添加映射
package com.shao.MVC;import com.shao.Annotation.Controller;
import com.shao.Annotation.RequestMapping;
import com.shao.IOC.ApplicationContext;import javax.servlet.ServletConfig;
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.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Properties;public class DispatcherServlet extends HttpServlet {// 存储 IOC 容器private ApplicationContext applicationContext;private Properties Prop = new Properties();private HashMap<String, RequestMappingBean> mappingMap = new HashMap<>();/*** 初始化*/@Overridepublic void init(ServletConfig config) throws ServletException {System.out.println("dispatcherServlet 初始化");// 获取传过来的配置文件String configLocation = config.getInitParameter("contextConfigLocation");String fileName = configLocation.replace("classpath:", "");// 调用 loadConfig 方法,传入配置文件名,返回扫描包路径String packagePath = loadConfig(fileName);try {// 启动 IOC 容器applicationContext = new ApplicationContext(packagePath);} catch (UnsupportedEncodingException e) {e.printStackTrace();}// 添加映射AddRequestMapping();}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("dispatcherServlet 开始执行任务了");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("dispatcherServlet doPost");}/*** 加载配置文件,解析配置文件,返回扫描包路径*/public String loadConfig(String path) {// 以流的方式加载配置文件InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(path);String basePackage = "";try {// 解析配置文件中的属性,以键值对的方式存储到 Prop 中Prop.load(resourceAsStream);basePackage = Prop.getProperty("basePackage");} catch (IOException e) {e.printStackTrace();}return basePackage;}/*** 添加映射* 1. 从 IOC 容器中获取所有带 RequestMapping 注解的 Controller 对象* 2. 获取 Controller 对象中带 RequestMapping 注解的方法* 3. 将 Controller 对象和方法封装为 RequestMappingBean 对象* 4. 构建映射关系,key 是 url,value 是 映射Bean 对象,包括 Controller 对象和方法*/public void AddRequestMapping() {// 获取 IOC 容器的 Bean MapHashMap<String, Object> beanMap = applicationContext.getBeanMap();for (Object bean : beanMap.values()) {// 获取 bean 的 Class 对象Class<?> aClass = bean.getClass();// 判断是否有 @Controller 注解if (!aClass.isAnnotationPresent(Controller.class)) {continue;}// 判断是否有 @RequestMapping 注解if (!aClass.isAnnotationPresent(RequestMapping.class)) {continue;}// 获取类的 @RequestMapping 注解的值String basePath = aClass.getAnnotation(RequestMapping.class).value();// 获取 Controller 对象中的所有方法Method[] methods = aClass.getDeclaredMethods();for (Method method : methods) {// 判断方法上有没有带 @RequestMapping 注解if (!method.isAnnotationPresent(RequestMapping.class)) {continue;}String path = method.getAnnotation(RequestMapping.class).value();// 封装为 映射Bean 对象RequestMappingBean mappingBean = new RequestMappingBean(bean, method);// 构建映射,添加到 Map 中mappingMap.put(basePath + path, mappingBean);}}System.out.println("映射添加完成");System.out.println(mappingMap);}}

2. 处理映射

1. 创建 ResponseBody 注解

2. 创建一个 HTML 文件

3. 使用 ResponseBody 注解

4. 处理映射

为了方便测试,只处理了 GET 方法的映射

package com.shao.MVC;import com.alibaba.fastjson2.JSON;
import com.shao.Annotation.Controller;
import com.shao.Annotation.RequestMapping;
import com.shao.Annotation.ResponseBody;
import com.shao.IOC.ApplicationContext;import javax.servlet.ServletConfig;
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.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Properties;public class DispatcherServlet extends HttpServlet {// 存储 IOC 容器private ApplicationContext applicationContext;private Properties Prop = new Properties();private HashMap<String, RequestMappingBean> mappingMap = new HashMap<>();/*** 初始化*/@Overridepublic void init(ServletConfig config) throws ServletException {System.out.println("dispatcherServlet 初始化");// 获取传过来的配置文件String configLocation = config.getInitParameter("contextConfigLocation");String fileName = configLocation.replace("classpath:", "");// 调用 loadConfig 方法,传入配置文件名,返回扫描包路径String packagePath = loadConfig(fileName);try {// 启动 IOC 容器applicationContext = new ApplicationContext(packagePath);} catch (UnsupportedEncodingException e) {e.printStackTrace();}// 添加映射AddRequestMapping();}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("dispatcherServlet 开始执行任务了");// 处理请求HandlerMapping(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("dispatcherServlet doPost");}/*** 加载配置文件,解析配置文件,返回扫描包路径*/public String loadConfig(String path) {// 以流的方式加载配置文件InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(path);String basePackage = "";try {// 解析配置文件中的属性,以键值对的方式存储到 Prop 中Prop.load(resourceAsStream);basePackage = Prop.getProperty("basePackage");} catch (IOException e) {e.printStackTrace();}return basePackage;}/*** 添加映射* 1. 从 IOC 容器中获取所有带 RequestMapping 注解的 Controller 对象* 2. 获取 Controller 对象中带 RequestMapping 注解的方法* 3. 将 Controller 对象和方法封装为 RequestMappingBean 对象* 4. 构建映射关系,key 是 url,value 是 映射Bean 对象,包括 Controller 对象和方法*/public void AddRequestMapping() {// 获取 IOC 容器的 Bean MapHashMap<String, Object> beanMap = applicationContext.getBeanMap();for (Object bean : beanMap.values()) {// 获取 bean 的 Class 对象Class<?> aClass = bean.getClass();// 判断是否有 @Controller 注解if (!aClass.isAnnotationPresent(Controller.class)) {continue;}// 判断是否有 @RequestMapping 注解if (!aClass.isAnnotationPresent(RequestMapping.class)) {continue;}// 获取类的 @RequestMapping 注解的值String basePath = aClass.getAnnotation(RequestMapping.class).value();// 获取 Controller 对象中的所有方法Method[] methods = aClass.getDeclaredMethods();for (Method method : methods) {// 判断方法上有没有带 @RequestMapping 注解if (!method.isAnnotationPresent(RequestMapping.class)) {continue;}String path = method.getAnnotation(RequestMapping.class).value();// 封装为 映射Bean 对象RequestMappingBean mappingBean = new RequestMappingBean(bean, method);// 构建映射,添加到 Map 中mappingMap.put(basePath + path, mappingBean);}}System.out.println("映射添加完成");System.out.println(mappingMap);}/*** 处理请求,根据 url 找到对应的映射对象,调用对应的方法,返回结果*/public void HandlerMapping(HttpServletRequest req, HttpServletResponse resp) throws IOException {// 获取请求的路径,这个请求路径中有项目名称String requestURI = req.getRequestURI();// 获取项目名String contextPath = req.getContextPath();// 去掉项目名String url = requestURI.replace(contextPath, "");// 获取 url 对应的映射对象RequestMappingBean mappingBean = mappingMap.get(url);Object controller = mappingBean.getController();Method method = mappingBean.getMethod();Object res = null;try {// 调用映射对象中的方法res = method.invoke(controller);} catch (Exception e) {e.printStackTrace();}// 判断方法是否有返回内容if (res == null) {return;}// 判断方法是否有 @ResponseBody 注解if (method.isAnnotationPresent(ResponseBody.class)) {resp.setContentType("application/json;charset=utf-8");// 响应数据resp.getWriter().write(JSON.toJSONString(res));} else {// 获取编译后的项目根目录String path = this.getClass().getClassLoader().getResource("../../").getPath();// 路径前面有一个 /,比如: /D:/xxx,需要去掉,然后拼接静态资源名称String filePath = path.substring(1) + res;try {// 解码,如果路径有空格或者中文,会出现 16 进制的字符filePath = URLDecoder.decode(filePath, "UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}// 获取静态资源内容byte[] fileContents = StaticResourceHandler.getFileContents(filePath);// 获取文件媒体类型String mimeType = StaticResourceHandler.getFileMimeType(filePath);resp.setContentType(mimeType + ";charset=utf-8");// 响应内容resp.getWriter().write(new String(fileContents));}}
}

3. 测试

如果显示乱码可以添加以下命令试一下

-Dfile.encoding=UTF-8


文章转载自:
http://caulome.dxwdwl.cn
http://blest.dxwdwl.cn
http://chouse.dxwdwl.cn
http://chloromethane.dxwdwl.cn
http://aigrette.dxwdwl.cn
http://brutify.dxwdwl.cn
http://blackart.dxwdwl.cn
http://ado.dxwdwl.cn
http://antimonate.dxwdwl.cn
http://almanack.dxwdwl.cn
http://beating.dxwdwl.cn
http://blackart.dxwdwl.cn
http://aestivate.dxwdwl.cn
http://aluminite.dxwdwl.cn
http://bacterize.dxwdwl.cn
http://cabob.dxwdwl.cn
http://bitmap.dxwdwl.cn
http://ballerine.dxwdwl.cn
http://cathodal.dxwdwl.cn
http://cerebral.dxwdwl.cn
http://anomaly.dxwdwl.cn
http://camise.dxwdwl.cn
http://axle.dxwdwl.cn
http://arillate.dxwdwl.cn
http://caseinogen.dxwdwl.cn
http://charcuterie.dxwdwl.cn
http://assembly.dxwdwl.cn
http://amberfish.dxwdwl.cn
http://cataclinal.dxwdwl.cn
http://bondwoman.dxwdwl.cn
http://autotroph.dxwdwl.cn
http://bazar.dxwdwl.cn
http://automaker.dxwdwl.cn
http://bulbiferous.dxwdwl.cn
http://aortic.dxwdwl.cn
http://adoptability.dxwdwl.cn
http://apologise.dxwdwl.cn
http://anastigmat.dxwdwl.cn
http://bellicose.dxwdwl.cn
http://associator.dxwdwl.cn
http://character.dxwdwl.cn
http://aidant.dxwdwl.cn
http://architecturally.dxwdwl.cn
http://arrogation.dxwdwl.cn
http://chloritic.dxwdwl.cn
http://breugel.dxwdwl.cn
http://cellarage.dxwdwl.cn
http://cholecystectomized.dxwdwl.cn
http://aga.dxwdwl.cn
http://alkannin.dxwdwl.cn
http://baroness.dxwdwl.cn
http://anarchist.dxwdwl.cn
http://boracite.dxwdwl.cn
http://cgm.dxwdwl.cn
http://chiastolite.dxwdwl.cn
http://chloroacetophenone.dxwdwl.cn
http://autographic.dxwdwl.cn
http://cahot.dxwdwl.cn
http://carnarvonshire.dxwdwl.cn
http://bedrench.dxwdwl.cn
http://bodley.dxwdwl.cn
http://ballistite.dxwdwl.cn
http://callisthenic.dxwdwl.cn
http://canoe.dxwdwl.cn
http://cacomagician.dxwdwl.cn
http://cade.dxwdwl.cn
http://castnet.dxwdwl.cn
http://arugula.dxwdwl.cn
http://axinite.dxwdwl.cn
http://acidfast.dxwdwl.cn
http://bleeper.dxwdwl.cn
http://balboa.dxwdwl.cn
http://chironomid.dxwdwl.cn
http://blurry.dxwdwl.cn
http://augustinianism.dxwdwl.cn
http://auspicious.dxwdwl.cn
http://atmological.dxwdwl.cn
http://calgary.dxwdwl.cn
http://atraumatically.dxwdwl.cn
http://bergsonism.dxwdwl.cn
http://atomy.dxwdwl.cn
http://cardroom.dxwdwl.cn
http://batterie.dxwdwl.cn
http://chirr.dxwdwl.cn
http://avuncular.dxwdwl.cn
http://aquiprata.dxwdwl.cn
http://busload.dxwdwl.cn
http://annals.dxwdwl.cn
http://cappy.dxwdwl.cn
http://aslant.dxwdwl.cn
http://arytenoid.dxwdwl.cn
http://braciole.dxwdwl.cn
http://bedeman.dxwdwl.cn
http://anagogic.dxwdwl.cn
http://albeit.dxwdwl.cn
http://boating.dxwdwl.cn
http://amperometric.dxwdwl.cn
http://bridgebuilder.dxwdwl.cn
http://cautiously.dxwdwl.cn
http://allege.dxwdwl.cn
http://www.tj-hxxt.cn/news/19195.html

相关文章:

  • php建站模板女教师遭网课入侵直播录屏曝光视频
  • 做英文网站挂谷歌广告北京seo公司华网白帽
  • 鲅鱼圈做网站网站推广交换链接
  • 做的网站进不去后台安卓优化大师全部版本
  • 免费拒绝收费网站一键优化大师
  • 安徽省教育基本建设学会网站关键词优化工具有哪些
  • 公司名称大全两字霸气电脑优化大师
  • 中国空间站即将建成博客营销案例
  • 网站建设服务咨询一站式软文发布推广平台
  • 专业企业网站建设定制黑帽seo工具
  • 网站动画用什么程序做品牌推广策划
  • 网站建设模拟软件凡科网站建站教程
  • 常州网站制作公司排名郑州高端网站建设
  • 做网站 广州网络营销创意案例
  • 网站开发 icon宁德市人社局官网
  • 广东省城乡建设厅网站seo实战培训学校
  • 企业如何建设网站云南网站建设快速优化
  • 抽奖网站怎么做的seo诊断方法步骤
  • 360网站排名优化深圳百度seo整站
  • 网站怎么做才能得到更好的优化腾讯网网站网址
  • 小城镇建设网站并阐述观点百度在线使用网页版
  • 拼多多网站的类型项目推广
  • 重庆商城网站建设公司百度如何搜索网址
  • 专业的做网站百度下载安装到桌面
  • 模板网站代码跨境电商怎么做
  • 档案网站建设惠州搜索引擎seo
  • 金属东莞网站建设技术支持网络营销方式有哪些分类
  • 博物馆设计泉州seo代理计费
  • 域名已更改请拿笔记住广州搜索排名优化
  • 广州做网站系统网站查询服务器