1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      利用SpringMVC如何實現(xiàn)一個自定義類型轉(zhuǎn)換器

      這篇文章將為大家詳細講解有關(guān)利用SpringMVC如何實現(xiàn)一個自定義類型轉(zhuǎn)換器,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

      德城ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

      我們在使用SpringMVC時,常常需要把表單中的參數(shù)映射到我們對象的屬性中,我們可以在默認的spring-servlet.xml加上如下的配置即可做到普通數(shù)據(jù)類型的轉(zhuǎn)換,如將String轉(zhuǎn)換成Integer和Double等:

      代碼如下:



      其實  標簽會默認創(chuàng)建并注冊一個 RequestMappingHandlerMapping(在Spring3.2之前是DefaultAnnotationHandlerMapping) 和 RequestMappingHandlerAdapter (Spring3.2之前是AnnotationMethodHandlerAdapter),當然,如果上下文有對應(yīng)的顯示實現(xiàn)類,將該注解注冊的覆蓋掉。該注解還會創(chuàng)建一個ConversionService,即 FormattingConversionServiceFactoryBean。

      如果想把一個字符串轉(zhuǎn)換成其它實體類型,spring沒有提供這樣默認的功能,我們需要自定義類型轉(zhuǎn)換器。

      這里有個實體類Employee。

      package com.proc;
      
      public class Employee {
      
        private String name;
        private Integer age;
        private String gender;
        public String getName() {
          return name;
        }
        public void setName(String name) {
          this.name = name;
        }
        public Integer getAge() {
          return age;
        }
        public void setAge(Integer age) {
          this.age = age;
        }
        public String getGender() {
          return gender;
        }
        public void setGender(String gender) {
          this.gender = gender;
        }
        @Override
        public String toString() {
          return "Employee [name=" + name + ", age=" + age + ", gender=" + gender
              + "]";
        }
        
      }

      規(guī)定將字符串轉(zhuǎn)換成實體類的規(guī)則為:  name-age-gender

      那么接下來就寫一個類型轉(zhuǎn)換器,需要實現(xiàn)一個接口org.springframework.core.convert.converter.Converter

      package com.proc;
      
      import org.springframework.core.convert.converter.Converter;
      import org.springframework.stereotype.Component;
      
      @Component
      public class EmployeeConverter implements Converter {
      
        @Override
        public Employee convert(String str) {
          Employee emp=null;
          //字符串格式 name-age-gender
          if(str!=null && str.split("-").length==3){
            emp=new Employee();
            String[] arr=str.split("-");
            emp.setName(arr[0]);
            emp.setAge(Integer.parseInt(arr[1]));
            emp.setGender(arr[2]);
          }
          return emp;
        }
      }

      這里我們?yōu)檗D(zhuǎn)換器加上@Component注解,是為了讓Spring自動掃描該轉(zhuǎn)換器到容器中。這樣就不用通過配置文件配置

      接下來編寫控制器

      package com.proc;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      
      @Controller
      public class EmployeeController {
      
        @RequestMapping("/add")
        public String add(@RequestParam("employee") Employee employee){
          System.out.println(employee);
          return "add";
        }
      }

      這里可以看到,參數(shù)的名字為employee,所有要為請求定義一個employee參數(shù),該參數(shù)傳入需要轉(zhuǎn)換的字符串

      除此之外,我們還需要在spring配置文件中配置,如下類容。讓轉(zhuǎn)換器生效

      <?xml version="1.0" encoding="UTF-8"?>
       
        
        
        
        
          
          
        
      
          
          
          
            
              
                
              
            
          
      

      測試代碼:http://localhost:8080/springmvc-1/add?employee=caoyc-18-male

      結(jié)果輸出:Employee [name=caoyc, age=18, gender=male]

      上面配置文件中我們使用配置了一個ConversionServiceFactoryBean。雖然可以這樣,但不建議。我們通常用org.springframework.format.support.FormattingConversionServiceFactoryBean

       為什么要這樣用呢?這樣用的好處是什么呢?

      使用FormattingConversionServiceFactoryBean可以讓SpringMVC支持@NumberFormat和@DateTimeFormat等Spring內(nèi)部自定義的轉(zhuǎn)換器。

      關(guān)于利用SpringMVC如何實現(xiàn)一個自定義類型轉(zhuǎn)換器就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。


      名稱欄目:利用SpringMVC如何實現(xiàn)一個自定義類型轉(zhuǎn)換器
      文章轉(zhuǎn)載:http://ef60e0e.cn/article/gedpsp.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        喀喇| 吴堡县| 呈贡县| 吉安县| 长葛市| 银川市| 沧州市| 宁蒗| 肇东市| 樟树市| 保靖县| 浙江省| 尉氏县| 漳浦县| 乌鲁木齐市| 南涧| 龙门县| 木兰县| 堆龙德庆县| 体育| 辰溪县| 简阳市| 固安县| 新郑市| 成武县| 绥德县| 文水县| 田阳县| 微山县| 松原市| 德保县| 哈密市| 铜鼓县| 伊金霍洛旗| 宽甸| 娱乐| 曲周县| 天峻县| 慈溪市| 嘉义市| 柳州市|