一、@RequestParam

这个注解用来绑定单个请求数据,既可以是url中的参数,也可以是表单提交的参数和上传的文件

它有三个属性,value用于设置参数名,defaultValue用于对参数设置默认值,required为true时,如果参数为空,会报错

好,下面展示具体例子:

首先是vm:

<h1>param1:${param1}</h1>
<h1>param2:${param2}</h1>

好吧,就为了展示两个参数

第一种情况:

    @RequestMapping(value = "/hello1.htm")
    public String hello1(ModelMap modelMap,Integer param1, int param2) {
        modelMap.addAttribute("param1", param1);
        modelMap.addAttribute("param2", param2);
        return "hello";
    }

这里前面的参数时包装型,后面的参数时原始类型

直接用url请求:

http://localhost:8080/hello1.htm?param1=1&param2=2

结果:

但是:

如果不传param2:  

http://localhost:8080/hello1.htm?param1=1

直接就报错了

因为无法将null转换为原始类型

所以:建议所有的参数都用包装类型,别用原始类型

第二种情况:

仍然是上面的那个controller,地址改为

http://localhost:8080/hello1.htm?param2=1&param1=2

就是让param2=1,param1=2,想试验下,参数绑定是和顺序有关,还是只和参数名称有关,结果:

所以,springMvc参数绑定只和参数名字有关系

第三种情况:

如果页面上表单里的参数和代码里的参数名不一样怎么办,这时候就可以用注解了:

 @RequestMapping(value = "/hello1.htm")
    public String hello1(ModelMap modelMap, @RequestParam(value = "paramTest") Integer param1, Integer param2) {
        modelMap.addAttribute("param1", param1);
        modelMap.addAttribute("param2", param2);
        return "hello";
    }

在param1前面加上了注解,这时候第一个参数只接受paramTest名字的参数,param1此时无效了。

如果此时我们这么请求:

http://localhost:8080/hello1.htm?param1=1&param2=2

spring直接报错,必须要这么请求了:

http://localhost:8080/hello1.htm?paramTest=1&param2=2

结果:

第四种情况:

有时候页面上的表单客户不填任何值,但是在控制器里希望它有默认值

可以这样:

    @RequestMapping(value = "/hello1.htm")
    public String hello1(ModelMap modelMap, @RequestParam(defaultValue = "5") Integer param1, Integer param2) {
        modelMap.addAttribute("param1", param1);
        modelMap.addAttribute("param2", param2);
        return "hello";
    }

这里用了RequestParam的defaultValue属性,如果url参数中没传param1,也不会报错,使用默认值,比如我们这么请求:

http://localhost:8080/hello1.htm?param2=2

结果:

但是,如果url中对param1赋值了:

http://localhost:8080/hello1.htm?param1=3&param2=2

结果:

也就是说,我们赋的值会修改默认值

第五种情况:

RequestParam还有个属性:required

意思是必须传值,否则报错,就是这么任性

        @RequestMapping(value = "/hello1.htm")
        public String hello1(ModelMap modelMap, @RequestParam(required = true) Integer param1, Integer param2) {
            modelMap.addAttribute("param1", param1);
            modelMap.addAttribute("param2", param2);
            return "hello";
        }

但是当required=true,和defaultValue= 同时出现时,required失效,可传可不传

简单类型参数绑定小结:

springMVC默认根据参数名字来绑定,而不是参数位置

使用包装类型,否则如果不传值,会报错

使用@RequestParam(value="")来改变参数名字

使用@RequestParam(defaultValue=""),不传参时,使用默认值

使用@RequestParam(required=true),强制必须传参数

二、@PathVariable

用这个注解可以将URL中的占位符参数绑定到控制器处理方法的入参中,可以这样用:

    @RequestMapping("/hello2.htm/{param1}/{param2}")
    public String hello2(ModelMap modelMap, @PathVariable Integer param1, @PathVariable Integer param2) {
        System.out.println("进入了hello2控制器");
        System.out.println(param1 + "," + param2);
        modelMap.addAttribute("param1", param1);
        modelMap.addAttribute("param2", param2);
        return "hello";
    }

URL:

http://localhost:8080/hello2.htm/1/2

结果:

如果不加PathVariable注解,是无法绑定的

    @RequestMapping("/hello2.htm/{param1}/{param2}")
    public String hello2(ModelMap modelMap,Integer param1, @PathVariable Integer param2) {
        System.out.println("进入了hello2控制器");
        System.out.println(param1 + "," + param2);
        modelMap.addAttribute("param1", param1);
        modelMap.addAttribute("param2", param2);
        return "hello";
    }

去掉了第一个参数的注解:

