• SpringMVC的请求(上)


    文章目录

    一、获得请求参数

    在这里插入图片描述

    二、获得基本类型参数

    在这里插入图片描述

     @RequestMapping(value = "/quick11")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public void save11(String userName,int age) throws IOException {
            System.out.println(userName);
            System.out.println(age);
     //http://localhost:8080/SpringMVC/user/quick11?userName=jack&age=17
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    三、获得pojo类型参数

    在这里插入图片描述

    @RequestMapping(value = "/quick12")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public void save12(User user) throws IOException {
            System.out.println(user);
    //http://localhost:8080/SpringMVC/user/quick12?username=jack&age=22
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    四、获得数组类型参数

    在这里插入图片描述

    @RequestMapping(value = "/quick13")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public void save13(String [] strs) throws IOException {
            System.out.println(Arrays.asList(strs));
     //http://localhost:8080/SpringMVC/user/quick13?strs=111&strs=222&strs=333
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    五、获得集合类型参数

    在这里插入图片描述
    form.jsp:

    <%--
      Created by IntelliJ IDEA.
      User: 86166
      Date: 2022/6/29
      Time: 17:14
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@page isELIgnored="false" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <form action="${pageContext.request.contextPath}/user/quick14" method="post">
        <input type="text" name="userList[0].username">
        <input type="text" name="userList[0].age">
        <input type="text" name="userList[1].username">
        <input type="text" name="userList[1].age">
        <input type="submit" value="提交">
    
    </form>
    
    </body>
    </html>
    
    
    • 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

    VO:

    package com.study.domain;
    
    import java.util.List;
    
    public class VO {
        private List<User> userList;
    
        public List<User> getUserList() {
            return userList;
        }
    
        public void setUserList(List<User> userList) {
            this.userList = userList;
        }
    
        @Override
        public String toString() {
            return "VO{" +
                    "userList=" + userList +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
     @RequestMapping(value = "/quick14")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public void save14(VO vo) throws IOException {
            System.out.println(vo);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    六、请求数据乱码

    在这里插入图片描述

     <!-- 配置全局过滤filter-->
        <filter>
            <!--   随便命名     -->
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>utf-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <!--   /*:表示强制所有的请求先通过过滤器处理     -->
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
     @RequestMapping(value = "/quick14")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public void save14(VO vo) throws IOException {
            System.out.println(vo);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    七、参数绑定注解

    当请求参数名称与Controller的业务方法参数不一致时,需要通过@RequestParam注解显示绑定。
    在这里插入图片描述

     @RequestMapping(value = "/quick15")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public void save15(@RequestParam(value = "name",required = false,defaultValue = "jack")String userName) throws IOException {
            System.out.println(userName);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    八、获得Restful风格参数

    在这里插入图片描述
    在这里插入图片描述

    //http://localhost:8080/SpringMVC/user/quick16/jack
        @RequestMapping(value = "/quick16/{username}")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public void save16(@PathVariable("username") String userName) throws IOException {
            System.out.println(userName);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    九、自定义类型转换器

    在这里插入图片描述
    1.实现DateConverter接口:

    package com.study.converter;
    import org.springframework.core.convert.converter.Converter;
    
    import javax.xml.crypto.Data;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateConverter implements Converter<String,Date> {
    
        @Override
        public Date convert(String dateStr) {
            //将日期字符串转换为日期对象 返回
            Date date=null;
            SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
            try {
                date = d.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
    
            return date;
        }
    }
    
    
    • 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

    2.声明转换器:

    
        <!--声明转换器-->
        <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
            <property name="converters">
                <list>
                    <bean class="com.study.converter.DateConverter"></bean>
                </list>
            </property>
        </bean>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.在annotation-driven中引用转换器

     <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    
    • 1
     @RequestMapping(value = "/quick17")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public void save17(Date date) throws IOException {
            System.out.println(date);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    十、获得Servlet相关的API

    在这里插入图片描述

     @RequestMapping(value = "/quick18")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public void save18(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
            System.out.println(request);
            System.out.println(response);
            System.out.println(session);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    十一、获得请求头

    在这里插入图片描述

     @RequestMapping(value = "/quick19")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public void save19(@RequestHeader(value = "User-Agent",required = false) String user_agent) throws IOException {
            System.out.println(user_agent);
    
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    十二、@CookieValue

    在这里插入图片描述

     @RequestMapping(value = "/quick20")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public void save20(@CookieValue(value = "JSESSIONID",required = false) String jsessionId) throws IOException {
            System.out.println(jsessionId);
    
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    golang适合做什么
    FPGA之旅设计99例之第一例-----LED灯
    滚雪球学Java(09-1):Java中的算术运算符,你真的掌握了吗?
    【Lua基础 第6章】 Lua 数组、Lua的错误处理、Lua 模块与包、元表(Metatable)和元方法
    二、python+前端 实现MinIO分片上传
    8┃音视频直播系统之 WebRTC 信令系统实现以及通讯核心并实现视频通话
    C++中变量是按值访问的, Python 中变量的值是按引用访问的示例说明
    全志F133(D1s)芯片 如何在Tina下进行显示旋转?
    Linux系统——Tomcat部署及优化
    Linux网络套接字之TCP网络程序
  • 原文地址:https://blog.csdn.net/qq_43514330/article/details/125530207