下面为六种形式代码
//形式一:参数可以为request,response
@RequestMapping("test1")
public String test1(HttpServletRequest request , HttpServletResponse response){
return "index";
}
//形式二:参数可以为String,但URL的请求链接上,请求名必须与方法名一致
//例如:...?username=***&password=***
@RequestMapping("test2")
public String test2(String username,String password){
return "index";
}
//形式三:参数可以为Bean,但URL的请求链接上,请求名必须与对象属性名一致
//Bean的每个属性必须要有set方法
@RequestMapping("test3")
public String test3(User user){
return "index";
}
//形式四:参数可以为两个Bean
//如果请求中只写了一个请求参数,但是两个Bean中都有此参数对应的属性
//那么这两个Bean对应的属性都会赋值
@RequestMapping("test4")
public String test4(User user , Person person){
return "index";
}
//形式五:参数可以为数组
//请求链接上写多个名字一样的请求参数即可赋值
@RequestMapping("test5")
public String test5(String[] name){
return "index";
}
//形式六:参数为时间类型
//必须要在该类中注册属性编辑器
@RequestMapping("test6")
public String time(Date date){
System.out.println(date);
return "index";
}
//注册属性编辑器
@InitBinder
public void initBinder(ServletRequestDataBinder binder){
//registerCustomEditor()注册自定义风格属性编辑器
//参数一为Date的class对象
//参数二为CustomDateEditor()自定义时间风格编辑器,true表示可以为空
binder.registerCustomEditor(Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
注:形式二,形式三,形式四的参数会自动存入Request域中
声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。