SpringMvc参数获取

这篇具有很好参考价值的文章主要介绍了SpringMvc参数获取。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

一、封装为简单数据类型

二、封装为单个对象

(1)实体类

(2)控制层

三、封装为关联对象

(1)实体类

(2)控制层

(3)使用表单发送带有参数的请求

四、封装为List集合

(1)控制层

五、封装为对象类型集合

(1)实体类

六、封装为Map集合

(1)实体类

七、使用Servlet原生对象获取参数

八、自定义参数类型转换器

(1)定义转换器类,实现Converter接口

(2)注册类型转换器对象


一、封装为简单数据类型

SpringMvc支持参数注入的方式用于获取请求数据,即将请求参数直接封装到方法的参数中。如下:

@Controller
public class MyController1 {
  
    @RequestMapping("/c1/h1")
    public void t1(String name,int age){
        System.out.println("name="+name+" age="+age);
    }
}

访问该方法时,请求参数名和方法参数名相同就可以自动完成封装。

http://localhost:8080/c1/h1?name=%E5%BC%A0%E4%B8%89&age=90

二、封装为单个对象

SpringMvc支持将参数直接封装为对象,如下:

(1)实体类

public class Student {
    private int id;
    private String name;
    private String sex;
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

   //省略了get和set
}

(2)控制层

 @RequestMapping("/c1/h2")
    public void t2(Student student){
        System.out.println(student);
    }

访问该方法时,请求参数名和方法参数的属性名相同,即可自动完成封装。

http://localhost:8080/c1/h2?name=%E6%9D%8E%E5%9B%9B&sex=%E7%94%B7&&id=3

三、封装为关联对象

(1)实体类

public class Student {
    private int id;
    private String name;
    private String sex;
    private Address address;

    //忽略get和set了,记住一定要有空参函数,不然会报空指针异常
}

public class Address {
    private String info;//地址信息
    private String postcode;//邮编
  //忽略get和set了,记住一定要有空参函数,不然会报空指针异常
}

(2)控制层

 @RequestMapping("/c1/h2")
    public void t2(Student student){
        System.out.println(student);
    }

访问该方法时,请求参数名和方法参数的属性名相同,既可自动完成封装。

http://localhost:8080/c1/h2?name=%E6%9D%8E%E5%9B%9B&sex=%E7%94%B7&&id=3&address.info=beijing&address.postcode=20

(3)使用表单发送带有参数的请求

我们也可以使用表单发送带有参数的请求

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>表单提交</title>
</head>
<body>
<form action="/c1/h2" method="post">
    id:<input name="id">
    姓名:<input name="name">
    性别:<input name="sex">
    住址:<input name="address.info">
    邮编:<input name="address.postcode">
    <input type="submit">
</form>
</body>
</html>

四、封装为List集合

(1)控制层

//绑定简单数据类型List参数,参数前必须添加@RequestParam注解

 @RequestMapping("/c1/h4")
    public void t5(@RequestParam List<String> u){
        System.out.println(u);
    }

请求的参数写法:http://localhost:8080/c1/h4?u=89&u=sdsd&u=beijing

不仅仅可以绑定List,同时他也可以绑定数组,如下:

@RequestMapping("/c1/h3")
    public void t4(@RequestParam String[] users){
        System.out.println("------------------");
        for(String u:users){
            System.out.println(u);
        }
    }

 请求参数写法:http://localhost:8080/c1/h4?users=908&users=jhg

五、封装为对象类型集合

SpringMvc不支持将参数封装为对象类型的LIst集合,但可以封装到有List属性的对象中

(1)实体类

public class Student {
  private int id;
  private String name;
  private String sex;
  private List<Address> address; // 地址集合
  // 省略getter/setter/tostring
}

请求的参数写法:http://localhost:8080/c1/h2?name=op&sex=male&id=12&address[0].info=beijing&address[0].postcode=9090&address[1].info=anhui&address[1].postcode=87

六、封装为Map集合

