今天在做增加用戶信息時遇到把表單序列化後提交不到controller的問題。原因是我的表單中包含Date類型,srpingmvc的bean類型中沒有包含Date、Double等類型。腫麼辦呢?
解決: 增加方法initBinder,並使用註解@InitBinder標註,那麼spring mvc在綁定表單之前,都會先註冊這些編輯器。
/**
* @ClassName UserListController
* @Description TODO(用戶資料管理)
* @author Administrator
* @Date 2017年10月12日 上午10:29:04
* @version 1.0.0
*/
@Controller
@RequestMapping("/user_list")
public class UserListController extends BaseController {
@Autowired
private UserService mUserService;
/**
*
* @Description (TODO 自定義數據綁定, 解決此問題)
* @param binder
*/
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
}