心得18--JavaWEB国际化类和jstl对应的标签案例分析

1.

locale类测试

package com.hbsi.demo;

import java.util.Locale;

 

publicclass Demo1 {

   publicstaticvoid main(String[] args) {

      Locale locale = Locale.getDefault();

      System.out.println(locale.getLanguage()+"-"+locale.getCountry());    

   }

}

国际化登录界面编写(此jsp只编写了中文和英文两种配置文件)

<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    

    <title>My JSP '4.jsp' starting page</title>

    

  </head>

  

  <body>

    <%

      ResourceBundle rb = ResourceBundle.getBundle("com.hbsi.mypropertity.myproperties",request.getLocale());

     %>

     <form>

     <%=rb.getString("username")%>  :  <inputtype="text"name="username"><br><br>

     <%=rb.getString("password")%> : <inputtype="password"name="password"><br><br>

     <inputtype="submit"value="<%=rb.getString("submit")%>">    

     <inputtype="reset"value="<%=rb.getString("reset")%>">

     </form>

  </body>

</html>

对应的标签编写的登录界面:案例一(setBundle标签):

<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"prefix="fmt" %>

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    

    <title>My JSP '4.jsp' starting page</title>

    

  </head>

  <fmt:setLocalevalue="en"/>

  <fmt:setBundlebasename="com.hbsi.mypropertity.myproperties"/>

  <body>

     <form>

     <fmt:messagekey="username"/>  :  <inputtype="text"name="username"><br><br>

     <fmt:messagekey="password"/> : <inputtype="password"name="password"><br><br>

     <inputtype="submit"value="<fmt:messagekey="submit"/>">    

     <inputtype="reset"value="<fmt:messagekey="reset"/>">

     </form>

  </body>

</html>

案列二(bundle标签):

<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"prefix="fmt" %>

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    

    <title>My JSP '4.jsp' starting page</title>

    

  </head>

  <fmt:bundlebasename="com.hbsi.mypropertity.myproperties">

  <body>

     <form>

     <fmt:messagekey="username"/>  :  <inputtype="text"name="username"><br><br>

     <fmt:messagekey="password"/> : <inputtype="password"name="password"><br><br>

     <inputtype="submit"value="<fmt:messagekey="submit"/>">    

     <inputtype="reset"value="<fmt:messagekey="reset"/>">

     </form>

  </body>

  </fmt:bundle>

</html>

2.

DateFormat类文件例子:

 package com.hbsi.demo;

 

import java.text.DateFormat;

import java.text.ParseException;

import java.util.Date;

import java.util.Locale;

 

public class Demo2 {

       public static void main(String[] args) throws ParseException {

              Date date = new Date();

              DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT,Locale.CHINA);

              System.out.println(df.format(date));

              System.out.println("--------------");

              DateFormat df1 = DateFormat.getTimeInstance(DateFormat.MEDIUM,Locale.CHINA);

              System.out.println(df1.format(date));

              System.out.println("-------------");

              DateFormat df2 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.FULL,Locale.CHINA);

              System.out.println(df2.format(date));

              System.out.println("--------------");

              DateFormat df3 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,Locale.ENGLISH);

              System.out.println(df3.format(date));

              System.out.println("--------------");

              

              String str = "12-11-23";

              DateFormat df4 = DateFormat.getDateInstance(DateFormat.SHORT,Locale.CHINESE);

              System.out.println(df4.parse(str));

              System.out.println("--------------");

              String str1 = "2012-10-25";

              DateFormat df5 = DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.CHINESE);

              System.out.println(df5.parse(str1));

       }

}

NumberFormat类文件

package com.hbsi.demo;

 

import java.text.NumberFormat;

import java.text.ParseException;

import java.util.Locale;

 

public class Demo3 {

       public static void main(String[] args) throws ParseException {

              int number = 120;

              //测试同一数值在不同国家输出的货币形式,这里需要注意的是如果系统不存在该国家的货币形式会以乱码输出(¤120.00)

              NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);

              System.out.println(nf.format(number));

              System.out.println("------------");

              NumberFormat nf5 = NumberFormat.getCurrencyInstance(Locale.US);

              System.out.println(nf5.format(number));

              System.out.println("------------");

              NumberFormat nf6 = NumberFormat.getCurrencyInstance(Locale.ENGLISH);

              System.out.println(nf6.format(number));

              System.out.println("------------");

              

              //测试百分比的输出,百分比跟数值的输出格式基本相同,比如下面的例子结果都是60%

              double number1 = 0.60;

              NumberFormat nf1 = NumberFormat.getPercentInstance(Locale.CHINA);

