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;
	}

}

  

转载于:https://www.cnblogs.com/charkey/p/11588724.html

最新回复(0)
/jishucn3ueC_2FIvN4SCLZM9ThPhoJRTYMSUsslL5YMHQ_3D_3D8