springmvc接收参数8种方式param1-param8

  • Post author:
  • Post category:其他



一.表单传参(string类型)

  • 控制层接收的参数名和页面表单传过来的参数名称相同

    • <%--表单数据提交1,表单名字和控制层获取的参数名一致,后台获取与输入的一致,可能有乱码--%>
      <form action="param1" method="post">
          <input type="text" name="username"><br>
          <input type="submit">
      </form>
      
      
          @RequestMapping("/param1")
          @ResponseBody
          public String param1(String username){
              System.out.println("param1==>"+username);
              return "param1";
          }

  • 控制层接收的参数名和页面表单传过来的参数名称不同,直接传,传不过来,值为null,需要加个注解,如果设置了默认值,不输入时,值为默认值,分页时可以用到

    • <%--表单数据提交1,表单名字和控制层获取的参数名不一致时,后台获取为null
      如果想要一致,加上@RequestParam(value = "username")
      --%>
      <form action="param2" method="post">
          <input type="text" name="username"><br>
          <input type="submit">
      </form>
      @RequestMapping("/param2")
          @ResponseBody
          //参数名称和表单中传过来的参数名称不一致,加上@RequestParam,还可以写一个默认值defaultValue,避免传过来的时候为空
          public String param2(@RequestParam(value = "username",defaultValue = "我给你设置了一个默认值") String name){
              System.out.println("param2==>"+name);
              return "param2";
          }


二. 表单传参(数组,param3)

<form action="param3" method="post">
    <input type="text" name="username"><br>
    <input type="checkbox" name="ah" value="唱歌">唱歌
    <input type="checkbox" name="ah" value="运动">运动
    <input type="checkbox" name="ah" value="跳舞">跳舞 <br>
    <input type="submit">
</form>
@RequestMapping("/param3")
    @ResponseBody//需要转json,要去导入三个jackson依赖
    //还需要在springmvc.xml里面配置注解驱动
    //防止乱码可以去web.xml文件里面配置过滤器
    public Object param3(String[] ah,String username){
        System.out.println("param3==>"+ah+"username:"+username);
        return ah;
    }

在springmvc.xml配置注解驱动

在web.xml文件配置过滤器,防止中文乱码

    <filter>
        <filter-name>filter</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>filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>




三.表单传参(用集合来接收页面传过来的参数,param4,)


页面还是数组那个页面

控制层代码,需要加@RequestParam,不然报错500

 @RequestMapping("/param4")
    @ResponseBody//需要转json,要去导入三个jackson依赖
    //还需要在springmvc.xml里面配置注解驱动
    //防止乱码可以去web.xml文件里面配置过滤器
    public Object param4(@RequestParam List<String> ah, String username){
        System.out.println("param4==>"+ah+"username:"+username);
        return ah;
    }

四.表单传参(param5),对象的形式接收,类中有一个数组属性,在这个案例当中,用来模拟CheckBox传过来的值,


传来的参数名和类的属性名相同,用对象的方式是可以取到复选框这种数组的/多选项




,前提是这个数组要是这个类的属性


<form action="param5" method="post">
    <input type="text" name="stuId"><br>
    <input type="text" name="stuName"><br>
    <input type="text" name="age"><br>
    <input type="checkbox" name="ah" value="唱歌">唱歌
    <input type="checkbox" name="ah" value="运动">运动
    <input type="checkbox" name="ah" value="跳舞">跳舞 <br>
    <input type="submit">
</form>

以对象的形式来取,可以省略servlet三部曲当中的第一步:取表单数据,别人帮我们做好了,但是在页面传值的时,

参数

名必须要和类的属性名一样


    @RequestMapping("/param5")
    @ResponseBody//需要转json,要去导入三个jackson依赖
    //还需要在springmvc.xml里面配置注解驱动
    //防止乱码可以去web.xml文件里面配置过滤器
    public Object param5(Student student){
//        System.out.println("param4==>"+ah+"username:"+username);
        return student;
    }




.传来的参数名和类的属性名不相同,或者说不是对象的属性:

  • 用map来接收页面传过来的数据,需要

    加上@RequestParam
  • 一定要注意,用map,因为是根据键来取值,键需要唯一,所以多选框这种是不适用的,比如让你去选择爱好,爱好有很多选项,但是name=“ah”对应多个选项,所以会有问题,只能拿到爱好当中的一个

  • 简单来说,有复选框,多数据选择时不用map



注意此时用来map,ah复选框只能取到一个值


<form action="param6" method="post">
    <input type="text" name="stuId"><br>
    <input type="text" name="stuName"><br>
    <input type="text" name="age"><br>
    <input type="checkbox" name="ah" value="唱歌">唱歌
    <input type="checkbox" name="ah" value="运动">运动
    <input type="checkbox" name="ah" value="跳舞">跳舞 <br>
    <input type="submit">
</form>
  @RequestMapping("/param6")
    @ResponseBody
    public Object param6(@RequestParam Map map){
        return map;
    }


六.当存在多对一或者一对多关系的时候


  • 比如一个班级有多个学生,学生表当中有班级表的外键,如下图

  • 那么我们刚刚用对象的方式来传值,就需要注意了,在页面传的时候,就不能直接传
  • className和classId,因为在学生类当中,班级类是作为学生类的一个属性


多对一的关系,注意到这一点就差不多了,其他的和“四”差不多

七.传递时间

<hr>
<%--传递时间--%>
<form action="param8" method="post">
    <input type="text" name="stuName" placeholder="学生名称"> <br>
    <input type="text" name="creatTime" placeholder="创建时间"> <br>
    <input type="submit">
</form>

  • 传递的时间不是对象的属性


    • 直接在controller里面转换​​​​​​​

       @RequestMapping("/param8")
          @ResponseBody
          public Object param8(String stuName, @DateTimeFormat(pattern = "yyyy-MM-dd") Date creatTime){
              System.out.println(creatTime);
              //这里是从页面到controller,直接字符串到参数转为日期,但是如果这个日期是对象的属性呢?
              return creatTime;
          }
  • 传递的时间是对象的属性

    • 则需要在类当中用注解进行转换​​​​​​​



版权声明:本文为qq_60555957原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。