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

首頁 > 學院 > 開發設計 > 正文

Spring MVC--8.JSON、國際化、文件上傳

2019-11-08 02:52:51
字體:
來源:轉載
供稿:網友

九、處理JSON

1.基本步驟

(1)加入 jar 包:

(2)編寫目標方法,使其返回 JSON 對應的對象或集合

@ResponseBody    @RequestMapping("/testJson")    public Collection<Employee> testJson(){       return employeeDao.getAll();    } 

(3)在方法上添加@ResponseBody 注解

 

2. HttpMessageConverter<T>

         HttpMessageConverter<T>是 SPRing3.0 新添加的一個接口,負責將請求信息轉換為一個對象(類型為 T),將對象(類型為 T)輸出為響應信息

(1)接口定義的方法:

Boolean canRead(Class<?>clazz,MediaType – mediaType):

         指定轉換器可以讀取的對象類型,即轉換器是否可將請求信息轉換為 clazz 類型的對象,同時指定支持 MIME 類型(text/html,applaiction/json等)

Boolean canWrite(Class<?>clazz,MediaType mediaType):

         指定轉換– 器是否可將 clazz 類型的對象寫到響應流中,響應流支持的媒體類型在MediaType 中定義。

LIst<MediaType>getSupportMediaTypes():

         該轉換器支持的媒體– 類型。

T read(Class<? extends T>clazz,HttpInputMessage inputMessage– ):

         將請求信息流轉換為 T 類型的對象。

void write(T t,MediaTypecontnetType,HttpOutputMessgae –outputMessage):

         將T類型的對象寫到響應流中,同時指定相應的媒體類型為 contentType。

(2)運行流程

3.使用 HttpMessageConverter<T>

    使用 HttpMessageConverter<T> 將請求信息轉化并綁定到處理方法的入參中或將響應結果轉為對應類型的響應信息,Spring提供了兩種途徑:

    使用 @RequestBody / @ResponseBody– 對處理方法進行標注

    使用 HttpEntity<T> / ResponseEntity<T>作為處理方法的入參或返回值

 

    當控制器處理方法使用到 @RequestBody/@ResponseBody 或HttpEntity<T>

/ResponseEntity<T>時, Spring 首先根據請求頭或響應頭的Accept屬性選擇匹配的HttpMessageConverter, 進而根據參數類型或泛型類型的過濾得到匹配的HttpMessageConverter, 若找不到可用的HttpMessageConverter 將報錯

    @RequestBody @ResponseBody 不需要成? 對出現

 

<br>模擬文件上傳   <form action="testHttpMessageConverter" method="POST" enctype="multipart/form-data">      File:<input type="file" name="file">      Desc:<input type="text"  name="desc">      <input type="submit" value="提交">   </form>

@ResponseBody   @RequestMapping("/testHttpMessageConverter")   public StringtestHttpMessageConverter(@RequestBody String body){      System.out.println(body);      return "helloworld! "+ newDate();   }
<br>模擬文件下載   <a href="testResponseEntity">Test ResponseEntity</a>
@RequestMapping("/testResponseEntity")   public ResponseEntity<byte[]>testResponseEntity(Httpsession session) throws IOException{      byte [] body = null;      ServletContextservletContext = session.getServletContext();      InputStreamin = servletContext.getResourceAsStream("/files/abc.txt");      body= newbyte[in.available()];      in.read(body);           HttpHeadersheaders = newHttpHeaders();      headers.add("Content-Disposition","attachment;filename=abc.txt");           HttpStatusstatusCode = HttpStatus.OK;           ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers,statusCode);      return response;   } 

十、國際化

1.國際化概述

    默認情況下,SpringMVC 根據 Accept-Language參數判斷客戶端的本地化類型。

    當接受到請求時,SpringMVC 會在上下文中查找一個本地化解析器LocalResolver,找到后使用它獲取請求所對應的本地化類型信息。

    SpringMVC還允許裝配一個動態更改本地化類型的攔截器,這樣通過指定一個請求參數就可以控制單個請求的本地化類型。

 

 1.在頁面上能夠根據瀏覽器語言設置的情況對文本(不是內容), 時間, 數值進行本地化處理:   使用 JSTL 的 fmt 標簽<a href="i18n">I18N PAGE</a>

i18n.properties:

i18n.user=Useri18n.passWord=Passwordi18n_zh_CN.properties:

i18n.user=用戶名i18n.password=密碼i18n_en_US.properties:

