av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring常用一些工具類實(shí)例匯總

瀏覽:100日期:2023-07-29 11:27:22

一、內(nèi)置Resource類型

org.springframework.core.io.UrlResource org.springframework.core.io.ClassPathResource:以類路徑的方式進(jìn)行訪問 org.springframework.core.io.FileSystemResource:以文件系統(tǒng)絕對(duì)路徑的方式進(jìn)行訪問 org.springframework.web.context.support.ServletContextResource:以相對(duì)于 Web 應(yīng)用根目錄的方式進(jìn)行訪問 org.springframework.core.io.InputStreamResource org.springframework.core.io.ByteArrayResource org.springframework.core.io.support.EncodedResource :就是Resource加上encoding, 可以認(rèn)為是有編碼的資源。當(dāng)您使用 Resource 實(shí)現(xiàn)類加載文件資源時(shí),它默認(rèn)采用操作系統(tǒng)的編碼格式。如果文件資源采用了特殊的編碼格式(如 UTF-8),則在讀取資源內(nèi)容時(shí)必須事先通過 EncodedResource 指定編碼格式,否則將會(huì)產(chǎn)生中文亂碼的問題。 org.springframework.core.io.VfsResource:在jboss里經(jīng)常用到, 相應(yīng)還有 工具類 VfsUtils org.springframework.util.ResourceUtils:它支持“classpath:”和“file:”的地址前綴,它能夠從指定的地址加載文件資源,常用方法:getFile()

二、本地化文件資源

org.springframework.core.io.support.LocalizedResourceHelper:允許通過文件資源基名和本地化實(shí)體獲取匹配的本地化文件資源并以 Resource 對(duì)象返回

三、操作 Servlet API 的工具類

org.springframework.web.context.support.WebApplicationContextUtils 工具類獲取 WebApplicationContext 對(duì)象。

WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);

四、XML工具類

org.springframework.util.xml.AbstractStaxContentHandler org.springframework.util.xml.AbstractStaxXMLReader org.springframework.util.xml.AbstractXMLReader org.springframework.util.xml.AbstractXMLStreamReader org.springframework.util.xml.DomUtils org.springframework.util.xml.SimpleNamespaceContext org.springframework.util.xml.SimpleSaxErrorHandler org.springframework.util.xml.SimpleTransformErrorListener org.springframework.util.xml.StaxUtils org.springframework.util.xml.TransformerUtils

五、web相關(guān)工具類

org.springframework.web.util.CookieGenerator org.springframework.web.util.HtmlCharacterEntityDecoder org.springframework.web.util.HtmlCharacterEntityReferences org.springframework.web.util.HtmlUtils:HTML 特殊字符轉(zhuǎn)義,常用方法 htmlEscape(),htmlUnescape()。 org.springframework.web.util.HttpUrlTemplate 這個(gè)類用于用字符串模板構(gòu)建url, 它會(huì)自動(dòng)處理url里的漢字及其它相關(guān)的編碼. 在讀取別人提供的url資源時(shí), 應(yīng)該經(jīng)常用 String url = "http://localhost/myapp/{name}/{id}" org.springframework.web.util.JavaScriptUtils:JavaScript 特殊字符轉(zhuǎn)義,常用方法:javaScriptEscape()。 org.springframework.web.util.Log4jConfigListener 用listener的方式來(lái)配制log4j在web環(huán)境下的初始化 org.springframework.web.util.UriTemplate org.springframework.web.util.UriUtils :處理uri里特殊字符的編碼 org.springframework.web.util.WebUtils getCookie(HttpServletRequest request, String name) 獲取 HttpServletRequest 中特定名字的 Cookie 對(duì)象。如果您需要?jiǎng)?chuàng)建 Cookie, Spring 也提供了一個(gè)方便的 CookieGenerator 工具類。 getSessionAttribute(HttpServletRequest request, String name) 獲取 HttpSession 特定屬性名的對(duì)象,否則您必須通過 request.getHttpSession.getAttribute(name) 完成相同的操作。 getRequiredSessionAttribute(HttpServletRequest request, String name) 和上一個(gè)方法類似,只不過強(qiáng)制要求 HttpSession 中擁有指定的屬性,否則拋出異常。 getSessionId(HttpServletRequest request) 獲取 Session ID 的值。 void exposeRequestAttributes(ServletRequest request, Map attributes) 將 Map 元素添加到 ServletRequest 的屬性列表中,當(dāng)請(qǐng)求被導(dǎo)向(forward)到下一個(gè)處理程序時(shí),這些請(qǐng)求屬性就可以被訪問到了。

六、參數(shù)檢測(cè)工具類org.springframework.util.Assert

Assert斷言工具類,通常用于數(shù)據(jù)合法性檢查。

平時(shí)做判斷通常都是這樣寫:

if (message== null || message.equls('')) { throw new IllegalArgumentException('輸入信息錯(cuò)誤!'); }

用Assert工具類上面的代碼可以簡(jiǎn)化為:

