spring— Spring与Web环境集成

ApplicationContext应用上下文获取方式

应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件) 方式获取的,但是每次从容器中获 得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件) ,这样的弊端是配置文件加载多次, 应用上下文对象创建多次。

在Web项目中,可以使用ServletContextListener监听Web应用的启动,我们可以在Web应用启动时,就加载 Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,这样 就可以在任意位置从域中获得应用上下文ApplicationContext对象了。

Spring提供获取应用上下文的工具

Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内 部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具 WebApplicationContextUtils供使用者获得应用上下文对象。
所以我们需要做的只有两件事:
①在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>

②使用WebApplicationContextUtils获得应用上下文对象ApplicationContext

SpringMVC开发步骤

①导入SpringMVC相关坐标

        <!--SpringMVC坐标--><dependency>    <groupId>org.springframework</groupId>  <artifactId>spring-webmvc</artifactId><version>5.0.5.RELEASE</version></dependency> <!--Servlet坐标--> <dependency>  <groupId>javax.servlet</groupId>  <artifactId>servlet-api</artifactId>  <version>2.5</version> </dependency><!--Jsp坐标--><dependency>   <groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId>  <version>2.0</version></dependency>

②配置SpringMVC核心控制器DispathcerServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0">
<!--    <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>--><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>

③创建Controller类和视图页面
④使用注解配置Controller类中业务方法的映射地址


@Controller("controller")
public class QuickController {public QuickController() {System.out.println("aa");}@RequestMapping("/qq")public  String method(){System.out.println("controller");return "success.jsp";}
}

⑤配置SpringMVC核心文件 spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
"><context:component-scan base-package="com.controller" /><mvc:annotation-driven />
</beans>

⑥客户端发起请求测试

SpringMVC的执行流程

在这里插入图片描述
①用户发送请求至前端控制器DispatcherServlet。 ②DispatcherServlet收到请求调用HandlerMapping处理器映射器。
③处理器映射器找到具体的处理器(可以根据xml配置、注解进行查找),生成处理器对象及处理器拦截器(如果有则 生成)一并返回给DispatcherServlet。
④DispatcherServlet调用HandlerAdapter处理器适配器。
⑤HandlerAdapter经过适配调用具体的处理器(Controller,也叫后端控制器)。
⑥Controller执行完成返回ModelAndView。
⑦HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet。
⑧DispatcherServlet将ModelAndView传给ViewReslover视图解析器。
⑨ViewReslover解析后返回具体View。
⑩DispatcherServlet根据View进行渲染视图(即将模型数据填充至视图中)。DispatcherServlet响应用户

SpringMVC组件解析

  1. 前端控制器:DispatcherServlet
    用户请求到达前端控制器,它就相当于 MVC 模式中的 C,DispatcherServlet 是整个流程控制的中心,由它调用其它组件处理用户的请求,DispatcherServlet 的存在降低了组件之间的耦合性。
  2. 处理器映射器:HandlerMapping
    HandlerMapping 负责根据用户请求找到 Handler 即处理器,SpringMVC 提供了不同的映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等。
  3. 处理器适配器:HandlerAdapter
    通过 HandlerAdapter 对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理器进行执行。
  4. 处理器:Handler
    它就是我们开发中要编写的具体业务控制器。由 DispatcherServlet 把用户请求转发到 Handler。由 Handler 对具体的用户请求进行处理。
  5. 视图解析器:View Resolver View Resolver
    负责将处理结果生成 View 视图,View Resolver 首先根据逻辑视图名解析成物理视图名,即具体 的页面地址,再生成 View 视图对象,最后对 View 进行渲染将处理结果通过页面展示给用户。
  6. 视图:View
    SpringMVC 框架提供了很多的 View 视图类型的支持,包括:jstlView、freemarkerView、pdfView等。最常用的 视图就是 jsp。一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据 业务需求开发具体的页面

3.3 SpringMVC注解解析

@RequestMapping

作用:用于建立请求 URL 和处理请求方法之间的对应关系

位置:

​ 类上,请求URL 的第一级访问目录。此处不写的话,就相当于应用的根目录

​ 方法上,请求 URL 的第二级访问目录,与类上的使用@ReqquestMapping标注的一级目录一起组成访问虚拟路径

