国际化标签
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<fmt:setLocale value=“${pageContext.request.locale}”/>(页面的Locale)
<fmt:setBundle basename=“cn/itcast/web/jsp/config/hello”/>(资源文件基名)
<fmt:message key=“itcast.hello”>(资源文件key)
<fmt:param> value=“itcast.name”/>(资源文件占位符)
<fmt:requestEncoding value="UTF-8"/>设置POST请求消息的字符集编码
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <fmt:setLocale value="${pageContext.request.locale}"/><!--获取页面的Locale --> 6 <fmt:setBundle basename="cn.zengfansheng.config/message"/><!-- 资源文件基名--> 7 <body> 8 <table border="1" align="center"> 9 <caption> 10 <fmt:message key="login.title"/><!--资源文件中对应的key--> 11 </caption> 12 <tr> 13 <th><fmt:message key="login.username"/></th> 14 <td><input type="text" name="username"/></td> 15 </tr> 16 <tr> 17 <th><fmt:message key="login.password"/></th> 18 <td><input type="password" name="password"/></td> 19 </tr> 20 <tr> 21 <td colspan="2" align="center"> 22 <input type="submit" value='<fmt:message key="login.submit"/>'/> 23 </td> 24 </tr> 25 </table> 26 <hr/> 27 <fmt:message key="hello"> 28 <fmt:param value="jack"/><!-- 资源文件占位符{0}{1}--><fmt:param value="marry"/> 29 </fmt:message> 30 </body> 31 </html>
message_en_US.properties:
1 login.title=LOGIN IN 2 login.username=USERNAME 3 login.password=PASSWORD 4 login.submit=SUBMIT 5 language.title=CHOOSE LANGUAGE 6 language.chinese=CHINESE 7 language.english=ENGLISH 8 hello=hello{0}my name is{1}
message_zh_CN.properties:
1 login.title=\u7528\u6237\u767B\u5F55 2 login.username=\u7528\u6237\u540D 3 login.password=\u5BC6\u7801 4 login.submit=\u63D0\u4EA4 5 language.title=\u9009\u62E9\u8BED\u8A00 6 language.chinese=\u4E2D\u6587 7 language.english=\u82F1\u6587 8 hello=\u4F60\u597D{0}\u6211\u7684{1}
解决中文乱码问题:
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <body> 7 <form action="${pageContext.request.contextPath}/value.jsp" method="post"> 8 用户名:<input type="text" name="username"/> 9 <input type="submit" value="提交"/> 10 </form> 11 <hr/> 12 <fmt:requestEncoding value="UTF-8"/> 13 你提交的内容是:${param.username} 14 <%-- 15 String country = request.getParameter("country");//乱码 16 String tel = request.getParameter("tel"); 17 byte[] buf = country.getBytes("ISO8859-1");//字节数组 18 country = new String(buf,"UTF-8"); 19 out.write(country+"<br/>"); 20 out.write(tel); 21 --%> 22 </body> 23 </html>
by hacket