• SpringMVC学习笔记——1


    一、SpringMVC简介

    1.1、SpringMVC概述

    SpringMVC是一个基于Spring开发的MVC轻量级框架,Spring3.0后发布的组件,SpringMVC和Spring可以无缝整合,使用DispatcherServlet作为前端控制器,且内部提供了处理器映射器、处理器适配器、视图解析器等组件,可以简化JavaBean封装,Json转化、文件上传等操作。
    在这里插入图片描述

    1.2、SpringMVC快速入门

    在这里插入图片描述

    1. 导入spring-mvc坐标
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>5.3.7version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 配置前端控制器DispatcherServlet
    
    <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">
    
        
        <servlet>
            <servlet-name>DispatcherServletservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        servlet>
        <servlet-mapping>
            <servlet-name>DispatcherServletservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 编写Controller,配置映射路径,并交给SpringMVC容器管理
    @Controller
    public class QuickController {
        @RequestMapping("/show")
        private void show(){
            System.out.println("show running...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    1.3、Controller中访问容器中的Bean

    1. 创建Bean
    @Service
    public class QuickServiceImpl implements QuickService {
    }
    
    • 1
    • 2
    • 3
    1. 用spring-xml文件扫描包含该Bean的包
    
    <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"
           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
    ">
        
        <context:component-scan base-package="com.Smulll.Service"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. 在Web-xml文件中配置ContextLoaderListener
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 在Controller中访问容器中的Bean
    @Controller
    public class QuickController {
        /*直接注入Service进行使用*/
        @Autowired
        private QuickService quickService;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    1.4、SpringMVC关键组件的浅析

    上面已经完成的快速入门的操作,也在不知不觉中完成的Spring和SpringMVC的整合,我们只需要按照规则去定义Controller和业务方法就可以。但是在这个过程中,肯定是很多核心功能类参与到其中,这些核心功能类,一般称为组件。当请求到达服务器时,是哪个组件接收的请求,是哪个组件帮我们找到的Controller,是哪个组件帮我们调用的方法,又是哪个组件最终解析的视图?

    组件描述常用组件
    处理器映射器:HandlerMapping匹配映射路径对应的Handler,返回可执行的处理器链对象HandlerExecutionChain对象RequestMappingHandlerMapping
    处理器适配器:HandlerAdapter匹配HandlerExecutionChain对应的适配器进行处理器调用,返回视图模型对象RequestMappingHandlerAdapter
    视图解析器:ViewResolver对视图模型对象进行解析InternalResourceViewResolver

    在这里插入图片描述

    二、SpringMVC的请求处理

    2.1、请求映射路径配置

    配置映射路径,映射器处理器才能找到Controller的方法资源,目前主流映射路径配置方式就是@RequestMapping

    相关注解作用使用位置
    @RequestMapping设置控制器方法的访问资源路径,可以接收任何请求方法和类上
    @GetMapping设置控制器方法的访问资源路径,可以接收GET请求方法和类上
    @PostMapping设置控制器方法的访问资源路径,可以接收POST请求方法和类上
    • 当给类加了@RequestMapping,想要访问某个方法就必须要在虚拟地址上加入类的RequestMapping名以及方法的RequestMapping名
    @Controller
    @RequestMapping("/quick")
    public class QuickController {
        /*直接注入Service进行使用*/
        @Autowired
        private QuickService quickService;
        //使用GET方法获取数据
        //@RequestMapping(value = {"/show","/showaaa"},method = RequestMethod.GET)
        @GetMapping("/show")
        //当给类加了@RequestMapping("/quick"),想访问show方法就必须要 虚拟地址+quick/show
        private String show(){
            System.out.println("show running..."+quickService);
            return "/index.jsp";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.2、请求数据的接收

    2.2.1、键值对方式接收数据

    接收普通请求数据,当客户端提交的数据是普通键值对形式时,直接使用同名形参接收即可

    @Controller
    public class ParamController {	
    	//http://localhost:8080/SpringMVCDome1/Param4?username=zhangsan&age=12&hobby=football
        @RequestMapping("/Param4")
        public String Param4(@RequestParam Map<String,String> map){
            map.forEach((k,v)->{
                System.out.println(k+v);
            });
            return "index.jsp";
        }
    
    	//http://localhost:8080/SpringMVCDome1/Param4?hobby=pingpang&hobby=football&hobby=basketball
        @RequestMapping("/Param3")
        public String Param3(@RequestParam List<String> hobby){
            for (String s : hobby) {
                System.out.println(s);
            }
            return "index.jsp";
        }
    
    	//http://localhost:8080/SpringMVCDome1/Param4?hobby=pingpang&hobby=football&hobby=basketball
        @RequestMapping("/Param2")
        public String Param2(String[] hobby){
            for (String s : hobby) {
                System.out.println(s);
            }
            return "index.jsp";
        }
    
    	//http://localhost:8080/SpringMVCDome1/Param4?username=zhangsan&age=23
        @RequestMapping("/Param1")
        public String Param1(@RequestParam("username") String name, int age){
            System.out.println(name+age);
            return "index.jsp";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • @RequestParam可以使获取的数据不创建对象而是直接注入形参,因此使用该注解可以使得参数不一样而注入
      • @RequestParam有三个属性
      • value:要注入参数的名字
      • required:该数据是否必须注入
        • true:是
        • false:否(默认)
      • defaultValue:当数据未提交时的默认值

    2.2.2、封装JavaBean数据

    接收实体JavaBean属性数据,单个JavaBean数据:提交的参数名称只要与Java的属性名一致,就可以进行自动封装

    username=haohao&age=35&hobbies=eat&hobbies=sleep
    
    • 1
    	public class User{
    	private string username ;
    	private Integer age;
    	private String [] hobbies;
    	private Date birthday;
    	private Address address;
    	// ...省略get和set方法
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    @GetMapping("/show")
    pubiic string show(User user){
    	system.out.println(user);
    	return "/index.jsp" ;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.2.3、接收JSON格式数据

    • 使用POST请求方式,添加@RequestBody可以使json格式字符串整体打印出来,不会被解析
    @PostMapping("/Param5")
    public String Param5(@RequestBody String body) {
        System.out.println(body);
        return "index.jsp";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用Json工具 ( jackson )将Json格式的字符串转化为JavaBean进行操作

    <dependency>
        <groupId>com.fasterxml.jackson.coregroupId>
        <artifactId>jackson-databindartifactId>
        <version>2.9.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    @PostMapping ("/show" )
    public string show(@RequestBody string body) throws IOException {
    	system.out.println(body);
    	//获得objectMapper
    	ObjectMapper objectMapper = new ObjectMapper();
    	//将json格式字符串转化成指定的User
    	User user = objectMapper.readValue(body,User.class);
    	System.out.println(user);
    	return "/index.jsp";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 配置HandlerAdapter可以自动执行转换的工作
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">bean>
            list>
        property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    @PostMapping ("/show" )
    public string show(@RequestBody User user) throws IOException {
    	System.out.println(user);
    	return "/index.jsp";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.2.4、接收Restful风格数据

    什么是Rest风格?
    Rest (Representational State Transfer)表象化状态转变(表述性状态转变),在2000年被提出,基于HTTP、URI
    、xml、JSON等标准和协议,支持轻量级、跨平台、跨语言的架构设计。是Web服务的一种新网络应用程序的设计风格和开发方式。

    Restful风格的请求,常见的规则有如下三点:
    用URI表示某个模块资源,资源名称为名词;

    模板URI资源
    用户模块userhttp://localhost/user
    商品模块producthttp://localhost/product
    账户模块accounthttp://localhost/account
    日志模块loghttp://localhost/log

    用请求方式表示模块具体业务动作,例如:GET表示查询、POST表示插入、PUT表示更新、DELETE表示删除

    URI资源请求方式参数解释
    http://localhost/user/100GET存在URL地址中:100查询id=100的User数据
    http://localhost/userPOST存在请求体中Json: {“username”:“haohao” ,“age”:18}插入user数据
    http://localhost/userPUT存在请求体中Json : {“id”:100,“username”:“haohao” , “age”:18}修改id=100的User数据
    http://localhost/user/100DELETE存在URL地址中:100删除id=100的User数据
    http://localhost/product/5GET存在URL地址中:5查询id=5的Product数据
    http://localhost/productPOST存在请求体中Json: {“proName”:“小米手机” ,“price”:1299}插入Product数据
    http://localhost/productPUT存在请求体中Json : {“id”:5,“proName”:“小米手机” ,“price”:1299}修改id=5的Product数据
    http://localhost/product/5DELETE存在URL地址中:5删除id=5的Product数据
    • 用HTTP响应状态码表示结果,国内常用的响应包括三部分:状态码、状态信息、响应数据
    {
    	"code":200,
    	"message":"成功",
    	"data":{
    	"username":"haohao",
    	"age":18
    	}
    }
    {
    	"code":300,
    	"message":"执行错误",
    	"data":"",
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    接收Restful风格数据,Restful请求数据一般会在URL地址上携带,可以使用注解@PathVariable(占位符参数
    名称)

    http://localhost/user/100
    
    • 1
    @PostMapping ("/user/{id}")
    public string findUserById(@Pathvariable("id") Integer id){
    	system.out.println(id);
    	return "/index.jsp";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    请求URL资源地址包含多个参数情况

    http://localhost/user/haohao/18
    
    • 1
    @PostMapping ("/user/{username}/{age}")
    public string findUserById(@Pathvariable("username") String username,@Pathvariable("age") Integer age){
    	system.out.println(username+"=="+age);
    	return "/index.jsp";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.2.5、接收上传的文件

    接收文件上传的数据,文件上传的表单需要一定的要求,如下:

    • 表单的提交方式必须是POST
    • 表单的enctype属性必须是multipart/form-data
    • 文件上传项需要有name属性
    <form action="" enctype="multipart/form-data" method="post">
    	<input type="file" name="myFile">
    form>
    
    • 1
    • 2
    • 3
    http://localhost:8080/SpringMVCDome1/Param6
    
    • 1
    @PostMapping("/Param6")
    public String Param6(@RequestBody MultipartFile myFile) {
        System.out.println(myFile);
        return "/index.jsp";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    服务器端,由于映射器适配器需要文件上传解析器,而该解析器默认未被注册,所以手动注册

    
    <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    	<property name="defaultEncoding" value="UTF-8"/>
    	<property name="maxUploadSizePerFile" value="1048576"/>
    	<property name="maxUploadSize" value="3145728"/>
    	<property name="maxInMemorySize" value="1048576"/>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    而CommonsMultipartResolver底层使用的Apache的是Common-fileuplad等工具API进行的文件上传

    <dependency>
    	<groupId>commons-fileuploadgroupId>
    	<artifactId>commons-fileuploadartifactId>
    	<version>1.4version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    @PostMapping("/Param6")
    public String Param6(@RequestBody MultipartFile myFile) throws IOException {
        System.out.println(myFile);
        //将上传的文件进行保存
        //1、获取当前上传的文件的输入流
        InputStream inputStream = myFile.getInputStream();
        //2、获得上传文件位置的输出流
        OutputStream outputStream = new FileOutputStream("D:\\"+"aaa");
        //3、执行文件拷贝
        IOUtils.copy(inputStream,outputStream);
        outputStream.close();
        inputStream.close();
    
        return "/index.jsp";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.2.6、获取header头信息和cookie信息

    接收Http请求头数据,接收指定名称的请求头

    GetMapping ("/headers")
    public string headers(@RequestHeader("Accept-Encoding") String acceptEncoding){
    	System.out.println ( "Accept-Encoding:"+acceptEncoding);
    	return "/index.jsp";
    
    • 1
    • 2
    • 3
    • 4

    接收所有的请求头信息

    @GetMapping ( "/ headersMap" )
    public string headersMap(@RequestHeader Map<String,string> map){
    	map.forEach((k, v)->{
    		system.out.println(k+":"+V);
    	});
    	return " /index.jsp" ;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    获得客户端携带的Cookie数据

    GetMapping("/cookies")
    public String cookies(@CookieValue(value = "JSESSIONID",defaultValue = "") String jsessionid){
    	system.out.println(jsessionid);
    	return "/index.jsp" ;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.2.7、直接获取Requset和Session域中的数据

    获得转发Request域中数据,在进行资源之间转发时,有时需要将一些参数存储到request域中携带给下一个资源

    GetMapping ("/request1")
    public string request1(HttpServletRequest request) {
    	//存储数据
    	request.setAttribute("username","haohao");
    	return "/request2";
    }
    GetMapping ("/request2")
    public string request2(@RequestAttribute("username") String username) {
    	system.out.println(username);
    	return "/index.jsp";
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.3、Javaweb常用对象获取

    直接打印参数request和response,可以发现这两个参数是SpringMVC自动传入的

    @GetMapping("/Param10")
    public String Param10(HttpServletRequest request, HttpServletResponse response) {
        System.out.println(request);
        System.out.println(response);
        return "/index.jsp";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    2.4、请求静态资源

    在Spring-MVC框架下,无法直接访问到web文件下的静态资源

    静态资源请求的三种解决方案:

    • 第一种方案,可以再次激活Tomcat的DefaultServlet,Servlet的url-pattern的匹配优先级是:精确匹配>目录匹配>扩展名匹配>缺省匹配,所以可以指定某个目录下或某个扩展名的资源使用DefaultServlet进行解析:
    <servlet-mapping>
    	<servlet-name>defaultservlet-name>
    	<url-pattern>*.htmlurl-pattern>
    servlet-mapping>
    <servlet-mapping>
    	<servlet-name>defaultservlet-name>
    	<url-pattern>/img/*url-pattern>
    servlet-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 第二种方式,在spring-mvc.xml中去配置静态资源映射,匹配映射路径的请求到指定的位置去匹配资源
    
    <mvc:resources mapping="/img/*" location="/img/" />
    <mvc:resources mapping="/css/*" location="/css/" />
    <mvc:resources mapping="/js/*" location="/js/" />
    <mvc:resources mapping="/html/*" location="/html/" />
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 第三种方式,在spring-mvc.xml中去配置,该方式是注册了一个
      DefaultServletHttpRequestHandler处理器,静态资源的访问都由该处理器去处理,这也是开发中使用最多的
    <mvc:default-servlet-handler/>
    
    • 1

    2.5、注解驱动标签

    该标签内部会帮我们注册RequestMappingHandlerMapping、注册RequestMappingHandlerAdapter并注入Json消息转换器等,上述配置就可以简化成如下:

    
    <mvc:annotation-driven/>
    
    <mvc:default-servlet-handler/>
    
    • 1
    • 2
    • 3
    • 4

    PS: 标签在不同的版本中,帮我们注册的组件不同,Spring 3.0.X版本注册是
    DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter,由于框架的发展,从Spring 3.1.X开始注册组件变为RequestMappingHandlerMappingRequestMappingHandlerAdapter.

    三、SpringMVC的响应处理

    3.1、传统同步业务数据响应

    Spring的接收请求的部分我们讲完了,下面在看一下Spring怎么给客户端响应数据,响应数据主要分为两大部分:

    • 传统同步方式:准备好模型数据,在跳转到执行页面进行展示,此方式使用越来越少了,基于历史原因,一些旧项目还在使用;
    • 前后端分离异步方式:前端使用Ajax技术+Restful风格与服务端进行Json格式为主的数据交互,目前市场上几乎都是此种方式了。

    传统同步业务数据响应

    • 请求资源转发
    • 请求资源重定向
    • 响应模型数据
    • 直接回写数据给客户端

    在这里插入图片描述

    • 转发的关键字forword
    • 重定向的关键字redirect

    通过视图模型的方式进行转发数据

    @Controller
    public class QuickController1 {
        @GetMapping("/res1")
        //当给类加了@RequestMapping("/quick"),想访问show方法就必须要 虚拟地址+quick/show
        private ModelAndView show(ModelAndView modelAndView){
            User user = new User();
            user.setUsername("zhangsan");
            user.setAge(18);
            user.setGender('男');
    
            modelAndView.addObject("user",user);
            modelAndView.setViewName("/index.jsp");
            return modelAndView;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    直接回写字符串

    需要使用Tomcat8

    @RequestMapping("/res2")
    @ResponseBody//告诉SpringMVC返回的字符串不是视图名,是以响应体方式响应的数据
    public String res2(){
        return "hello,world";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.2、前后端分离异步业务数据响应

    其实此处的回写数据,跟上面回写数据给客户端的语法方式一样,只不过有如下一些区别:

    • 同步方式回写数据,是将数据响应给浏览器进行页面展示的,而异步方式回写数据一般是回写给Ajax引擎的,即谁访问服务器端,服务器端就将数据响应给谁
    • 同步方式回写的数据,一般就是一些无特定格式的字符串,而异步方式回写的数据大多是Json格式字符串

    回写普通数据使用@ResponseBody标注方法,直接返回字符串即可,此处不在说明;
    回写Json格式的字符串,即将直接拼接Json格式的字符串或使用工具将JavaBean转换成Json格式的字符串回写

    @GetMapping("/response3")
    @ResponseBody
    public string response3(HttpServletResponse response){
    	return "{\"username\":\"haohaol\",\"age\":18}";
    }
    @GetMapping("/response4")
    @ResponseBody
    public string response4() throws JsonProcessingException {
    	//创建JavaBean
    	User user = new User();
    	user.setUsername("haohao");
    	user.setAge(18);
    	//使用Jackson转换成json格式的字符串
    	String json = new objectMapper().writevalueAsString(user);
    	return json;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    使用@RestController 相当于@ResponseBody和@Controller结合

  • 相关阅读:
    [c语言]深入返回值为函数指针的函数
    vue3第二十三节(全局属性方法应用)
    鞋店小程序商城开发指南
    AIGC之文本内容生成概述(下)—— GPT
    PreMaint CMS系统:数字化驱动起重机远程管理的智能未来
    Android studio 升级常见报错
    一文搞懂│php 中的 DI 依赖注入
    借助ChatGPT的神奇力量,解锁AI无限可能!
    制造企业MES管理系统解决方案的深化应用
    2022.07月总结
  • 原文地址:https://blog.csdn.net/weixin_74155781/article/details/132802216