Springmvc国际化和文件上传(八)

来源:互联网 发布:淘宝冻结资金在哪里看 编辑:程序博客网 时间:2024/05/22 00:43

一、国际化

我们这里需要解决三个问题

  1. 在页面上能够根据浏览器语言设置的情况对文本(不是内容), 时间, 数值进行本地化处理
  2. 可以在 bean 中获取国际化资源文件 Locale 对应的消息
  3. 可以通过超链接切换 Locale, 而不再依赖于浏览器的语言设置情况

问题一

这里我们先解决第一个问题。
默认情况下SpringMVC根据Accept-Language参数判断客户端的本地化类型。
当接收到请求时,SpringMVC会在上下文中查找一个本地化解析器(LocaleResolver),找到后使用它获取请求所对应的本地化类型消息。

<bean id="messageSource"        class="org.springframework.context.support.ResourceBundleMessageSource">    <!-- 国际化信息所在的文件名 -->        <property name="basename" value="i18n"></property>      <!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称  -->                   <property name="useCodeAsDefaultMessage" value="true" />       </bean>

页面上直接使用jstl标签显示信息

<fmt:message key="i18n.user"></fmt:message>

配置国际化资源文件i18n.properties、i18n_en_US.properties

i18n.user=Useri18n.password=Password

i18n_zh_CN.properties

i18n.user=\u7528\u6237\u540Di18n.password=\u5BC6\u7801

问题二

在入参时候直接将Locale放到参数中,这样可以直接在 bean 中获取国际化资源文件 Locale 对应的消息。

@AutowiredResourceBundleMessageSource messageSource;@RequestMapping("/i18n")public String testI18n(Locale locale) {    String val = messageSource.getMessage("i18n.user", null, locale);    System.out.println(val);    return "i18n";}

问题三

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

页面上切换语言

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

SpringMVC还允许装配一个动态更改本地化类型的拦截器,这样通过指定一个请求参数就可以控制单个请求的本地化类型。

AcceptHeaderLocaleResolver:根据Http请求头Accept-Language参数确定本地化类型。默认使用。
CookieLocaleResolver:根据指定的Cookie值确定本地化类型。
SessionLocaleResolver:根据Session中特定的属性确定本地化类型。
LocaleChangeInterceptor:从请求参数中获取本次请求对应的本地化类型。

二、文件上传

SpringMVC为文件上传提供了直接支持,这种支持是通过即插即用的MultiResolver实现的。Spring 用Jakarta Commons FileUpload技术实现了一个MultiResolver实现类:CommonsMultipartResolver

SpringMVC 上下文默认没有装配 MultiResolver, 因此默认情况下上传文件比较麻烦,因此我们上传文件时需要配置MultiResolver。
需要将commons-io.jar和commons-fileupload.jar引入环境中。

<!--  上传:配置MultipartResovler --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    <!-- defaultEncoding:必须和jsp页面上的pageEncoding一致 -->    <property name="defaultEncoding" value="utf-8"></property>    <property name="maxUploadSize" value="1024"></property></bean>

页面表单

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

后台处理

@RequestMapping("/testUploadFile")public String testUploadFile(@RequestParam(value="desc") String desc,        @RequestParam("file") MultipartFile file) throws IOException {    System.out.println("desc: " + desc);    System.out.println("fileName: " + file.getOriginalFilename());    System.out.println("inputStream: " + file.getInputStream().available());    return "success";}
原创粉丝点击