属性:

​ value:用于指定请求的URL。它和path属性的作用是一样的

​ method:用于指定请求的方式

​ params:用于指定限制请求参数的条件。它支持简单的表达式。要求请求参数的key和value必须和配置的一模一样

例如:

​ params = {“accountName”},表示请求参数必须有accountName

​ params = {“moeny!100”},表示请求参数中money不能是10

@Controller("controller")
public class QuickController {public QuickController() {System.out.println("aa");}@RequestMapping(value = {"/qq"},method = {RequestMethod.GET},params = {"name"})public  String method(){System.out.println("controller");return "success.jsp";}
}

3.4 SpringMVC的XML配置解析

SpringMVC有默认组件配置,默认组件都是DispatcherServlet.properties配置文件中配置的,该配置文件地址org/springframework/web/servlet/DispatcherServlet.properties,该文件中配置了默认的视图解析器,如下:

org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver

翻看该解析器源码,可以看到该解析器的默认设置,如下:

REDIRECT_URL_PREFIX = "redirect:"  --重定向前缀
FORWARD_URL_PREFIX = "forward:"    --转发前缀(默认值)
prefix = "";     --视图名称前缀
suffix = "";     --视图名称后缀
  1. 视图解析器

我们可以通过属性注入的方式修改视图的的前后缀

<!--配置内部资源视图解析器-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
"><context:component-scan base-package="com.controller" /><mvc:annotation-driven /><bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="suffix" value=".jsp"/><property name="prefix" value="/WEB-INF/"/></bean>
</beans>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391895.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Elasticsearch集群知识笔记

Elasticsearch集群知识笔记 Elasticsearch内部提供了一个rest接口用于查看集群内部的健康状况&#xff1a; curl -XGET http://localhost:9200/_cluster/healthresponse结果&#xff1a; {"cluster_name": "format-es","status": "green&qu…

Item 14 In public classes, use accessor methods, not public fields

在public类中使用访问方法&#xff0c;而非公有域 这标题看起来真晦涩。。解释一下就是&#xff0c;如果类变成public的了--->那就使用getter和setter&#xff0c;不要用public成员。 要注意它的前提&#xff0c;如果是private的class&#xff08;内部类..&#xff09;或者p…

子集和与一个整数相等算法_背包问题的一个变体:如何解决Java中的分区相等子集和问题...

子集和与一个整数相等算法by Fabian Terh由Fabian Terh Previously, I wrote about solving the Knapsack Problem (KP) with dynamic programming. You can read about it here.之前&#xff0c;我写过有关使用动态编程解决背包问题(KP)的文章。 你可以在这里阅读 。 Today …

matplotlib图表介绍

Matplotlib 是一个python 的绘图库&#xff0c;主要用于生成2D图表。 常用到的是matplotlib中的pyplot&#xff0c;导入方式import matplotlib.pyplot as plt 一、显示图表的模式 1.plt.show() 该方式每次都需要手动show()才能显示图表&#xff0c;由于pycharm不支持魔法函数&a…

到2025年将保持不变的热门流行技术

重点 (Top highlight)I spent a good amount of time interviewing SMEs, data scientists, business analysts, leads & their customers, programmers, data enthusiasts and experts from various domains across the globe to identify & put together a list that…

spring—SpringMVC的请求和响应

SpringMVC的数据响应-数据响应方式 页面跳转 直接返回字符串 RequestMapping(value {"/qq"},method {RequestMethod.GET},params {"name"})public String method(){System.out.println("controller");return "success";}<bea…

Maven+eclipse快速入门

1.eclipse下载 在无外网情况下&#xff0c;无法通过eclipse自带的help-install new software输入url来获取maven插件&#xff0c;因此可以用集成了maven插件的免安装eclipse(百度一下有很多)。 2.jdk下载以及环境变量配置 JDK是向前兼容的&#xff0c;可在Eclipse上选择编译器版…

源码阅读中的收获

最近在做短视频相关的模块&#xff0c;于是在看 GPUImage 的源码。其实有一定了解的伙伴一定知道 GPUImage 是通过 addTarget 链条的形式添加每一个环节。在对于这样的设计赞叹之余&#xff0c;想到了实际开发场景下可以用到的场景&#xff0c;借此分享。 我们的项目中应该有很…

