@RequestParam的具体使用

@RequestParam的具体使用

  • 作用:将请求体url里面的参数绑定到controller里对应名字的参数上

  • 语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””)

  • 具体代码:

package com.day01springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
 
/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 20:58 2018/11/16
 */
@RestController
public class HelloController2 {
 
    
    /**
     * 接收普通请求参数
     * http://localhost:8080/hello/show16?name=linuxsir
     * url参数中的name必须要和@RequestParam("name")一致
     * @return
     */
    
    @RequestMapping("show16")
    public String test16(@RequestParam("name")String name){
        return name;
    }
}

img


    /**
     * 接收普通请求参数
     * http://localhost:8080/hello/show17
     * url中没有name参数不会报错、有就显示出来
     * @return
     */
    @RequestMapping("show17")
    public String test17(@RequestParam(value="name",required=false)String name){
       return name;
    }
 

img

 /**
     * 接收普通请求参数
     * http://localhost:8080/hello/show18?name=998 显示为998
     * http://localhost:8080/hello/show18?name 显示为hello
     * @return
     */
    @RequestMapping("show18")
    public String test18(@RequestParam(value="name",required=true,defaultValue="hello")String name){
       return name;
    }
 
``
 

img

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值