国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - 學習SpringMVC——國際化+上傳+下載詳解

學習SpringMVC——國際化+上傳+下載詳解

2020-07-12 15:39JackieZheng JAVA教程

本篇文章主要介紹了學習SpringMVC——國際化+上傳+下載,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。

一個軟件,一個產品,都是一點點開發并完善起來的,功能越來越多,性能越來越強,用戶體驗越來越好……這每個指標的提高都需要切切實實的做點東西出來,好比,你的這個產品做大了,用的人多了,不僅僅再是上海人用,北京人用,還有印度人用,法國人用等等,可以說這個產品已經走上了國際化的大舞臺。當印度的哥們輸入url訪問產品時,界面上彈出“歡迎您,三哥”,估計哥們當場就蒙圈了。而這個時候,國際化就應運而生了。

要做國際化這道菜,真的沒有想象中的那么復雜,反而很簡單,不信你看——

1. 注入resourcebundlemessagesource

在springmvc.xml添加用于國際化處理的beanresourcebundlmessagesource

<bean id="messagesource" class="org.springframework.context.support.resourcebundlemessagesource"> 

  <property name="basename" value="i18n"></property> 

</bean> 

這里property中的name是與注入類中的屬性名一直的,這里的value決定了后面國際化文件的名稱,記得是i18n,馬上你就會看到它的用法。

 2. 創建國際化文件

總共需要創建三個國際化屬性文件

學習SpringMVC——國際化+上傳+下載詳解

i18n.properties-默認的國際化文件

i18n_en_us.properties-用于英文環境的國際化文件

i18n_zh_cn.properties-用于中文環境的國際化文件

注意:這里為什么文件的名稱都是i18n開頭,因為在第一點的springmvc.xml配置文件中,配置的value值就是i18n

對于i18n.properties和i18n_en_us.properties文件的內容相同,如下

i18n.username=username

i18n.password=password 

i18n_zh_cn.properties

i18n.username=\u7528\u6237\u540d

i18n.password=\u5bc6\u7801 

3.新建頁面

分別新建兩個頁面,一個是i18n.jsp,顯示用戶名,同時有跳轉到i18n2.jsp的超鏈接,另一個是i18n2.jsp,顯示密碼,同時有跳轉到i18n.jsp的超鏈接。

i18n.jsp

<%@ page language="java" contenttype="text/html; charset=utf-8"

  pageencoding="utf-8"%>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>insert title here</title>

</head>

<body>

  <fmt:message key="i18n.username"></fmt:message><br><br>

   

  <a href="i18n2">i18n2</a>

</body>

</html> 

i18n2.jsp

<%@ page language="java" contenttype="text/html; charset=utf-8"

  pageencoding="utf-8"%>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>insert title here</title>

</head>

<body>

  <fmt:message key="i18n.password"></fmt:message><br><br>

   

  <a href="i18n">i18n</a>

</body>

</html> 

同時,顯然我們需要在index.jsp中添加一個入口,鏈接到i18n.jsp頁面,如下

<a href="i18n">i18n</a> 

為了能夠直接點擊就鏈接過去,而不需要通過handler處理并跳轉到視圖的套路,我們需要在springmvc.xml中添加標簽

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

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

這樣就能實現直接在地址欄中直接訪問到i18n.jsp和i18n2.jsp頁面了。

學習SpringMVC——國際化+上傳+下載詳解  

小坑:如果i18n.jsp和i18n2.jsp中的編碼方式采用默認“iso-8859-1”,就會出現頁面顯示亂碼

學習SpringMVC——國際化+上傳+下載詳解

當把編碼改為“utf-8”后,就能夠正常顯示

學習SpringMVC——國際化+上傳+下載詳解

以上是國際化這道菜的基礎做法,那么如果我還想做一道不用直接訪問i18n.jsp,而是經過handler處理后呈現的i18n.jsp,或者我還想做一道不用那么麻煩還要切換語言的國際化菜,有沒有可能,當然,接著看——

1.  注釋之前在springmvc.xml添加對于i18n.jsp直接訪問的標簽

<!-- 

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

--> 

2. 在hanlder處理類springmvctest中添加處理接口

@autowired

private resourcebundlemessagesource messagesource;

@requestmapping("/i18n")

public string testi18n(locale locale){

  string val = messagesource.getmessage("i18n.username", null, locale);

  system.out.println(val);

  return "i18n";

} 

注意這里注入了國際化處理類resourcebundlemessagesource,并使用其getmessage方法獲取國際化后的屬性值。

啟動tomcat服務可以看到

學習SpringMVC——國際化+上傳+下載詳解

那么如果根據自己的設定,在不同的語言環境中顯示相應語言的信息呢

1.  配置sessionlocaleresolver和localechangeinterceptor

<!-- 配置sessionlocaleresolver -->

<bean id="localeresolver" class="org.springframework.web.servlet.i18n.sessionlocaleresolver"></bean>

       

<!-- 配置localechangeinterceptor -->

<mvc:interceptors>

  <bean class="org.springframework.web.servlet.i18n.localechangeinterceptor"></bean>

</mvc:interceptors> 

這里的localechangeinterceptor主要用于將帶有locale信息的請求解析為一個locale對象,并得到一個localeresolver對象

之后這里的sessionlocalresolver就會將上面的localresolver對象轉化為session的屬性,并從中取出這個屬性,也就是locale對象,返回給應用程序。

 2. 在index.jsp中添加超鏈接