马尔科夫链蒙特卡洛_蒙特卡洛·马可夫链

马尔科夫链蒙特卡洛A Monte Carlo Markov Chain (MCMC) is a model describing a sequence of possible events where the probability of each event depends only on the state attained in the previous event. MCMC have a wide array of applications, the most common of…

PAT乙级1012

题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805311146147840 题解 就比较简单&#xff0c;判断每个数字是哪种情况&#xff0c;然后进行相应的计算即可。 下面的代码中其实数组是不必要的&#xff0c;每取一个数字就可以直接进行相应计算。 // P…

我如何在昌迪加尔大学中心组织Google Hash Code 2019

by Neeraj Negi由Neeraj Negi 我如何在昌迪加尔大学中心组织Google Hash Code 2019 (How I organized Google Hash Code 2019 at Chandigarh University Hub) This is me !!! Neeraj Negi — Google HashCode Organizer这就是我 &#xff01;&#xff01;&#xff01; Neeraj …

leetcode 665. 非递减数列(贪心算法)

给你一个长度为 n 的整数数组&#xff0c;请你判断在 最多 改变 1 个元素的情况下&#xff0c;该数组能否变成一个非递减数列。 我们是这样定义一个非递减数列的&#xff1a; 对于数组中所有的 i (0 < i < n-2)&#xff0c;总满足 nums[i] < nums[i 1]。 示例 1: …

django基于存储在前端的token用户认证

一.前提 首先是这个代码基于前后端分离的API,我们用了django的framework模块,帮助我们快速的编写restful规则的接口 前端token原理: 把(token加密后的字符串,keyname)在登入后发到客户端,以后客户端再发请求,会携带过来服务端截取(token加密后的字符串,keyname),我们再利用解密…

数据分布策略_有效数据项目的三种策略

数据分布策略Many data science projects do not go into production, why is that? There is no doubt in my mind that data science is an efficient tool with impressive performances. However, a successful data project is also about effectiveness: doing the righ…

cell 各自的高度不同的时候

1, cell 根据文字、图片等内容&#xff0c;确定自己的高度。每一个cell有自己的高度。 2&#xff0c;tableView 初始化 现实的时候&#xff0c;不是从第一个cell开始显示&#xff0c;&#xff08;从第二个&#xff1f;&#xff09;&#xff0c;非非正常显示。 a:cell 的高度问题…

leetcode 978. 最长湍流子数组(滑动窗口)

当 A 的子数组 A[i], A[i1], …, A[j] 满足下列条件时&#xff0c;我们称其为湍流子数组&#xff1a; 若 i < k < j&#xff0c;当 k 为奇数时&#xff0c; A[k] > A[k1]&#xff0c;且当 k 为偶数时&#xff0c;A[k] < A[k1]&#xff1b; 或 若 i < k < j&…

spring boot源码下载地址

github下载&#xff1a; https://github.com/spring-projects/spring-boot/tree/1.5.x git地址&#xff1a; https://github.com/spring-projects/spring-boot.git 因为项目中目前使用的就是spring boot 1.5.19版本&#xff0c;因此这里先研究spring boot 1.5版本源码.转载于:h…

java基础学习——5、HashMap实现原理

一、HashMap的数据结构 数组的特点是&#xff1a;寻址容易&#xff0c;插入和删除困难&#xff1b;而链表的特点是&#xff1a;寻址困难&#xff0c;插入和删除容易。那么我们能不能综合两者的特性&#xff0c;做出一种寻址容易&#xff0c;插入删除也容易的数据结构&#xff1…

看懂nfl定理需要什么知识_NFL球队为什么不经常通过?

看懂nfl定理需要什么知识Debunking common NFL myths in an analytical study on the true value of passing the ball在关于传球真实价值的分析研究中揭穿NFL常见神话 Background背景 Analytics are not used enough in the NFL. In a league with an abundance of money, i…

Docker初学者指南-如何创建您的第一个Docker应用程序

您是一名开发人员&#xff0c;并且想要开始使用Docker&#xff1f; 本文是为您准备的。 (You are a developer and you want to start with Docker? This article is made for you.) After a short introduction on what Docker is and why to use it, you will be able to cr…