最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC自定義屬性編輯器詳解及實(shí)例

 更新時(shí)間:2017年06月14日 14:52:40   投稿:lqh  
這篇文章主要介紹了SpringMVC自定義屬性編輯器詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下

SpringMVC自定義屬性編輯器詳解及實(shí)例

自定義springMVC的屬性編輯器主要有兩種方式,一種是使用@InitBinder標(biāo)簽在運(yùn)行期注冊(cè)一個(gè)屬性編輯器,這種編輯器只在當(dāng)前Controller里面有效;還有一種是實(shí)現(xiàn)自己的 WebBindingInitializer,然后定義一個(gè)AnnotationMethodHandlerAdapter的bean,在此bean里面進(jìn)行注冊(cè) ,這種屬性編輯器是全局的。 

第一種方式:

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//必須有一個(gè)參數(shù)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的配置文件里面定義一個(gè)AnnotationMethodHandlerAdapter,并設(shè)置其WebBindingInitializer屬性為我們自己定義的WebBindingInitializer對(duì)象 

<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> 

 第二種方式經(jīng)過上面兩步就可以定義一個(gè)全局的屬性編輯器了。

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

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論

犍为县| 南皮县| 韶山市| 津市市| 双鸭山市| 新蔡县| 时尚| 凤凰县| 阜城县| 什邡市| 黔南| 盐源县| 育儿| 抚顺市| 金乡县| 白银市| 建湖县| 新野县| 海南省| 贵州省| 甘泉县| 阜南县| 利津县| 儋州市| 榆林市| 马尔康县| 城固县| 皋兰县| 八宿县| 汾西县| 鹤岗市| 呼伦贝尔市| 隆德县| 深水埗区| 昆山市| 沈阳市| 朝阳区| 门源| 嘉义县| 海晏县| 延长县|