※上傳
官網說要有 commons-fileupload
※maven、gradle
compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.3' ------------------------------- <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency>
※
※spring 設定檔
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8" p:maxUploadSize="10240" />
※不需要 <mvc:annotation-driven />
※id 如果不是這個,就會出「Expected MultipartHttpServletRequest: is a MultipartResolver configured?」的錯,所以複製官網的就好
※controller
@RequestMapping(value = "test_upload", method=RequestMethod.POST) public String upload(@RequestParam("file") MultipartFile file) { System.out.println("類型=" + file.getContentType()); System.out.println("參數類型=" + file.getName()); System.out.println("名稱=" + file.getOriginalFilename()); System.out.println("大小=" + file.getSize()); try { System.out.println("大小=" + file.getBytes().length); System.out.println("InputStream=" + file.getInputStream()); } catch (IOException e) { e.printStackTrace(); } return "hello"; }
※不寫 @RequestParam,就會出「Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]: Specified class is an interface」的錯
※測試
<form action="ooo/xxx/test_upload.mvc" enctype="multipart/form-data" method="post"> <input type="file" name="file" /> <input type="reset" value="重置"> <input type="submit" value="提交"> </form>
※如果使用 GET ,就會出「The current request is not a multipart request」的錯
※國際化
※自動轉換
瀏覽器是什麼語言會自動轉換※properties
------------------ i18n.properties ------------------ hello=default hello mvc.lang=default value ------------------ i18n_en_US.properties ------------------ hello=hello mvc.lang=english {0}{1} ------------------ i18n_zh_CN.properties ------------------ hello=\u4F60\u597D mvc.lang=\u7B80\u4E2D {0}{1} ------------------ i18n_zh_TW.properties ------------------ hello=\u4F60\u597D mvc.lang=\u7E41\u4E2D {0}{1}
※hello=你好,mvc.lang=繁中、简中
※spring 設定檔
<context:component-scan base-package="controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n" p:defaultEncoding="UTF-8" /> <mvc:annotation-driven /> <mvc:view-controller path="ooo/xxx/next1.mvc" view-name="jump1" />
※連 id 都不能改
※頁面
------------------ index.jsp ------------------ <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <fmt:bundle basename="i18n"> <fmt:message key="hello" /> </fmt:bundle> <fmt:message key="hello" /> <a href="ooo/xxx/next1.mvc">next1</a> ------------------ jump1.jsp ------------------ <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <h1>jump1</h1> <fmt:message key="mvc.lang"> <c:choose> <c:when test="${not empty zero && not empty one}"> <fmt:param value="${zero}" /> <fmt:param value="${one}" /> </c:when> <c:otherwise> <fmt:param value="" /> <fmt:param value="" /> </c:otherwise> </c:choose> </fmt:message> <a href="next2.mvc">next2</a> ------------------ jump2.jsp ------------------ <h1>jump2</h1> ${msg}<br /> <fmt:message key="mvc.lang"></fmt:message>
※fmt:bundle basename 是用 jstl 的功能
※注意 index 是第一頁,這時 spring 還沒有連到國際化的檔案,所以只使用第 9 行,就只會出現「???hello???」,按 next1 會有 request,所以再來就不用「fmt:bundle basename」了
※jump1 看起來看多,其實只是要把「{0}{1}」變不見而已
※注意 jump1 要跳到 jump2 的超連結,不用再加 ooo/xxx
※controller
package controller; import java.util.Locale; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/ooo/xxx/") public class XxxAction { @Autowired private ResourceBundleMessageSource messageSource; @RequestMapping("next2") public ModelAndView next2(Locale locale) { String zero = null, one = null, suc = null; if (Locale.TAIWAN.equals(locale)) { zero = "對"; one = "嗎?"; suc = "成功"; } else if (Locale.PRC.equals(locale)) { zero = "对"; one = "吗?"; suc = "成功"; } else if (Locale.US.equals(locale)) { zero = "all"; one = " right?"; suc = "success"; } System.out.println(messageSource.getMessage("mvc.lang", new Object[] { zero, one }, locale)); ModelAndView mav = new ModelAndView(); mav.setViewName("jump2"); mav.addObject("msg", suc); mav.addObject("zero", zero); mav.addObject("one", one); return mav; } }
※注意替換 {0} 這種東西時,只有在 java 有用,顯示還是會將「{0}{1}」顯示出來,並不會在頁面替換,所以才有 zero、one 這兩個變數讓前端顯示
※手動轉換
頁面有超連結,想換什麼語言就換什麼語言,有三種,參考官網,這裡只寫 SessionLocaleResolver※頁面
<a href="ooo/xxx/btnChangeLang.mvc?locale=en_US">english</a> <a href="ooo/xxx/btnChangeLang.mvc?locale=zh_CN">简体中文</a> <a href="ooo/xxx/btnChangeLang.mvc?locale=zh_TW">繁體中文</a>
※jump1 和 jump2 的頁面和自動轉換一樣
※controller
@RequestMapping("btnChangeLang") public String btnChangeLang(Locale locale) { System.out.println(messageSource.getMessage("mvc.lang", null, locale)); return "jump1"; }
※jump1 有個超連結,會到 next2 方法,方法和自動轉換一樣
※spring 設定檔
<context:component-scan base-package="controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n" p:defaultEncoding="UTF-8" /> <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> </mvc:interceptors> <mvc:default-servlet-handler /> <mvc:annotation-driven />
※id localeResolver、messageSource 不能改
※和自動轉換比,增加了 SessionLocaleResolver 和攔截器 LocaleChangeInterceptor,最後刪除了 mvc:view-controller
沒有留言:
張貼留言