springmvc之使用@RequestParam绑定请求参数

时间:2022-07-23
本文章向大家介绍springmvc之使用@RequestParam绑定请求参数,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
@RequestMapping("/springmvc")
@Controller
public class SpringmvcTest {
    private static final String SUCCESS = "success";
    
    @RequestMapping(value="/testRequestParam",method=RequestMethod.GET)
    public String testRequestParam(@RequestParam(value="username") String username,
            @RequestParam(value="age",required=false,defaultValue="0") Integer age) {
        System.out.println("username="+username+","+"age="+age);
        return SUCCESS;
    }
}

jsp页面:

<a href="springmvc/testRequestParam?username=tom&age=18">testRequestParam</a>

说明:可以使用RequestParam注解来传递前端请求传过来的参数,value表示传过来的参数名,required表示是否必须,defaultValue表示默认值。当在方法中使用Int接收整型数据时,必须要指定defaultValue="0",否则可以直接用Integer来接收。