欢迎来到代码驿站!

JSP代码

当前位置:首页 > 软件编程 > JSP代码

ResourceBundle类在jsp中的国际化实现方法

时间:2021-06-20 08:48:23|栏目:JSP代码|点击:

今天第一次听说页面国际化这个词,所以查资料,顺便做了一个小页面,做做记录。

首先是两个资源文件,分别为msg_en_US.properties和msg_zh_CN.properties

 

显然中文字符是需要转换过来的。

这是jsp页面的代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@page import="java.util.*"%>
<!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>国际化</title>
  <style type="text/css">
  div{
   width: 300px;
   text-align: center;
   margin: 0 auto;
  }
  </style>
 </head>
 
 <body>
  <a href="/International/test.jsp?language=zh" rel="external nofollow" >简体中文</a>|<a href="/International/test.jsp?language=en" rel="external nofollow" >English</a>
  <%
   ResourceBundle bund=ResourceBundle.getBundle("msg",request.getLocale());
   String str=request.getParameter("language");
   if(str!=null){
    if("zh".equals(str)){
     bund=ResourceBundle.getBundle("msg",Locale.CHINA);
    }
    else if("en".equals(str)){
     bund=ResourceBundle.getBundle("msg",Locale.US);
    } 
   }
  %>
  <div>
   <h1><%=bund.getString("inf") %></h1>
   <table>
    <tr>
     <td><%=bund.getString("name") %>:</td>
     <td><input type="text" name="name"/></td>
    </tr>
    <tr>
     <td><%=bund.getString("sex") %>:</td>
     <td><input type="text" name="sex"/></td>
    </tr>
    <tr>
     <td><%=bund.getString("age") %>:</td>
     <td><input type="text" name="age"/></td>
    </tr>
    <tr>
     <td><%=bund.getString("tel") %>:</td>
     <td><input type="text" name="tel"/></td>
    </tr>
   </table><br/>
   <input type="submit" value="<%=bund.getString("submit")%>" name="submit"/>
  </div>
 </body>
</html>
 

结果图

 

上一篇:JSP由浅入深(6)―― JSP声明

栏    目:JSP代码

下一篇:Sun认为C#不会替代Java

本文标题:ResourceBundle类在jsp中的国际化实现方法

本文地址:http://www.codeinn.net/misctech/144924.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有