摘要:的可以將一種類型轉(zhuǎn)換成另一種類型。的類名稱必須為。這個必須包含一個屬性,它列出要在應(yīng)用程序中使用的所有定制。在使用時,必須編寫一個實現(xiàn)接口的類。方法相反,它是返回目標對象的字符串表示法。為此,在應(yīng)用程序中,選擇比選擇更合適。
1. Converter
Spring的Converter可以將一種類型轉(zhuǎn)換成另一種類型。
在使用時,必須編寫一個實現(xiàn)org.springframework.core.convert.converter.Converter接口的java類。這個接口的聲明如下
public interface Converter{ T convert(S var1); }
這里的S表示源類型,T表示目標類型。
下面展示了一個將String類型轉(zhuǎn)換成Date類型的Converter
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.core.convert.converter.Converter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StringToDateConverter implements Converter{ private static final Log logger = LogFactory.getLog(StringToDateConverter.class); private String datePattern; public StringToDateConverter(String datePattern) { this.datePattern = datePattern; } public Date convert(String s) { try { SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern); dateFormat.setLenient(false); return dateFormat.parse(s); } catch (ParseException e) { throw new IllegalArgumentException("invalid date format. Please use this pattern"" + datePattern + """); } } }
為了使用Spring MVC應(yīng)用程序定制的Converter,需要在配置文件中添加一個conversionService bean。Bean的類名稱必須為org.springframework.context.support.ConversionServiceFactoryBean。這個bean必須包含一個converters屬性,它列出要在應(yīng)用程序中使用的所有定制Converter。下面bean聲明注冊了上面StringToDateConverter。
之后,還需要給annotation-driven元素的conversion-service屬性賦上bean名稱,如下
2. Formatter
Formatter和Converter一樣,也是將一種類型轉(zhuǎn)換成另一種類型。但是,F(xiàn)ormatter的源類型必須是一個String。
在使用時,必須編寫一個實現(xiàn)org.springframework.format.Formatter接口的java類。這個接口的聲明如下
public interface Formatterextends Printer , Parser { } public interface Printer { String print(T var1, Locale var2); } public interface Parser { T parse(String var1, Locale var2) throws ParseException; }
這里的T表示輸入字符串要轉(zhuǎn)換的目標類型。
parse方法利用指定的Locale將一個String解析成目標類型。print方法相反,它是返回目標對象的字符串表示法。
下面展示了一個將String類型轉(zhuǎn)換成Date類型的Formatter
import org.springframework.format.Formatter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class DateFormatter implements Formatter{ private String datePattern; private SimpleDateFormat dateFormat; public DateFormatter(String datePattern) { this.dateFormat = dateFormat; dateFormat = new SimpleDateFormat(datePattern); dateFormat.setLenient(false); } public Date parse(String s, Locale locale) throws ParseException { try { SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern); dateFormat.setLenient(false); return dateFormat.parse(s); } catch (ParseException e) { throw new IllegalArgumentException("invalid date format. Please use this pattern"" + datePattern + """); } } public String print(Date date, Locale locale) { return dateFormat.format(date); } }
為了使用Spring MVC應(yīng)用程序定制的Formatter,需要在配置文件中添加一個conversionService bean。Bean的類名稱必須為org.springframework.format.support.FormattingConversionServiceFactoryBean。這個bean可以用一個formatters屬性注冊Formatter,用一個converters屬性注冊Converter。下面bean聲明注冊了上面DateFormatter。
之后,還需要給annotation-driven元素的conversion-service屬性賦上bean名稱,如下
3. 用Registrar注冊Formatter
注冊Formatter的另一種方法是使用Registrar。
下面就用Registrar來注冊前面的DateFormatter。
先需要實現(xiàn)org.springframework.format.FormatterRegistrar接口,如下
import org.springframework.format.FormatterRegistrar; import org.springframework.format.FormatterRegistry; public class MyFormatterRegistrar implements FormatterRegistrar { private String datePattern; public MyFormatterRegistrar(String datePattern) { this.datePattern = datePattern; } public void registerFormatters(FormatterRegistry formatterRegistry) { formatterRegistry.addFormatter(new DateFormatter(datePattern)); //register more formatters here } }
有了Registrar,就不需要在Spring MVC配置文件中注冊Formatter,只要在配置文件中注冊Registrar就行,如下
4. 選擇Converter還是Formatter
Converter是一般工具,可以將一種類型轉(zhuǎn)換成另一種類型。例如,將String轉(zhuǎn)換成Date,或者將Long轉(zhuǎn)換成Date。Converter既可以用在web層,也可以用在其它層中。
Formatter只能將String轉(zhuǎn)成成另一種java類型。例如,將String轉(zhuǎn)換成Date,但它不能將Long轉(zhuǎn)換成Date。所以,F(xiàn)ormatter適用于web層。為此,在Spring MVC應(yīng)用程序中,選擇Formatter比選擇Converter更合適。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/64816.html
摘要:處理器是繼前端控制器的后端控制器,在的控制下對具體的用戶請求進行處理。由于涉及到具體的用戶業(yè)務(wù)請求,所以一般情況需要程序員根據(jù)業(yè)務(wù)需求開發(fā)。 1、mcv整體架構(gòu)和流程 showImg(https://segmentfault.com/img/bV55Qq?w=860&h=406); 用戶發(fā)送請求至前端控制器 DispatcherServlet DispatcherServlet 收到...
摘要:現(xiàn)在給定一個項目的包結(jié)構(gòu)在中有以下配置只掃描注解可以看出要把最后的包寫上,不能包含子包,所以不能寫成。 大家好,我是豬弟,豬在我心中從來不是蠢的代名詞,而是懶的代名詞,本次準備記錄一個在開發(fā)測試過程中遇到的問題,跟蹤了三天spring和第三方RPC組件的源碼,最終發(fā)現(xiàn)了問題是因為第三方組件沒有處理好而父子容器導(dǎo)致的,還有一個因素是spring注解掃描重疊。 Spring版本:4.3...
摘要:關(guān)鍵注解的關(guān)鍵注解主要有其中主要是用于標記該類是一個控制器,用于指示的哪一個類或方法來處理請求動作,即用于標識具體的處理器。默認已經(jīng)裝配了作為組件的實現(xiàn)類,而由使用,將請求信息轉(zhuǎn)換為對象。 關(guān)鍵注解 springmvc的關(guān)鍵注解主要有@Controller/@RequestMapping/@RequestParam/@PathVariable/@RequestHeader/@Cooki...
摘要:源碼倉庫本文倉庫三層結(jié)構(gòu)表現(xiàn)層模型業(yè)務(wù)層持久層工作流程用戶前端控制器用戶發(fā)送請求前端控制器后端控制器根據(jù)用戶請求查詢具體控制器后端控制器前端控制器處理后結(jié)果前端控制器視圖視圖渲染視圖前端控制器返回視圖前端控制器用戶響應(yīng)結(jié) SpringMvc 【源碼倉庫】【本文倉庫】 三層結(jié)構(gòu) 表現(xiàn)層 MVC模型 業(yè)務(wù)層 service 持久層 dao 工作流程 用戶->前端控制器:用戶...
摘要:入門筆記簡介是一種基于的實現(xiàn)了設(shè)計模式的請求驅(qū)動類型的輕量級框架,是系開源項目中的一個,和配合使用。配置在中需要添加使用的和映射規(guī)則。入門較快,而掌握起來相對較難。 SpringMVC入門筆記 1. 簡介 Spring MVC是一種基于Java的實現(xiàn)了Web MVC設(shè)計模式的請求驅(qū)動類型的輕量級Web框架 ,是Spring系開源項目中的一個,和IoC配合使用。通過策略接口,Spring...
閱讀 1255·2021-09-22 15:32
閱讀 1812·2019-08-30 15:53
閱讀 3330·2019-08-30 15:53
閱讀 1487·2019-08-30 15:43
閱讀 532·2019-08-28 18:28
閱讀 2673·2019-08-26 18:18
閱讀 762·2019-08-26 13:58
閱讀 2615·2019-08-26 12:10