1.导入包
- jstl.jar
- standard.jar
2.在springmvc.xml中配置
<!-- 配置国际化资源文件 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="i18n"></property> </bean>
3.配置以下文件
i18n_en_US.properties
i18n.username=Username
i18n.password=Password
i18n_zh_CN.properties(实际上是用户名,密码)
i18n.username=u7528u6237u540D
i18n.password=u5BC6u7801
i18n.properties
i18n.username=Username
i18n.password=Password
springmvcTest.java
@RequestMapping("/springmvc") @Controller public class SpringmvcTest {
private static final String SUCCESS = "success"; @RequestMapping("/test") public String test() { System.out.println("RequestmMapping"); return SUCCESS; } }
index.jsp
<a href="springmvc/test">test</a>
success.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> <p>Success</p> <fmt:message key="i18n.username"></fmt:message> <br><br> <fmt:message key="i18n.password"></fmt:message> <br><br> </body> </html>
需要引入<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>。
启动服务器:
在IE浏览器中输入:http://localhost:8080/springmvc1/
点击:转到success.jsp
选择Inteernet选项--》语言--》添加english(美国) --》将该语言上移至第一位,刷新浏览器之后:
完成了国际化的操作。
说明:
(1) 若项目中使用了JSTL,则spingmvc会自动把视图由InternalResourceView转换为JstlView。
(2)使用Jstl的fmt标签则需要在springmvc中配置国际化资源文件。
(3)若希望直接响应通过springmvc渲染的页面,可以使用mvc:view-controller实现,下节在写。