国际化
国际化(internationalization) 简称i18n,是一种让软件在开发阶段就支持多种语言的技术
springmvc实现动态国际化(中英双语)
1. 提供中英双语资源文件( i18n_en_US.properties、 i18n_zh_CN.properties)
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
2、可在开发阶段使用ReloadableResourceBundleMessageSource它能自动重新加载资源文件
3. 指定springmvc的语言区域解析器,由它来确定使用哪个语言
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
4.配置国际化操作拦截器,如果采用基于(Session/Cookie)则必需配置
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
5.5 通过标签输出内容,而非直接输出内容
配置国际化操作拦截器,如果采用基于(Session/Cookie)则必需配置
5.5 springmvc的message标签输出
<%@ taglib prefix="t" uri="http://www.springframework.org/tags" %>
<t:message code="title"/>
5.5 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)
5.6 后台代码获取国际化信息
@RequestMapping("/i18n")
public String i18n(String state, HttpSession session){
if("English".equals(state)){
session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.US);
}else{
session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.CHINA);
}
return "i18n";
}
注:需修改pom.xml将国际化资源文件输出到target文件夹
<resource>
<directory>src/main/resources</directory>
<includes>
<!--<include>jdbc.properties</include>-->
<include>*.properties</include>
<include>*.xml</include>
</includes>
</resource>
springmvc的文件上传
简单步骤及代码
1.添加文件上传相关依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
2. 配置文件上传解析器(CommonsMultipartResolver)
在 springmvc-servlet.xml 中配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 必须和用户JSP 的pageEncoding属性一致,以便正确解析表单的内容 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 文件最大大小(字节) 1024*1024*50=50M-->
<property name="maxUploadSize" value="52428800"></property>
<!--resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常-->
<property name="resolveLazily" value="true"/>
</bean>
3 表单提交方式为method="post" enctype="multipart/form-data"
<form action="${pageContext.request.contextPath }/student/upload/${sid}" enctype="multipart/form-data" method="post">
<input type="hidden" value="${sid}" name="sid"/>
<input type="file" name="file">
<input type="submit" value="上传">
</form>
4. 文件项用spring提供的MultipartFile进行接收
5. IO流读写文件
@RequestMapping("/upload/{sid}")
public String upload(MultipartFile file,HttpServletRequest request,@PathVariable("sid") Integer sid){
String fileName = file.getOriginalFilename();
String realPath = request.getServletContext().getRealPath(serverDir + fileName);
Student student = new Student();
student.setSid(sid);
student.setPhotoname(fileName);
//图片的类型
student.setPhototype(file.getContentType());
studentService.updateByPrimaryKeySelective(student);
try {
FileUtils.copyInputStreamToFile(file.getInputStream(),new File(realPath));
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/student/list";
}
另附下载
@RequestMapping("/download/{photoname}")
public String success(MultipartFile file, HttpServletRequest request, HttpServletResponse response, @PathVariable("photoname") String photoname){
System.out.println(photoname);
String name = photoname+".jpg";
String type = "image/jpeg";
String realPath = request.getServletContext().getRealPath(serverDir + name);
response.setContentType(type);
response.setHeader("Content-Disposition","attachment;filename=" + name);//文件名
/*
* 从服务器拿图片到本地
* 源:服务器
* 目的:jsp输出图片
*/
File serverFile = new File(realPath);
try {
//下载效率低
// FileUtils.copyFile(serverFile, response.getOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(serverFile));
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
copyBufferedStream(in, out);
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
/**
* 提高下载效率
* @param in
* @param out
*/
private void copyBufferedStream(BufferedInputStream in, BufferedOutputStream out) {
byte[] bbuf = new byte[1024];
int len = 0;
try {
while((len = in.read(bbuf))!=-1) {
out.write(bbuf, 0, len);
}
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}