SpringMVC的@InitBinder参数转换

@Controller
@RequestMapping("/index")
public class IndexController {
	/**
	 * 解决前端传递的日期参数验证异常
	 * 
	 * @param binder
	 * @author hzj
	 */
	@InitBinder({"param", "date"})//指定校验参数
	protected void initBinder(WebDataBinder binder) {
		// binder.setDisallowedFields("name"); // 不绑定name属性
		binder.registerCustomEditor(String.class, new StringTrimmerEditor());

		// 此处使用Spring内置的CustomDateEditor
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}


	@ResponseBody
	@GetMapping("/initbinder")
	public String testInitBinder(String param, Date date) {
		return param + ":" + date;
	}

}

  

猜你喜欢

转载自www.cnblogs.com/charkey/p/11588724.html