<a href="i18n?locale=zh_cn">中文</a>

<br><br>

<a href="i18n?locale=en_us">英文</a> 

這樣,我們就可以看到結果

學習SpringMVC——國際化+上傳+下載詳解

說完國際化,再來說說springmvc對于json的支持。

在傳統的開發過程中,我們的handler即controller層通常遵循需要轉向一個jsp視圖的套路;但是這樣的場景并不能滿足所有的要求,比如我們很多時候只需要返回數據即可,而不是一個jsp頁面。那么這時候spring mvc3的@responsebody和@responseentity就支持這樣的功能。controller直接返回數據(這里我們說說json數據),而不是直接指向具體的視圖。這里簡單的分別舉一個上傳和下載的例子。

 1. 文件上傳

1.1 使用jquery在index.jsp實現ajax請求

<%@ page language="java" contenttype="text/html; charset=utf-8"

  pageencoding="utf-8"%>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8">

<title>insert title here</title>

<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>

<script>

  $(function(){

    $("#testjson").click(function(){

      var url = this.href;

      var args = {};

      $.post(url, args, function(data){

        for(var i=0; i<data.length; i++){

          var id = data[i].id;

          var lastname = data[i].lastname;

          alert(id + ": " + lastname);

        }

      })

      return false;

    })

  })

</script>

</head>

<body>

<a href="emps">list all employees</a><br/><br/>

 

<a href="testjson" id="testjson">testjson</a>

</body>

</html> 

這里核心的就是用jquery寫的ajax請求

請求的url就是定義的href;

data為請求響應后返回的數據;

正常情況下,我們應該請求到所有員工的信息,并且通過這里的遍歷得到每一個員工的所有信息如id、lastname等

1.2. 這里我們需要引入三個jar包

  1. jackson-annotation-2.1.5.jar

  2. jackso-core-2.1.5.jar

  3. jackso-databind-2.1.5.jar

這三個主要是用于后面在返回數據的轉換中用到的。

 1.3. 在handler springmvctest中添加接口

@responsebody

@requestmapping("testjson")

public collection<employee> testjson(){

  return employeedao.getall();

} 

這里我的個人理解,就是將通過employeedao查詢到的所有的員工信息,作為響應返回給接口,并最終通過一系列處理得到一個json的數據形式,然后在前臺頁面中遍歷解析。而完成這一切就是歸功于注解@responsebody。

具體來說,是有內部的一些converter來做這些轉換的,在接口方法中打斷點,進入調試

學習SpringMVC——國際化+上傳+下載詳解

選擇dispatcherservlet,找到this->handleradapters->elementdata,在這個數組中找到requestmappinghandleradapter,點進去找到messageconverters,便可以看到總共有7個converters

學習SpringMVC——國際化+上傳+下載詳解

這里第7個mappingjackson2httpmessageconverter就是我們添加了以上三個jar包后才加載進來的converter。可以看出,這里有足夠過對于不同數據類型處理的轉換器。

  1.4 在index.jsp中添加鏈接

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

最終的上傳結果如下

學習SpringMVC——國際化+上傳+下載詳解

2. 文件下載

2.1 準備下載源

在webcontent下新建files目錄,放入aaa.txt,作為下載源 

2.2 在index.jsp添加超鏈接作為下載入口

<a href="testresponseentity" id="testjson">testresponseentity</a><br/> 

2.3 在handler springmvctest中添加接口

@requestmapping("testresponseentity")

public responseentity<byte[]> testresponseentity(httpsession session) throws ioexception{

  byte[] body = null;

  servletcontext servletcontext = session.getservletcontext();

  inputstream in = servletcontext.getresourceasstream("/files/aaa.txt");

  body = new byte[in.available()];

  in.read(body);

     

  httpheaders headers = new httpheaders();

  headers.add("content-disposition", "attachment;filename=aaa.txt");

  httpstatus statuscode = httpstatus.ok;

  responseentity<byte[]> response = new responseentity<>(body, headers, statuscode);

  return response;

} 

啟動tomcat,我們可以看到aaa.txt真的可以下載啦~~~

學習SpringMVC——國際化+上傳+下載詳解

好了,到此為止,我們都干了啥

1. 支持國際化

2. 文件上傳

3. 文件下載

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美人成在线视频 | 黄色在线 | 午夜视频网 | 国产二区视频 | 成人在线视频观看 | 九九资源站 | 精品福利片 | 国产免费av在线 | 夜夜爽99久久国产综合精品女不卡 | 日韩精品区 | 成人综合久久 | 亚洲经典一区 | 成人黄色片网站 | 91免费视频 | 亚洲综合无码一区二区 | 国产婷婷 | 波多野结衣中文字幕一区二区三区 | 毛片免费在线播放 | 三区在线视频 | 在线播放黄 | 免费啪啪网站 | 欧美成人视屏 | 亚洲网站在线观看 | 精品中文字幕一区二区三区av | 欧美一级在线 | 91中文字幕在线观看 | 午夜视频免费在线观看 | 久久99精品久久久久 | 亚洲高清视频在线 | 精品网站www| 久草电影网| 黄色av网站在线观看 | 欧美精品一区二区三区在线 | 久久国产精品一区二区 | 欧美日在线 | 日韩高清一区二区 | 在线中文字幕视频 | 亚洲网站视频 | 欧美日韩国产一区二区三区 | 亚洲精品成人av | 久久中文字幕一区二区三区 |