http://localhost:8080/hello2.htm/1/2

结果:

传了空值到页面,无法绑定

Spring Boot Web项目之参数绑定的更多相关文章

  1. 最简单的spring boot web项目

    搭建效果为: 直接在网页输入请求,在页面中显示一行文字:Hello,Spring Boot 与一般的wen项目不同的地方: 1.不需要配置web.xml 文件,但需要注解@SpringBootAppl ...

  2. spring boot web项目在IDEA下热部署解决办法(四步搞定)

    最近在用spring boot 做一个web站点,修改了类.html.js等,刷新页面,没有生效,非要手动去make一下或者重启,大大降低了开发效率. 什么是热部署? 应用启动后会把编译好的Class ...

  3. Spring Boot- 用idea新建spring boot web项目

    1.新建project 2.选择Spring Initializr,next 3.输入项目信息,next 4.选择web依赖以及Spring Boot的版本,next 5.Finish 6.Enabl ...

  4. 第三章 Maven构建 Java Spring Boot Web项目

    3.1   认识Srping Boot Spring Boot是一个框架,是一种全新的编程规范,它的产生简化了对框架的使用,简化了Spring众多的框架中大量的繁琐的配置文件,所以说Spring Bo ...

  5. 【Spring Boot】Spring Boot之自定义配置参数绑定到Java Bean

    一.@Value方式 1.我的配置文件:application-dev.yml # 自定义项目配置 startproject: pro1: pro2: pro3: pro4: lists: - ' - ...

  6. Spring Boot Web项目整合jsp页面访问

    这个内容就是实操过程中各种访问不了jsp页面,各种尝试后的解决方案吧.可能不是最优的,但是目前能解决项目中的一些问题.之前觉得没有什么问题直接可以操作的,没想到在这部分还是耗时了. 开发工具idea2 ...

  7. SpringCloud 微服务一:spring boot 基础项目搭建

    spring cloud是建立在spring boot的基础上的,而之前虽然听说过,也随便看了一下spring boot,却没有真正使用,因此还必须先花时间学一下spring boot. spring ...

  8. Springboot 系列(七)Spring Boot web 开发之异常错误处理机制剖析

    前言 相信大家在刚开始体验 Springboot 的时候一定会经常碰到这个页面,也就是访问一个不存在的页面的默认返回页面. 如果是其他客户端请求,如接口测试工具,会默认返回JSON数据. { &quo ...

  9. Spring Boot Web Executable Demo

    Spring Boot Web Executable Demo */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.sr ...

随机推荐

  1. codevs 1153 道路游戏

    传送门   题目描述 Description 小新正在玩一个简单的电脑游戏.游戏中有一条环形马路,马路上有n 个机器人工厂,两个相邻机器人工厂之间由一小段马路连接.小新以某个机器人工厂为起点,按顺时针 ...

  2. iOS常用动画-b

    CoreAnimationEffect.h //  CoreAnimationEffect // //  Created by VincentXue on 13-1-19. //  Copyright ...

  3. FileUpload 改变控件显示的文字

    浏览

  4. 转:Top 10 Algorithms for Coding Interview

    The following are top 10 algorithms related concepts in coding interview. I will try to illustrate t ...

  5. 变身windows达人,用运行命令直接启动所有应用程序

    先看一下效果 在”运行“输入QQ,敲回车即打开登录窗口,相当于双击QQ登录快捷方式,对于码农.电脑一族这是不是一件高逼格,很酷的事?你甚至可以将任何你想设置命令启动的程序用该方式启动,比如敲chrom ...

  6. 使用Protractor进行AngularJS e2e测试案例

    环境: y@y:karma-t01$ protractor --version Version y@y:karma-t01$ node -v v4.2.2 y@y:karma-t01$ y@y:kar ...

  7. BZOJ 2434 阿狸的打字机

    http://www.lydsy.com/JudgeOnline/problem.php?id=2434 思路:建立fail树,并找出dfs序,那剩下要做的就是每次找到一个串的位置,然后询问它的区间里 ...

  8. oracle_执行计划_谓词信息和数据获取(access and filter区别) (转)

    These two terms in the Predicate Information section indicate when the data source is reduced. Simpl ...

  9. Tag标签系统设计

    转一篇关于tag的文章:  <Tagging: People-powered Metadata for the Social Web>出版于2008年,中文版译为<标签:标记系统设计 ...

  10. COCI2014-2015CONTEST#7——POLICE

    http://www.hsin.hr/coci/archive/2014_2015/contest7_tasks.pdf [题目描述] 有N个书架,每个书架可以容纳M本书.给出了若干本书,每本书有一个 ...