              System.out.println(nf1.format(number1));

              System.out.println("------------");

              NumberFormat nf2 = NumberFormat.getPercentInstance(Locale.US);

              System.out.println(nf2.format(number1));

              System.out.println("------------");

              

              //下面的两个例子是测试字符串转不同国家的数值格式,这里你的字符串是哪个国家的格式你转换的时候就要指明当地语言(Locale)

              String str = "¥120.00";

              NumberFormat nf3 = NumberFormat.getCurrencyInstance(Locale.CHINA);

              System.out.println(nf3.parse(str));

              System.out.println("------------");

              

              String str1 = "$100";

              NumberFormat nf4 = NumberFormat.getCurrencyInstance(Locale.US);

              System.out.println(nf4.parse(str1));

       }

}

标签例子:

<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"prefix="fmt" %>

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    

    <title>My JSP '7.jsp' starting page</title>

    

  </head>

  

  <body>

  <fmt:formatDatevalue="<%=new Date()%>" type="date"/><br>

  <fmt:formatDatevalue="<%=new Date()%>" type="time"/><br>

  <fmt:formatDatevalue="<%=new Date()%>" type="both"/><br>

  <hr>

  

  <fmt:parseDatevalue="2012-11-23"pattern="yyyy-MM-dd"/><br>

  <fmt:parseDatevalue="2012-11-23"type="date"/>

  <hr>

  

  <fmt:formatNumbervalue="120"type="currency"pattern=".00元"/><br>

  <fmt:formatNumbervalue="120"type="currency"pattern="$.0"/><br>

  <hr>

  

  <fmt:parseNumbervalue="120.00"type="number"/><br>

  <fmt:parseNumbervalue="120.00元"type="number"/><br>

 

  </body>

</html>

3.

MessageFormat类文件:

package com.hbsi.demo;

 

import java.text.MessageFormat;

import java.util.Date;

import java.util.Locale;

 

public class Demo4 {

       public static void main(String[] args) {

              String mes = "On {0}, a hurricance destroyed {1} houses and caused "

                     + "{2} of damage.";

              MessageFormat mf = new MessageFormat(mes,Locale.US);

              Object[] parame = {new Date(),99,10000000};

              System.out.println(mf.format(parame));

              System.out.println("------------");

              

              String mess = "At {0, time, short} on {0, date}, a hurricance destroyed{1} houses and caused {2, number, currency} of damage.";

              MessageFormat mf1 = new MessageFormat(mess,Locale.CHINA);

              Object[] parame1 = {new Date(),99,10000000};

              System.out.println(mf1.format(parame1));

       }

}

连接properties配置文件的类文件

package com.hbsi.demo;

 

import java.text.MessageFormat;

import java.util.Date;

import java.util.Locale;

import java.util.ResourceBundle;

 

public class Demo5 {

 

       /**

        * @param args

        */

       public static void main(String[] args) {

              //中文环境下

              //这句的Locale.CHINA设置message的显示格式

              ResourceBundle rb = ResourceBundle.getBundle("com.hbsi.mypropertity.myproperties",Locale.CHINA);

              String message = rb.getString("message");

              Object[] parame = {new Date(),99,1000000};

              //这句的Locale.CHINA设置占位符的显示格式

              MessageFormat mf = new MessageFormat(message,Locale.CHINA);

              System.out.println(mf.format(parame));

              System.out.println("------------");

              //英文环境下

              ResourceBundle rb1 = ResourceBundle.getBundle("com.hbsi.mypropertity.myproperties",Locale.US);

              String message1 = rb1.getString("message");

              Object[] parame1 = {new Date(),99,1000000};

              MessageFormat mf1 = new MessageFormat(message1,Locale.US);

              System.out.println(mf1.format(parame1));

 

       }

}

对应的标签文件:

<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"prefix="fmt" %>

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    

    <title>My JSP '8.jsp' starting page</title>

    

  </head>

  

  <body>

  <fmt:parseNumbervalue="99"type="number"var="num1"></fmt:parseNumber>

  <fmt:parseNumbervalue="1000000"type="number"var="num2"></fmt:parseNumber>

    <fmt:bundlebasename="com.hbsi.mypropertity.myproperties">

    <fmt:messagekey="message">

    <fmt:paramvalue="<%=new Date()%>"></fmt:param>

    <fmt:paramvalue="${num1}"></fmt:param>

    <fmt:paramvalue="${num2}"></fmt:param>

    </fmt:message>

    </fmt:bundle>

  </body>

</html>

 


 

posted @ 2012-11-23 16:29  yangkai_keven  阅读(171)  评论(0编辑  收藏  举报