Assert.hasText((message, '輸入信息錯(cuò)誤!');下面來(lái)介紹一下Assert 類中的常用斷言方法:

Assert.notNull(Object object, 'object is required') - 對(duì)象非空 Assert.isTrue(Object object, 'object must be true') - 對(duì)象必須為true Assert.notEmpty(Collection collection, 'collection must not be empty') - 集合非空 Assert.hasLength(String text, 'text must be specified') - 字符不為null且字符長(zhǎng)度不為0 Assert.hasText(String text, 'text must not be empty') - text 不為null且必須至少包含一個(gè)非空格的字符 Assert.isInstanceOf(Class clazz, Object obj, 'clazz must be of type [clazz]') - obj必須能被正確造型成為clazz 指定的類

七、請(qǐng)求工具類 org.springframework.web.bind.ServletRequestUtils

//取請(qǐng)求參數(shù)的整數(shù)值:public static Integer getIntParameter(ServletRequest request, String name)public static int getIntParameter(ServletRequest request, String name, int defaultVal) -->單個(gè)值public static int[] getIntParameters(ServletRequest request, String name) -->數(shù)組

還有譬如long、float、double、boolean、String的相關(guān)處理方法。

八、其他工具類

org.springframework.util.FileCopyUtils:它提供了許多一步式的靜態(tài)操作方法,能夠?qū)⑽募?nèi)容拷貝到一個(gè)目標(biāo) byte[]、String 甚至一個(gè)輸出流或輸出文件中。 org.springframework.core.io.support.PropertiesLoaderUtils:允許您直接通過基于類路徑的文件地址加載屬性資源。 oorg.springframework.orm.hibernate5.support.OpenSessionInViewFilter:過濾器將 Hibernate Session 綁定到請(qǐng)求線程中,它將自動(dòng)被 Spring 的事務(wù)管理器探測(cè)到。所以 OpenSessionInViewFilter 適用于 Service 層使用 HibernateTransactionManager 或 JtaTransactionManager 進(jìn)行事務(wù)管理的環(huán)境,也可以用于非事務(wù)只讀的數(shù)據(jù)操作中。 org.springframework.web.filter.CharacterEncodingFilter:當(dāng)通過表單向服務(wù)器提交數(shù)據(jù)時(shí),一個(gè)經(jīng)典的問題就是中文亂碼問題。雖然我們所有的 JSP 文件和頁(yè)面編碼格式都采用 UTF-8,但這個(gè)問題還是會(huì)出現(xiàn)。解決的辦法很簡(jiǎn)單,我們只需要在 web.xml 中配置一個(gè) Spring 的編碼轉(zhuǎn)換過濾器就可以了。 org.springframework.web.filter.ServletContextRequestLoggingFilter:請(qǐng)求跟蹤日志過濾器。在日志級(jí)別為 DEBUG 時(shí)才會(huì)起作用。 org.springframework.web.util.WebAppRootListener org.springframework.web.IntrospectorCleanupListener:緩存清除監(jiān)聽器 org.springframework.util.StringUtils:字符串工具類 CollectionUtils:集合工具類 org.springframework.util.SerializationUtils:對(duì)象序列化與反序列化 org.springframework.util.NumberUtils:處理數(shù)字的工具類, 有parseNumber 可以把字符串處理成我們指定的數(shù)字格式, 還支持format格式, convertNumberToTargetClass 可以實(shí)現(xiàn)Number類型的轉(zhuǎn)化。 org.springframework.util.FileSystemUtils:遞歸復(fù)制、刪除一個(gè)目錄。 org.springframework.util.DigestUtils:MD5加密 org.springframework.util.AntPathMatcher:風(fēng)格的處理 org.springframework.util.AntPathStringMatcher org.springframework.util.ClassUtils:用于Class的處理 org.springframework.util.CommonsLogWriter org.springframework.util.CompositeIterator org.springframework.util.ConcurrencyThrottleSupport org.springframework.util.CustomizableThreadCreator org.springframework.util.DefaultPropertiesPersister org.springframework.util.LinkedCaseInsensitiveMap:key值不區(qū)分大小寫的LinkedMap org.springframework.util.LinkedMultiValueMap:一個(gè)key可以存放多個(gè)值的LinkedMap org.springframework.util.ObjectUtils:有很多處理null object的方法. 如nullSafeHashCode, nullSafeEquals, isArray, containsElement, addObjectToArray, 等有用的方法 org.springframework.util.PatternMatchUtils:spring里用于處理簡(jiǎn)單的匹配。 org.springframework.util.PropertyPlaceholderHelper:用于處理占位符的替換。 org.springframework.util.ReflectionUtils:反射常用工具方法. 有 findField, setField, getField, findMethod, invokeMethod等有用的方法。 org.springframework.util.StopWatch 一個(gè)很好的用于記錄執(zhí)行時(shí)間的工具類, 且可以用于任務(wù)分階段的測(cè)試時(shí)間. 最后支持一個(gè)很好看的打印格式. 這個(gè)類應(yīng)該經(jīng)常用。 org.springframework.util.SystemPropertyUtils org.springframework.util.TypeUtils:用于類型相容的判斷. isAssignable org.springframework.util.WeakReferenceMonitor 弱引用的監(jiān)控

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产高清一区二区三区 | 免费精品 | 日韩高清一区二区 | 中文字幕在线中文 | 黄色片视频网站 | 久久午夜国产精品www忘忧草 | 九色综合网 | 亚洲成人一区二区 | 四虎影 | 日韩成人免费中文字幕 | 天天干天天谢 | 国产精品国产三级国产aⅴ浪潮 | 亚洲精品美女视频 | 国产精品综合一区二区 | 自拍视频网 | 国产精品伦理一区二区三区 | 欧美日韩一区在线播放 | www.天天操.com| 中文字幕在线免费观看 | 九九热这里只有精品在线观看 | 日本久久综合 | 精品欧美黑人一区二区三区 | 亚洲国产黄 | 婷婷丁香在线视频 | 久久精品国产久精国产 | 日韩激情免费 | 福利av在线 | 国产精品免费一区二区三区四区 | 国产视频在线一区二区 | 一区二区激情 | 91久久久久久久久久久久久 | 久久偷人 | 欧美日韩中文字幕 | 亚洲欧美综合精品另类天天更新 | 欧产日产国产精品视频 | 国产福利视频导航 | 亚洲午夜在线 | 九色视频网站 | 日韩中文字幕高清 | 亚洲精品在线视频 | 中文字幕1区 |