SpringMvc要封装Map集合,需要封装到装有Map属性的对象中

(1)实体类

package com.gq.pojo;

import java.util.List;
import java.util.Map;

public class Student {
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Map<String, Address> getAddress() {
        return address;
    }

    public void setAddress(Map<String, Address> address) {
        this.address = address;
    }

    private String name;
    private String sex;
    private Map<String,Address> address;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", address=" + address +
                '}';
    }

    public Student() {

    }



}

请求的参数写法:

http://localhost:8080/c1/h2?id=1&name=bz&sex=female&address[%E2%80%98one%E2%80%99].info=bj&address[%E2%80%98one%E2%80%99].postcode=100010&address[%E2%80%98two%E2%80%99].info=sh&address[%E2%80%98two%E2%80%99].postcode=100011

七、使用Servlet原生对象获取参数

SpringMvc也支持使用Servlet原生对象,在方法参数中定义HttpServletRequest,HttpServletResponse,HttpSession等类型的参数即可直接在方法中使用。

一般情况下,在SpringMvc中都会有对Servlet原生对象的方法的替代,推荐使用SpringMvc的方式代替Servlet原生对象。

    @RequestMapping("/c1/h5")
    public void t6(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println(request.getParameter("name"));
        System.out.println(response.getCharacterEncoding());
        System.out.println(session.getId());
    }

访问路径:http://localhost:8080/c1/h5?name=ioio

八、自定义参数类型转换器

前端传来的参数全部为字符串类型,SpringMvc使用自带的转换器将字符串参数转为需要的类型。如:

@RequestMapping("/c1/h6")
    public void t7(String name,int age){
        System.out.println(name.length());
        System.out.println(age*2);
    }

获取过来的name确实是String类型,age是int型。

但是在某些情况下,无法将字符串转为需要的类型,如:

@RequestMapping("/c1/h7")
    public void t8(Date date){
        System.out.println(date);
    }

这是因为日期数据有很多种格式,SpringMvc没办法将所有格式的字符串转换成日期类型。比如参数date=1786-01-01时,SpringMvc就无法解析参数。此时就需要自定义参数类型转换器。

(1)定义转换器类,实现Converter接口


//类型转换器必须要实现Converter接口,两个泛型表示转换之前的类型和转换后的类型
public class DateConverter implements Converter<String, Date> {
//source是转换类型前的数据
    @Override
    public Date convert(String source) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
//date是转换类型后的数据
        Date date=null;
        try{
            date=simpleDateFormat.parse(source);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return date;
    }
}

(2)注册类型转换器对象

在核心配置文件SpringMvcConfig.xml配置

SpringMvc参数获取,Spring类型框架,java,前端,开发语言

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 扫描包 -->
    <context:component-scan base-package="com.gq"/>
    <!-- 开启SpringMVC注解的支持 -->
    <mvc:annotation-driven/>




    <!-- 配置转换器工厂 -->
    <bean id="converterFactory" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <!-- 转换器集合 -->
        <property name="converters">
            <set>
                <!-- 自定义转换器 -->
                <bean class="com.gq.converter.DateConverter"></bean>
            </set>
        </property>
    </bean>

            <!-- 使用转换器工厂 -->
    <mvc:annotation-driven conversion-service="converterFactory"></mvc:annotation-driven>


</beans>

请求路径:

此时再访问http://localhost:8080/c1/param9?date=1987-12-01时,SpringMVC即可将请求参数封装为Date类型的参数。文章来源地址https://www.toymoban.com/news/detail-732150.html

