接着上一集,记录参数绑定的过程;

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

springMVC学习(5)-参数绑定

一、默认支持的类型:

在controller形参中添加如下类型的参数处理适配器会默认识别并进行赋值:

HttpServletRequest、HttpServletResponse、HttpSession、

Modal/ModalMap(Model和ModelMap的效果一样,如果直接使用Model,springmvc会实例化ModelMap)

 

二、简单类型的绑定:

通过@RequestParam对简单类型的参数进行绑定。

如果不使用@RequestParam,要求request传入参数名称和controller方法的形参名称一致,方可绑定成功。

如果使用@RequestParam,不用限制request传入参数名称和controller方法的形参名称一致。

通过required属性指定参数是否必须要传入,如果设置为true,没有传入参数,报下边错误:

HTTP Status 400 - Required Integer parameter 'XXXX' is not present

默认支持的简单参数类型绑定:

Integer、String、float、double、boolean 还有吗?

 

三、pojo参数绑定:

页面中请求的参数name要和Controller形参中pojo的属性值名字一样,才可以绑定成功;

四、自定义参数绑定实现日期类型绑定:

对于controller形参中pojo对象,如果属性中有日期类型,需要自定义参数绑定。

将请求日期数据串传成日期类型,要转换的日期类型和pojo中日期属性的类型保持一致。

 

以下修改商品Item的代码为例子来说明:

ItemsController:

 1 @Controller
 2 @RequestMapping("/items")
 3 public class ItemsController {
 4     
 5     @Autowired
 6     private ItemsService itemsService;
 7     
 8     @RequestMapping("/findItems")
 9     public ModelAndView findItems() throws Exception {
10         
11         List<ItemsCustom> itemsList = itemsService.findItemsList(null);
12         
13         ModelAndView modelAndView =  new ModelAndView();
14         modelAndView.addObject("itemsList", itemsList);
15         modelAndView.setViewName("items/itemsList");
16         return modelAndView;
17     }
18     
19     //商品信息修改页面显示
20     @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
21     //@RequestParam里边指定request传入参数名称和形参进行绑定。
22     //通过required属性指定参数是否必须要传入
23     //通过defaultValue可以设置默认值,如果id参数没有传入,将默认值和形参绑定。
24     public String editItems(Model model,@RequestParam(value="id",required=true,defaultValue="4") Integer items_id) throws Exception{
25         ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
26         
27         //通过形参中的model将model数据传到页面
28         //相当于modelAndView.addObject方法
29         model.addAttribute("itemsCustom", itemsCustom);
30         
31         return "items/editItems";
32     }
33     
34     //商品信息修改提交
35     @RequestMapping("/editItemsSubmit")
36     public String editItemsSubmit(HttpServletRequest request, Integer id, ItemsCustom itemsCustom)throws Exception {
37         
38         itemsService.updateItems(id, itemsCustom);
39         
40         //重定向到商品查询列表
41         return "redirect:findItems.action";
42         //页面转发
43         //return "forward:findItems.action";
44         //return "success";
45     }
46 }

editItems.jsp:

 1 <title>修改商品信息</title>
 2 
 3 </head>
 4 <body> 
 5 
 6 <form id="itemForm" action="${pageContext.request.contextPath }/items/editItemsSubmit.action" method="post" >
 7 <input type="hidden" name="id" value="${itemsCustom.id }"/>
 8 修改商品信息:
 9 <table width="100%" border=1>
10 <tr>
11     <td>商品名称</td>
12     <td><input type="text" name="name" value="${itemsCustom.name }"/></td>
13 </tr>
14 <tr>
15     <td>商品价格</td>
16     <td><input type="text" name="price" value="${itemsCustom.price }"/></td>
17 </tr>
18 <tr>
19     <td>商品生产日期</td>
20     <td><input type="text" name="createtime" value="<fmt:formatDate value="${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
21 </tr>

CustomDateConverter日期转换器:

 1 package com.cy.controller.converter;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 
 7 import org.springframework.core.convert.converter.Converter;
 8 
 9 /**
10  *     日期转换器
11  */
12 public class CustomDateConverter implements Converter<String,Date>{
13     @Override
14     public Date convert(String source) {
15         //实现 将日期串转成日期类型(格式是yyyy-MM-dd HH:mm:ss)
16         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
17         try {
18             //转成直接返回
19             return simpleDateFormat.parse(source);
20         } catch (ParseException e) {
21             e.printStackTrace();
22         }
23         //如果参数绑定失败返回null
24         return null;
25     }
26 
27 }
View Code

相关文章: