SpringMVC中如何实现@InitBinder参数转换的实例代码?
- 内容介绍
- 文章标签
- 相关推荐
本文共计366个文字,预计阅读时间需要2分钟。
这篇文章主要介绍了SpringMVC中的@InitBinder参数转换代码实例。文中通过示例代码详细展示了如何使用@InitBinder进行参数转换,对初学者或工作者具有一定的参考价值。需要的伙伴可以参考以下内容:
java@Controllerpublic class MyController {
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(yyyy-MM-dd), true)); }
@RequestMapping(/example) public String example(@RequestParam(date) @DateTimeFormat(pattern=yyyy-MM-dd) Date date) { // 处理业务逻辑 return success; }}
以上代码展示了如何使用@InitBinder来注册自定义的日期编辑器,使得表单中的日期字符串能够被正确转换为Date对象。对于需要学习或工作的朋友,这段代码具有一定的参考价值。
这篇文章主要介绍了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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计366个文字,预计阅读时间需要2分钟。
这篇文章主要介绍了SpringMVC中的@InitBinder参数转换代码实例。文中通过示例代码详细展示了如何使用@InitBinder进行参数转换,对初学者或工作者具有一定的参考价值。需要的伙伴可以参考以下内容:
java@Controllerpublic class MyController {
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(yyyy-MM-dd), true)); }
@RequestMapping(/example) public String example(@RequestParam(date) @DateTimeFormat(pattern=yyyy-MM-dd) Date date) { // 处理业务逻辑 return success; }}
以上代码展示了如何使用@InitBinder来注册自定义的日期编辑器,使得表单中的日期字符串能够被正确转换为Date对象。对于需要学习或工作的朋友,这段代码具有一定的参考价值。
这篇文章主要介绍了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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