到了这里,关于SpringMvc参数获取的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • Spring MVC获取参数和自定义参数类型转换器及编码过滤器

    Spring MVC获取参数和自定义参数类型转换器及编码过滤器

    目录   一、使用Servlet原生对象获取参数 1.1 控制器方法 1.2 测试结果 二、自定义参数类型转换器 2.1 编写类型转换器类 2.2 注册类型转换器对象  2.3 测试结果  三、编码过滤器 3.1 JSP表单 3.2 控制器方法 3.3 配置过滤器 3.4 测试结果  往期专栏文章相关导读  1. Maven系列专

    2024年02月10日
    浏览(12)
  • Java框架学习(二)SSM体系:Spring、SpringMVC、MybatisPlus

    Java框架学习(二)SSM体系:Spring、SpringMVC、MybatisPlus

    在原始的分层架构实现中,负责响应请求的Controller层依赖于业务逻辑处理的Service层,而业务逻辑处理的service层又依赖与数据访问Dao层。上下层间相互依赖耦合,耦合的缺陷在于牵一发而动全身,不利于后期维护拓展。 为了分层解耦,Spring采用IoC控制反转和DI依赖注入,来解

    2024年02月11日
    浏览(29)
  • SpringMVC: Java Web应用开发的框架之选

    在当今的软件开发领域中,Web应用的需求不断增长。为了满足这种需求,各种Web框架应运而生。其中,SpringMVC作为一种优秀的Java Web框架,受到广泛关注和使用。本文将以文章的形式给您讲解SpringMVC的重要概念、工作原理和核心组件。 SpringMVC是基于Java的Web应用开发框架,它是

    2024年02月09日
    浏览(10)
  • Java后端和前端传递的请求参数的三种类型

    在 HTTP 请求中,常见的请求参数类型有三种:`application/x-www-form-urlencoded`、`multipart/form-data` 和 `application/json`(通常用于 `raw` 类型)。这三种类型主要指的是请求体中的数据格式,其中包括参数的传递方式和编码。 1. **`application/x-www-form-urlencoded`:**    - 这是默认的编码类型

    2024年02月02日
    浏览(14)
  • SpringMVC 获取参数

    SpringMVC 获取参数

    1、通过ServletAPI获取 将HttpServletRequest作为控制器方法的形参,此时HttpServletRequest类型的参数表示封装了当前请求的请求报文的对象 2、通过控制器方法的形参获取请求参数 在控制器方法的形参位置,设置和请求参数同名的形参,当浏览器发送请求,匹配到请求映射时,在Dis

    2024年01月21日
    浏览(7)
  • SpringMVC---获取参数

    2024年02月04日
    浏览(11)
  • SpringMVC-获取请求参数

    用户输入信息后,如果想要得到用户输入的内容 , springMVC 应该如何做呢? 本次课讲解下再springmvc中获取请求参数及中文乱码问题 通过servletAPI获取 讲HttpServletRequest作为控制器方法的形参,此时HttpServletRequest类型的参数表示封装了当前请求的请求报文的对象 通过控制器方法的形

    2024年01月21日
    浏览(10)
  • SpringMVC(三)获取请求参数

    SpringMVC(三)获取请求参数

    SpringMVC封装的就是原生的servlet 我们进行测试如下所示: 我们的登陆页面如下所示:  点击登录之后,即会跳转到成功界面。 同时我们的控制台会进行输出:username:admin,password:123456 我们的页面如下所示: 我们进行如下界面: 我们点击登陆之后,会跳转到成功界面, 此时控

    2024年02月08日
    浏览(10)
  • 34.SpringMVC获取请求参数

    将 HttpServletRequest 作为 控制器方法的形参 ,此时HttpServletRequest类型的参数表示 封装了当前请求 的请求报文的对象 index.html TestParamController.java 成功获取到表单提交的信息,这是采用原生Servlet的方式获取 在控制器方法的形参位置, 设置和请求参数同名的形参 ,当浏览器发送

    2024年02月12日
    浏览(10)
  • SpringMVC之获取请求参数

    下面用到了thymeleaf,不知道的可以看我同专栏里的搭建框架这篇文章。 将HttpServletRequest作为控制器方法的形参,此时HttpServletRequest类型的参数表示封装了当前请求的请求报文的对象。 在控制器方法的形参位置,设置和请求参数同名的形参,当浏览器发送请求,匹配到请求映

    2024年02月12日
    浏览(12)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包