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

首頁 > 學院 > 開發設計 > 正文

springMVC注解@initbinder

2019-11-14 12:39:00
字體:
來源:轉載
供稿:網友

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

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

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

比較簡單的可以直接應用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"/>這樣就可以直接將上傳的日期時間字符串綁定為日期類型的數據了


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 军事| 高台县| 灌南县| 济源市| 广元市| 平顶山市| 都匀市| 绍兴县| 灵宝市| 镇安县| 遵化市| 南皮县| 沈阳市| 三亚市| 睢宁县| 易门县| 咸阳市| 三亚市| 通辽市| 阳江市| 金秀| 海城市| 噶尔县| 太原市| 临朐县| 中江县| 酒泉市| 锦屏县| 大兴区| 花莲县| 丹棱县| 沅江市| 南乐县| 福安市| 调兵山市| 霞浦县| 宣化县| 灵武市| 宝清县| 宜春市| 庆安县|