栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring MVC表单处理

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring MVC表单处理

您可以将自定义编辑器注册到控制器类的initBinder中:

@Controllerpublic class CoinViewController {    @Autowired    private CountryEditor countryEditor;    @InitBinder    protected void initBinder(final WebDataBinder binder, final Locale locale) {        binder.registerCustomEditor(Country.class, countryEditor);    }    ......}

locale
在这种情况下不需要参数,但是如果您需要使用语言环境进行转换,例如在使用日期时,该参数将很有用)

您可以定义

CountryEditor
以下内容:

@Componentpublic class CountryEditor extends PropertyEditorSupport {    @Autowired    private CountryService countryService;    @Override    public void setAsText(final String text) throws IllegalArgumentException {        try{  final Country country = countryService.findById(Long.parseLong(text)); setValue(cliente);        }catch(Exception e){ setValue(country); // or handle your exception        }    }}

我让spring用

@Component
注解处理编辑器的注入。因此,如果您喜欢这种方式,请记住为该类启用程序包扫描!

希望有帮助!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/466708.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号