i18n.user=Useri18n.password=Passwordi18n.jsp:

<fmt:message key="i18n.user"></fmt:message>     <br><br>   <a href="i18n2">I18N2 PAGE</a>i18n2.jsp:

<fmt:message key="i18n.password"></fmt:message>     <br><br>   <a href="i18n">I18N PAGE</a>springmvc.xml

   

<mvc:view-controller path="/i18n" view-name="i18n"/>    <mvc:view-controller path="/i18n2" view-name="i18n2"/>

   2. 可以在 bean 中獲取國際化資源文件 Locale 對應的消息在 bean 中注入 ResourceBundleMessageSource 的示例, 使用其對應的 getMessage 方法即可 注銷掉<mvc:view-controller path="/i18n" view-name="i18n"/>
@Autowired   privateResourceBundleMessageSource messageSource;     @RequestMapping("/i18n")   public String testI18n(Localelocale){      Stringval = messageSource.getMessage("i18n.user",null,locale);      System.out.println(val);      return "i18n";   } 

2.SessionLocaleResolver & LocaleChangeInterceptor 工作原理

 

3. 本地化解析器和本地化攔截器

AcceptHeaderLocaleResolver:根據 HTTP 請求頭的Accept-Language參數確定本地化類型,如果沒有顯式定義本地化解析器, SpringMVC 使用該解析器。

CookieLocaleResolver:根據指定的 Cookie 值確定本地化類型

SessionLocaleResolver:根據 Session 中特定的屬性確定本地化類型

LocaleChangeInterceptor:從請求參數中獲取本次請求對應的本地化類型。

 

i18n.jsp

<br><br>   <a href="i18n?locale=zh_CH">中文</a>     <br><br>   <a href="i18n?locale=en_US">英文</a> 

springmvc.xml

<!-- 配置 SessionLocalResolver -->   <bean id="localeResolver"      class="org.springframework.web.servlet.i18n.SessionLocaleResolver">   </bean>     <mvc:interceptors>           <!-- 配置LocaleChanceInterceptor -->      <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>   </mvc:interceptors> 

十一、文件上傳

1.概述

    Spring MVC 為文件上傳提供了直接的支持,這種支持是通過即插即用的MultipartResolver實現的。Spring 用Jakarta Commons FileUpload技術實現了一個

MultipartResolver 實現類:CommonsMultipartResovler

Spring MVC 上下文中默認沒有裝配 MultipartResovler,因此默認情況下不能處理文件的上傳工作,如果想使用Spring 的文件上傳功能,需現在上下文中配置MultipartResolver

 

 

2.配置 MultipartResolver

    defaultEncoding: 必須和用戶 JSP 的 pageEncoding 屬一致,以便正確解析表單的內容為了讓 CommonsMultipartResovler 正確工作,必須先將 Jakarta Commons FileUpload 及 Jakarta Commons io的類包添加到類路徑下。

index.jsp

<br><br>      <form action="testFileUpload"method="POST" enctype="multipart/form-data">       File: <input type="file"name="file"/>       Desc: <input type="text"name="desc"/>       <input type="submit"value="Submit"/>    </form>

 

Springmvc.xml

<!-- 配置 MultipartResolver -->   <bean id="multipartResolver"      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">      <property name="defaultEncoding" value="UTF-8"></property>      <property name="maxUploadSize" value="1024000"></property>   </bean> 

@RequestMapping("/testFileUpload")   public String testFileUpload(@RequestParam("desc") String desc,         @RequestParam("file") MultipartFilefile) throwsIOException{      System.out.println("desc: "+ desc);      System.out.println("OriginalFilename: "+ file.getOriginalFilename());      System.out.println("InputStream: "+ file.getInputStream());      return "success";   }

 

 

 源代碼文件:http://download.csdn.net/detail/QQ_26553781/9757497

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阜城县| 大同县| 神木县| 裕民县| 遂昌县| 栾川县| 武平县| 丰原市| 渭南市| 武宁县| 塔河县| 金湖县| 佛山市| 冷水江市| 三穗县| 荆州市| 钟祥市| 临城县| 广东省| 芒康县| 富裕县| 蕉岭县| 全州县| 贞丰县| 武安市| 石家庄市| 阿鲁科尔沁旗| 常熟市| 彭泽县| 昌图县| 淳安县| 陕西省| 五莲县| 肥城市| 吉水县| 射洪县| 新巴尔虎左旗| 筠连县| 宁乡县| 宁乡县| 鄄城县|