心得18--JavaWEB國際化類和jstl對應的標籤案例分析__大資料

來源:互聯網
上載者:User
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> 


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.