北京市住房与建设厅官方网站,WordPress表白墙主题,做网站安全联盟解,哈尔滨快速网站排名#x1f40c;个人主页#xff1a; #x1f40c; 叶落闲庭 #x1f4a8;我的专栏#xff1a;#x1f4a8; c语言 数据结构 javaweb 石可破也#xff0c;而不可夺坚#xff1b;丹可磨也#xff0c;而不可夺赤。 Spring MVC入门 一、Spring MVC概述二、入门案例2.1导入Sp… 个人主页 叶落闲庭 我的专栏 c语言 数据结构 javaweb 石可破也而不可夺坚丹可磨也而不可夺赤。 Spring MVC入门 一、Spring MVC概述二、入门案例2.1导入Spring MVC坐标与Servlet坐标2.2创建Spring MVC控制器类等同于Servlet功能2.3初始化Spring MVC环境同Spring环境2.4初始化Servlet容器加载Spring MVC环境 三、入门案例工作流程四、Controller加载控制4.1Controller加载控制与业务bean加载控制4.2bean的加载格式 总结 一、Spring MVC概述 web程序工作流程web程序通过浏览器访问页面前端页面使用异步提交的方式发送请求到后端服务器后端服务器采用表现层、业务层、数据层的三层架构的形式进行开发页面发送的请求由表现层接收获取用户的请求参数号将参数传递到业务层再由业务层访问数据层得到用户需要访问的数据后将数据返回给表现层表现层拿到数据后将数据转换成json格式发送给前端页面前端页面接收数据后解析数据并组织成用户浏览的最终页面信息交给浏览器。 Spring MVC是一种基于Java实现的MVC模型的轻量级Web框架。优点 使用简单开发便捷相比于Servlet 灵活性强
二、入门案例
2.1导入Spring MVC坐标与Servlet坐标
dependencies!--导入springmvc与servlet坐标--dependencygroupIdjavax.servlet/groupIdartifactIdjavax.servlet-api/artifactIdversion3.1.0/versionscopeprovided/scope/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion5.2.10.RELEASE/version/dependency/dependencies2.2创建Spring MVC控制器类等同于Servlet功能
//2.定义Controller使用Controller定义bean
Controller
public class UserController {//2.1设置当前操作的访问路径RequestMapping(/save)//2.2设置当前操作的返回值类型ResponseBodypublic String save() {System.out.println(user save...);return {module:springmvc};}
}2.3初始化Spring MVC环境同Spring环境
设定Spring MVC加载对应的bean
//3.创建Springmvc配置文件加载controller对应的bean
Configuration
ComponentScan(com.practice.controller)
public class SpringMvcConfig {
}2.4初始化Servlet容器加载Spring MVC环境
设置Spring MVC技术处理的请求
package com.practice.config;import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;/*** Author YJ* Date 2023/8/3 11:01* Description:定义一个Servlet容器启动的配置类在里面加载spring配置*/
public class ServletContainsInitConfig extends AbstractDispatcherServletInitializer {/*** 加载springmvc容器配置** return*/Overrideprotected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext ctx new AnnotationConfigWebApplicationContext();ctx.register(SpringMvcConfig.class);return ctx;}/*** 设置哪些请求归属springMVC处理** return*/Overrideprotected String[] getServletMappings() {return new String[]{/};}/*** 加载spring容器配置** return*/Overrideprotected WebApplicationContext createRootApplicationContext() {return null;}
}AbstractDispatcherServletInitializer类是SpringMVC提供的快速初始化Web3.0容器的抽象类AbstractDispatcherServletInitializer提供三个接口方法供用户实现 createServletApplicationContext()方法创建Servlet容器时加载SpringMVC对应的bean并放入 WebApplicationContext对象范围中而WebApplicationContext的作用范围为ServletContext容器范围
protected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext ctx new AnnotationConfigWebApplicationContext();ctx.register(SpringMvcConfig.class);return ctx;}getServletMappings()方法设定SpringMVC对应的请求映射路径设置为“/”表示拦截所有请求任意请求都将转入到SpringMVC进行处理
protected String[] getServletMappings() {return new String[]{/};}三、入门案例工作流程
1.服务器启动执行ServletContainsInitConfig类初始化web容器
public class ServletContainsInitConfig extends AbstractDispatcherServletInitializer2.执行createServletApplicationContext方法创建了WebApplicationContext对象
protected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext ctx new AnnotationConfigWebApplicationContext();ctx.register(SpringMvcConfig.class);return ctx;}3.加载SpringMvcConfig
Configuration
ComponentScan(com.practice.controller)
public class SpringMvcConfig {
}4.执行ComponentScan加载对应的bean
ComponentScan(com.practice.controller)5.加载UserController每个RequestMapping的名称对应一个具体的方法
Controller
public class UserController {//2.1设置当前操作的访问路径RequestMapping(/save)//2.2设置当前操作的返回值类型ResponseBodypublic String save() {System.out.println(user save...);return {module:springmvc};}
}6.执行getServletMappings方法定义所有的请求都通过SpringMVC
Overrideprotected String[] getServletMappings() {return new String[]{/};}四、Controller加载控制
4.1Controller加载控制与业务bean加载控制 SpringMVC相关bean表现层bean Spring控制的bean 业务beanService 功能beaDataSource等 SpringMVC相关bean加载控制 SpringMVC加载的bean对应的包均在com.practice.controller包内 Spring相关bean加载控制 方式一Spring加载的bean设定扫描范围为com.practice排除controller包内的bean 方式二Spring加载的bean设定扫描范围为精准范围例如service包、dao包等 方式三不区分Spring和SpringMVC环境加载到同一环境中。 名称ComponentScan 类型类注解 范例
ComponentScan(value com.practice,excludeFilters ComponentScan.Filter(type FilterType.ANNOTATION,classes Controller.class
)
)
public class SpringConfig {
}属性 excludeFilters排除扫描路径中加载的bean需要指定类别type与具体项classes includeFilters加载指定的bean需要制定类别type与具体项classes
4.2bean的加载格式
package com.practice.config;import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;/*** Author YJ* Date 2023/8/3 11:01* Description:定义一个Servlet容器启动的配置类在里面加载spring配置*/
public class ServletContainsInitConfig extends AbstractDispatcherServletInitializer {/*** 加载springmvc容器配置** return*/Overrideprotected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext ctx new AnnotationConfigWebApplicationContext();ctx.register(SpringMvcConfig.class);return ctx;}/*** 设置哪些请求归属springMVC处理** return*/Overrideprotected String[] getServletMappings() {return new String[]{/};}/*** 加载spring容器配置** return*/Overrideprotected WebApplicationContext createRootApplicationContext() {AnnotationConfigWebApplicationContext ctx new AnnotationConfigWebApplicationContext();ctx.register(SpringConfig.class);return ctx;}
}
简化开发
public class ServletContainsInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {Overrideprotected Class?[] getRootConfigClasses() {return new Class[]{SpringConfig.class};}Overrideprotected Class?[] getServletConfigClasses() {return new Class[]{SpringMvcConfig.class};}Overrideprotected String[] getServletMappings() {return new String[]{/};}总结 本文介绍了SpringMVC的概念及如何创建SpringMVC的项目。 欢迎各位小伙伴点赞关注