国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

springMVC注解@initbinder

2019-11-14 12:07:55
字體:
來源:轉載
供稿:網(wǎng)友

搞了一上午的bug最終還是因為自己sPRingMVC的注解不熟悉的原因,特此記錄。

在SpringMVC中,bean中定義了Date,double等類型,如果沒有做任何處理的話,日期以及double都無法綁定。解決的辦法就是使用spring mvc提供的@InitBinder標簽在我的項目中是在BaseController中增加方法initBinder,并使用注解@InitBinder標注,那么spring mvc在綁定表單之前,都會先注冊這些編輯器,當然你如果不嫌麻煩,你也可以單獨的寫在你的每一個controller中。剩下的控制器都繼承該類。spring自己提供了大量的實現(xiàn)類,諸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等許多,基本上夠用。

在實際操作中經(jīng)常會碰到表單中的日期 字符串和javabean中的日期類型的屬性自動轉換, 而springMVC默認不支持這個格式的轉換,所以必須要手動配置, 自定義數(shù)據(jù)類型的綁定才能實現(xiàn)這個功能。

比較簡單的可以直接應用springMVC的注解@initbinder和spring自帶的WebDataBinder類和操作

[java] view plain copy print?在CODE上查看代碼片@InitBinder      public void initBinder(WebDataBinder binder) {          SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd”);          dateFormat.setLenient(false);          binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));      }  
@InitBinder    public void initBinder(WebDataBinder binder) {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");        dateFormat.setLenient(false);        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));    }還要在springMVC配置文件中加上

[html] view plain copy print?在CODE上查看代碼片<!– 解析器注冊 –>      <bean class=“org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter”>          <property name=“messageConverters”>              <list>                  <ref bean=“stringHttpMessageConverter”/>              </list>          </property>      </bean>      <!– String類型解析器,允許直接返回String類型的消息 –>      <bean id=“stringHttpMessageConverter” class=“org.springframework.http.converter.StringHttpMessageConverter”/>  
<!-- 解析器注冊 -->    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">        <property name="messageConverters">            <list>                <ref bean="stringHttpMessageConverter"/>            </list>        </property>    </bean>    <!-- String類型解析器,允許直接返回String類型的消息 -->    <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>這樣就可以直接將上傳的日期時間字符串綁定為日期類型的數(shù)據(jù)了


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 敦化市| 四川省| 明光市| 平果县| 蓬安县| 扬中市| 新源县| 西华县| 嘉黎县| 海伦市| 叙永县| 会同县| 察隅县| 清水河县| 余干县| 旬阳县| 秀山| 榆林市| 蒲城县| 新安县| 高淳县| 读书| 贵州省| 定陶县| 南康市| 彩票| 寻乌县| 绍兴县| 汝南县| 安陆市| 宁陵县| 益阳市| 乌鲁木齐县| 灵台县| 突泉县| 喀喇沁旗| 精河县| 会理县| 东阿县| 射阳县| 柳河县|