SpringMVC相关bean(表现层bean)Spring控制的bean
- 业务bean(Service)
- 功能bean(DataSource等)
方式一:Spring加载的bean设定扫描范围为上层包,排除掉controller包内的bean
@ComponentScan类型:类注解
@Configuration@ComponentScan(value = "https://www.huyubaike.com/biancheng/com.itheim",excludeFilters = @ComponentScan.Filter()type = FilterType.ANNOTATION,classes = Controller.class))public class SpringConfig{}
属性:excludeFilters:排除扫描路径中加载的bean,需要指定类别(type)与具体项(classes)includeFilters:加载指定的bean,需要指定类别(type)与具体项(classes)方式二:SPring加载的bean设定扫描范围为精准范围 , 例如service包、dao包等方式三:不区分Spring与SpringMVC的环境,加载到同一个环境中
bean加载格式
public class ServletContainersInitConfig extends AbstractDsipatcherServletInitializer {protected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationCOntext();ctx.register(SpringMvcConfig.class);return ctx;}protected WebApplicationContext createRootApplicationContext() {AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationCOntext();ctx.register(SpringConfig.class);return ctx;}protected String[] getServletMappings() {return new String[]{"/"};}}
而Spring其实还为我们准备更加简便的配置方式public class ServletContainersInitConfig extends AbstractAnnotationConfigOispatcherServletInitializer {protected Class<?>[] getRootConfigCLasses() {return new Class[]{SpringConfig.class};}protected Class<?>[] getServletConfigClasses () {return new Class[]{SpringMvcConfig.class};}protected String[] getServletMappings() {return new String[]{"/"};}}
1.3、PostMan简介为了后面学习方便 , 将学习一款模拟前端发送请求的插件文章插图
Postman是一款功能强大的网页测试与发送网页HTTP请求的Chrome插件,常用于进行接口测试 。特征:简单、实用、美观、大方官网:https://www.postman.com/downloads/
Postman基本使用
注册登录
文章插图
创建工作空间/进入工作空间
文章插图
文章插图
发送请求/测试结果
文章插图
记得开启服务器地址备份的功能 快捷键:"Ctrl + S"
文章插图
创建好后点右边的Send就能重复使用了 , 不同的项目也可以在这里配置方便测试 。
文章插图
GET请求参数
文章插图
POST请求参数
文章插图
1.4、SpringMVC解决Post请求中文乱码问题为web容器添加过滤器并指定字符集 , Spring-web包中提供了专用 的字符过滤器
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherSerletInitializer {//配字符编码过滤器protected Fillter[] getServletFillters() {CharacterEncodingFIlter filter = new CharacterEncodingFilter();filter.setEncoding("utf-8")return new Filter[]{filter};}}
二、请求与响应2.1、请求映射路径团队多人开发,每人设置不同的请求路径 , 冲突问题常常需要设置模块名作为请求路径前缀 。比如当项目中出现了两个save请求路径,员工A开发的是book模块,路径就变成"/book/save";员工B开发的是user模块 , 路径就变成"/user/save" 。之前学的@RequestMapping这个注解它能够设置当前控制器方法请求访问路径,如果设置在类上则统一设置当前控制器方法请求路径前缀 。
示例
@Controller@RequestMapping("/user")public class UserCOntroller {@RequestMapping("/save")@ResponseBodypublic String save(){System.out.println("user save ...");retrun "{'modeule':'user save'}";}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- C# 6.0 添加和增强的功能【基础篇】
- 阴阳师炼狱胧车阵容怎么搭配
- 原神片剂深研第二关怎么通关
- 电饭煲蛋糕的做法是什么_电饭煲蛋糕做法
- 支付宝使用方法(支付宝怎么绑银行卡)
- 四十六 SpringCloud微服务实战——搭建企业级开发框架:【移动开发】整合uni-app搭建移动端快速开发框架-环境搭建
- 二 『现学现忘』Git分支 — 41、分支基本操作
- 支付宝小鸡今日答题
- iphone13有没有无线充电_iphone13支持无线充电吗
- 原神杯中遥吟活动怎么玩