国际化springmvc

1、国际化(internationalization)
简称i18n,是一种让软件在开发阶段就支持多种语言的技术

2、springmvc实现动态国际化(中英双语)
(1)提供中英双语资源文件

例如i18n_en_US.properties:

yhzh.homePage=welcome to home page
yhzh.label=userName
yhmm.label=password
hello.label=hello {0}, my name is {1}

login.error.label=account or password error
i18n_zh_CN.properties:

yhzh.homePage=欢迎来到首页
yhzh.label=用户帐号
yhmm.label=用户密码
hello.label=你好{0}, 我叫{1}

login.error.label=帐号或密码错误
(2)通过ResourceBundleMessageSource加载资源文件(basenames属性)

  <!--1) 配置国际化资源文件 -->
  <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
	<property name="basenames">
		<list> 
                        <value>i18n</value>
		</list>
	</property>
   </bean>
  注1:必须叫messageSource、必须叫messageSource、必须叫messageSource!!!
  注2:可在开发阶段使用ReloadableResourceBundleMessageSource它能自动重新加载资源文件

(3)指定springmvc的语言区域解析器,由它来确定使用哪个语言

配置语言区域解析器


注:必须叫localeResolver、必须叫localeResolver、必须叫localeResolver

语言解析器的类型:AcceptHeaderLocaleResolver/SessionLocleResolver/CookieLocaleResolver
各解析器的相关说明:
AcceptHeaderLocaleResolver(基于操作系统):
Spring采用的默认区域解析器是AcceptHeaderLocaleResolver。它通过检验HTTP请求的accept-language头部来解析区域。这个头部是由用户的web浏览器根据底层操作系统的区域设置进行设定。请注意,这个区域解析器无法改变用户的区域,因为它无法修改用户操作系统的区域设置
SessionLocaleResolver(基于会话):
它通过检验用户会话中预置的属性来解析区域。如果该会话属性不存在,它会根据accept-language HTTP头部确定默认区域
CookieLocaleResolver(基于Cookie):
这个区域解析器所采用的Cookie可以通过cookieName和cookieMaxAge属性进行定制。
defaultLocale:默认的语言区域
cookieName:设置cookieName名称
cookieMaxAge:设置cookieName有效时间,单位秒
cookiePath:设置cookie可见的地址,默认是“/”即对网站所有地址都是可见的,如果设为其它地址,则只有该地址或其后的地址才可见

配置国际化操作拦截器,如果采用基于(Session/Cookie)则必需配置

mvc:interceptors

</mvc:interceptors>
通过标签输出内容,而非直接输出内容
a、使用springmvc的message标签输出:

<%@ taglib prefix=“t” uri=“http://www.springframework.org/tags” %>
<t:message code=“title”/>
b、使用jstl的fmt标签库的标签输出:

<%@ taglib prefix=“fmt” uri=“http://java.sun.com/jsp/jstl/fmt” %>
<t:message code=“title”/>
注1:为什么在index.jsp使用<t:message code=“user_name”/>会报错
原因是在web.xml中配置的DispatcherServlet的url-pattern为“/”,不会匹配访问.jsp的url,
所以直接访问首页并不会经过DispatcherServlet,导致无法读取到资源文件
解决方案:首页转发到/WEB-INF/jsp/login.jsp即可

注2:切换语言的关键代码(系统必须使用SessionLocaleResolver解析器)
     session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,Locale.CHINA)

后台代码获取国际化信息
a、后台通过springmvc的消息机制显示消息

b、通过RequestContext获得国际化的消息

RequestContext requestContext = new RequestContext(request);
String errorMsg = requestContext.getMessage(“login.error.label”);
System.out.println(“errorMsg:” + errorMsg);
3、springmvc的文件上传
(1)添加文件上传相关依赖

commons-fileupload commons-fileupload 1.3.3 (2)配置文件上传解析器(CommonsMultipartResolver) (3)表单提交方式为method="post" enctype="multipart/form-data" (4)文件项用spring提供的MultipartFile进行接收

(5)IO流读写文件

@RequestMapping("/upload")
public String upload(MultipartFile file, HttpServletRequest request){
    String filename = file.getOriginalFilename();
    String realPath = request.getServletContext().getRealPath(serverDir+filename);
    try {
        FileUtils.copyInputStreamToFile(file.getInputStream(),new File(realPath));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "i18n";
}

4、springmvc的文件下载
@RequestMapping("/download")
public String saveAs(HttpServletResponse response, HttpServletRequest request) {
String type=“image/jpeg”;
String name=“3.jpg”;
response.setContentType(type);
response.setHeader(“Content-Disposition”,“attachment;filename=” + name);//文件名
File servletFile=new File(request.getServletContext().getRealPath(serverDir + name));
try {
//用缓存流下载
BufferedInputStream bit=new BufferedInputStream(new FileInputStream(servletFile));
BufferedOutputStream bos=new BufferedOutputStream(response.getOutputStream());
copyByBuffered(bit, bos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

/**
 * 用缓存流下载(提高性能)
 * @author LJ
 * @Date 2018年10月15日
 * @Time 下午10:33:28
 * @param in
 * @param out
 * @throws IOException
 */
private void copyByBuffered(BufferedInputStream in, BufferedOutputStream out) throws IOException {
    byte[] bt=new byte[1024];
    int len=0;
    while((len=in.read(bt))!=-1) {
        out.write(bt, 0, len);
    }
    in.close();
    out.close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值