四步用SSM实现国际化

申明:这个功能是在SSM项目上增加的操作,所以前提要先有一个SSM项目。

步骤:

一、新建文件messages,在其下新建两个文件,文件名及内容如下

message_en文件,存储的是英文:

message_zh_CN文件,存储的是中文(unicode码):

不会转可以使用jdk自带的native2ascii转unicode码

方式一:直接运行native2ascii.exe文件

方式二:Ctrl+R输入cmd后在dos命令窗口输入native2ascii

事例如下:

二、在spring xml中配置国际化信息

三、jsp页面中

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>SSM实现国际化</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
  </head>
  <body>
    <center>
     <div>
      <h2><spring:message code="title"></spring:message></h2>
      <form action="doLogin" method="post">
       <table>
        <tr>
         <td><spring:message code="username"></spring:message>:</td>
         <td><input type="text" name="user"></td>
        </tr>
        <tr>
         <td><spring:message code="password"></spring:message>:</td>
         <td><input type="text" name="password"></td>
        </tr>
        <tr>
         <td></td>
         <td>
          <input type="submit" value="<spring:message code="login"></spring:message>">
          <input type="button" value="<spring:message code="register"></spring:message>">
         </td>
        </tr>
       </table>
      </form>
     </div>
    </center>
  </body>
</html>

注意:需要导入spring标签库

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

四、一般中文是默认语言,所以需要设置英文成默认语言,以win10为例

    设置 ---》 时间和语言 ---》左侧选择 区域和语言

没有英文的就需要添加语言

结构目录如下:

到此就实现了国际化,效果如下

中文状态下:

英文状态下

 

 

 


版权声明:本文为qq_39936292原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。