Spring MVC学习之——如何接收请求传过来的参数

Spring MVC接收请求的参数

Springmvc中,接收页面提交的数据是通过方法形参来接收:

  • 处理器适配器调用springmvc使用反射将前端提交的参数传递给controller方法的形参

  • springmvc接收的参数都是String类型,所以spirngmvc提供了很多converter(转换器)在特殊情况下需要自定义converter,如对日期数据

1.少量参数(基本数据类型)

直接接收即可

案例:
controller

java 复制代码
@Controller
@RequestMapping("/account")
public class AccountController {

    @RequestMapping("/findAccount5")
    public String findAccount5(String username,Model model){
        model.addAttribute("msg", username);
        return "success";
    }
    
    @RequestMapping("/findAccount6")
    public String findAccount6(String username,Integer age,Model model){
        model.addAttribute("msg", username+" "+age);
        return "success";
    }
}

jsp中的超链接

java 复制代码
<a href="/account/findAccount5?username=eric">参数接收-基本数据类型</a>
<a href="/account/findAccount6?username=eric&age=22">参数接收-多个基本数据类型</a>

2. POJO类型参数绑定

  • 编写pojo

    java 复制代码
    public class Account implements Serializable {
        private Integer id;
        private String name;
        private Float money;
        private Address address;
       //省略get set toString方法
     }
    java 复制代码
    public class Address implements Serializable {
        private String provinceName;
        private String cityName;
         //省略get set toString方法
     }
  • 编写controller

    java 复制代码
    package com.by.controller;
    
    import com.by.pojo.Account;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping("/account")
    public class AccountController {
    
        @RequestMapping("/saveAccount")
        public String saveAccount(Account account, Model model){
            model.addAttribute("msg", account);
            return "success";
        }
    }
  • 在index.jsp里面添加表单

    html 复制代码
    <form action="account/saveAccount" method="post">
        账户名称:<input type="text" name="name"><br/>
        账户金额:<input type="text" name="money"><br/>
        账户省份:<input type="text" name="address.provinceName"><br/>
        账户城市:<input type="text" name="address.cityName"><br/>
        <input type="submit" value="保存">
    </form>

3. restful

  • restful概述:

    (Representational State Transfer,表现层状态转移):URL定位资源时,用HTTP动词(GET,POST,DELETE,PUT)描述操作

restful风格URL

@PathVaribale

  • 作用

    用于绑定url中的占位符。例如:/account/{id},这个{id}就是url占位符

    url支持占位符是spring3.0之后加入的,是springmvc支持rest风格url的重要标志。

  • 描述需要使用指定的请求方式来请求该方法

    java 复制代码
    @Controller
    @RequestMapping("/account")
    public class AccountController {
        
    	@RequestMapping(value="/findAccount7/{id}")
        public String findAccount11(@PathVariable Integer id, Model model){
            model.addAttribute("msg", id);
            return "success";
        }
    }
  • 测试:在index.jsp里面定义超链接

    html 复制代码
    <a href="/account/findAccount7/123">restful传参</a><br>

4.JSON接收

jsp

java 复制代码
  $.ajax({
             type:"post",
             url:"/account/findAccount",
             contentType:"application/json;charset=UTF-8",
             data:'{"id":1,"name":"张二狗","money":999.0}',
             success:function(data){
                 //{id:2, name:铁柱, money:100}
                 alert(data.name);
             }
         })

Controller

java 复制代码
       @RequestMapping("/findAccount")
       @ResponseBody
       public Account findAccount(@RequestBody Account account){
          return account;
       }
相关推荐
何事误红尘1 分钟前
全志A40i学习笔记():开发资料、启用串口
学习
for_ever_love__17 分钟前
python基础语法学习: 异常的传递性
开发语言·python·学习
PC2005-cloud1 小时前
DeepSeek Harness 创建 skill 指南:结构、安装、使用
笔记·学习
YM52e1 小时前
分页查询的基石:ArkTS 为鸿蒙商品列表设计 LIMIT/OFFSET 的表
android·学习·华为·harmonyos
math_hongfan1 小时前
主从嵌套层次分明:ArkUI 订单卡片内嵌明细的鸿蒙界面
学习·华为·harmonyos
还不秃顶的计科生3 小时前
具身智能论文学习8:Octo: An Open-Source Generalist Robot Policy
人工智能·深度学习·学习·机器学习·语言模型·vla·vlm
woshihuanglaoshi4 小时前
事务加持防超卖:ArkTS 在鸿蒙里玩转 TRANSACTION 出入库
学习·华为·harmonyos
世人万千丶4 小时前
去重插入与超限淘汰:ArkTS 实现鸿蒙搜索历史的 LIMIT 艺术
运维·服务器·学习·华为·harmonyos·鸿蒙
2601_949950635 小时前
在线练题更方便,用练题簿打造属于自己的移动题库
学习·考研·刷题·小程序推荐
lv__pf5 小时前
spring之整合mybatis【TL spring 12】
mysql·spring·mybatis