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

首頁 > 編程 > JSP > 正文

SpringMVC自定義屬性編輯器詳解及實例

2024-09-05 00:18:59
字體:
來源:轉載
供稿:網友

Spring MVC是通用的軟件,這其中包括很多其他編輯器,本文是錯新技術頻道小編為大家帶來的SpringMVC自定義屬性編輯器詳解及實例,希望對你學習有幫助。

SpringMVC自定義屬性編輯器詳解及實例

自定義springMVC的屬性編輯器主要有兩種方式,一種是使用@InitBinder標簽在運行期注冊一個屬性編輯器,這種編輯器只在當前Controller里面有效;還有一種是實現自己的 WebBindingInitializer,然后定義一個AnnotationMethodHandlerAdapter的bean,在此bean里面進行注冊 ,這種屬性編輯器是全局的。?

第一種方式:

import java.beans.PropertyEditorSupport; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date;  import javax.servlet.http.HttpServletResponse;  import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping;  @Controller public class GlobalController {       @RequestMapping("test/{date}")   public void test(@PathVariable Date date, HttpServletResponse response) throws IOException     response.getWriter().write( date);    }      @InitBinder//必須有一個參數WebDataBinder   public void initBinder(WebDataBinder binder) {     binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));          binder.registerCustomEditor(int.class, new PropertyEditorSupport() {        @Override       public String getAsText() {         // TODO Auto-generated method stub         return getValue().toString();       }        @Override       public void setAsText(String text) throws IllegalArgumentException {         // TODO Auto-generated method stub         System.out.println(text + "...........................................");         setValue(Integer.parseInt(text));       }            });   }       } 

? 這種方式這樣寫了就可以了

?第二種:?

1.定義自己的WebBindingInitializer

package com.xxx.blog.util;  import java.util.Date; import java.text.SimpleDateFormat;  import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.support.WebBindingInitializer; import org.springframework.web.context.request.WebRequest;  public class MyWebBindingInitializer implements WebBindingInitializer {    @Override   public void initBinder(WebDataBinder binder, WebRequest request) {     // TODO Auto-generated method stub     binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));   }  }  

2.在springMVC的配置文件里面定義一個AnnotationMethodHandlerAdapter,并設置其WebBindingInitializer屬性為我們自己定義的WebBindingInitializer對象?

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">     <property name="cacheSeconds" value="0"/>     <property name="webBindingInitializer">       <bean class="com.xxx.blog.util.MyWebBindingInitializer"/>     </property>   </bean> 

?第二種方式經過上面兩步就可以定義一個全局的屬性編輯器了。

注意:當使用了<mvc:annotation-driven />的時候,它 會自動注冊DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter 兩個bean。這時候第二種方式指定的全局屬性編輯器就不會起作用了,解決辦法就是手動的添加上述bean,并把它們加在<mvc:annotation-driven/>的前面。

以上就是錯新技術頻道小編為大家帶來的SpringMVC自定義屬性編輯器詳解及實例,更多的專業資訊盡在js.VeVb.com!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东辽县| 山丹县| 军事| 建水县| 察隅县| 马鞍山市| 鄄城县| 衡东县| 涪陵区| 囊谦县| 新干县| 谷城县| 长寿区| 上犹县| 乐昌市| 始兴县| 安义县| 滕州市| 浑源县| 辛集市| 安康市| 喜德县| 新干县| 临澧县| 安乡县| 工布江达县| 巨野县| 江陵县| 曲水县| 朝阳县| 蒲城县| 嘉义市| 偏关县| 云浮市| 中山市| 宽甸| 海门市| 阳西县| 澄迈县| 嘉黎县| 秀山|