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来接收。

猜你喜欢

转载自www.cnblogs.com/xiximayou/p/12177988.html