JSP前端总结

来源:互联网 发布:js只能输入字母和汉字 编辑:程序博客网 时间:2024/06/05 08:47

一、C标签
    一】 <c:out value="..." default="..." escapeXml="true">
            escapeXml:将value中的特殊符号进行转义。如果不指定为false,那么默认值为true。
            value:输出到浏览器中的值
            default:value值找不到时的默认值
        code:

复制代码
       <c:out value="hello word!" />            <%                pageContext.setAttribute("username", "jack");            %>            <br />            用户名:<c:out value="${username}" /> <br />            用户名:<c:out value="${fdusername}" default="未知"/> <br />                        链接:<c:out value="<a href='#'>下载</a>" escapeXml="false" /> <br />            链接:<c:out value="<a href='#'>下载</a>" escapeXml="true" />  <br />            链接:<c:out value="<a href='#'>下载</a>" /> <br/>
复制代码

 


         
    二】<c:set var/value/scope/target/property>
         var: 有两成含义。变量名 和 存入域对象的名称。
         target和property是针对于javaBean的。

         code:

复制代码
           <c:set var="name" value="杰克" scope="page"/>            <c:out value="${name}" /> <br />            <jsp:useBean id="user" class="com.suse.domain.User" scope="page" />            <c:set target="${user}" property="userName" value="jack" />            <c:out value="${user.userName}" /> <br />                        <%                Map<String, Integer> map = new HashMap<String, Integer>();                pageContext.setAttribute("map", map);            %>            <c:set target="${map}" property="key"  value="jack"/>            <c:set target="${map}" property="value" value="7000"/>                        <c:out value="${map.key}" /> <br />            <c:out value="${map.value}" />    
复制代码

 



    三】<c:remove />
        <c:remove var="varName" [scope="{page|request|session|application}"]>
        code:

            <c:set var="name" value="jack" scope="page" />            <c:out value="${name}" default="未知" />            <c:remove var="name" scope="page"/>            <c:out value="${name}" default="未知" />


    四】<c:catch var="..">


            ...可能出错的代码
        </c:cath>
        var:会将错误信息封装在此

        code:
         <c:catch var="message">                    <%                    int a = 10/0;                %>            </c:catch>            <c:out value="${message}" default="无异常!" />


    五】<c:if var/test/scope >
            var:保存test属性的结果。true or false
            test:布尔值决定是否处理标签体中的内容
            scope:将test属性的结果保存到哪个域对象中。
        code:

             <c:if test="${25>16}" var="flag" scope="request">                25>16            </c:if>            <jsp:forward page="value.jsp" />


    六】<c:choose>
            <c:when test="...">
                    。。。
            </c:when>
            <c:otherwise>
                    。。。
            </c:otherwise>
        </c:choose>

        **${param.age}  得到地址栏参数age的值

        code:

复制代码
          <c:choose>                <c:when test="${param.age > 16}">                    你成年了                </c:when>                <c:otherwise>                    你还未成年                </c:otherwise>            </c:choose>
复制代码

 



    七】<c:forEach var/items/varStatus/begin/end/step> .. </c:forEach>

        var:将当前迭代到的元素保存到page这个域对象中的属性名称
        items:要进行迭代的的集合对象
        varStatus:是个page域对象。保存着此时迭代的信息。 
                    属性:first:是否为第一个(true or false)  index:当前下标
                          count:计数          end:是否为最后一个(true or false)

        code:         

复制代码
            <%            List<String> list = new ArrayList<String>();            list.add("jack");            list.add("merry");            list.add("berry");            list.add("xixi");            list.add("sisi");            request.setAttribute("list", list);            %>            <c:forEach var="item" items="${list}" varStatus="status">                ${status.index} - ${item} - ${status.first} - ${status.last} - ${status.count} <br />            </c:forEach>
复制代码


    八】 <c:url var/value/scope>
    九】 <c:param name/value>
    十】<c:redirect url> 重定向
            eg:

               <c:redirect url="/value.jsp">                    <c:param name="country" value="美国">                    <c:param name="tel" value="123">                </c:redirect>

    十一】中文通过GET方式传参的乱码解决问题

复制代码
             传统方式:            <a href="/day17/c/value.jsp?country=<%=URLEncoder.encode("中国", "UTF-8")%>&age=21">                传统方式传中文            </a>        JSTL标签方式:(<c:url>、<c:redirect>需要配合<c:param>联合使用)            <c:url    var="myUrl" value="/c/value.jsp" scope="page">                <c:param name="country" value="中国" />                <c:param name="age" value="15" />            </c:url>            <a href="${myUrl}"> JSTL标签传递中文方式 </a>
复制代码


二、EL表达式
    一】获取数据:(获取的对象必须是域对象中存储了的对象)
        普通变量
        JavaBean对象
            略
        Collection(Set和List)对象
        code:
          

复制代码
             <c:set var="name" value="jack" scope="page"/>            姓名:${name} <br />                        <%                List<String> list = new ArrayList<String>();                list.add("jack");                list.add("merry");                list.add("berry");                pageContext.setAttribute("list", list);            %>                    用户列表: <br />            ${list[0]},,,,, ${list[1]} ,,,,,, ${list[2]}        Map对象            code:            <%                Map<String, String> map = new LinkedHashMap<String, String>();                map.put("name", "xixi");                pageContext.setAttribute("map", map);            %>            <br />            ${map.key} —— ${map.value}                    or            ${map['name']}---${map['age']}            eg:            <c:forEach var="entry" items="${map}">                ${entry.key} : ${entry:value}            </c:forEach>        数组对象            eg:            <%                int[] intArray = {10, 20, 30};                pageContext.setAttribute("intArray", intArray);            %>            第一个元素:${intArray[0]}            第二个元素:${intArray[1]}
复制代码

        注意:
            1、必须要放到域对象中。
            2、若找不到,返回空白字符串,而不是null
        总结:
            ${user.address.city}
            ${user.like[0]} :获取有序集合某个位置的元素
            ${user.['key']} : 获得map集合中指定key的值

    二】执行运算
        1)关系运算符
            等于:     == 或 eq   eg: ${5 == 5} 、 ${5 eq 5}   
            不等于: != 或 ne   eg: ${4 != 5} 、 ${4 ne 5}
            小于:   < 或者 lt
            大于:     > 或者 gt
            小于等于:<= 或者 le
            大于等于:>= 或者 ge
        2)逻辑运算符
            交集:&& <==> and   eg: { A && B } <==> ${A and B}
            并集:|| <==> or    eg: { A || B}  <==> ${A or B}
            非:  !  <==> not    eg: {!A}       <==> ${not A}
        3)**重点使用:
            empty:检查变量是否为null或者"";
            二元表达式:${user!=null ? user.name : ""}
            eg:(用户类型:用户或者游客显示)
            <c:set var="username" value="jack" scope="session"/>
            欢迎${!empty username?username:"游客" }光临!

    三】获取web开发常用对象(隐式方法)
        1)jsp九大内置对象:
            page/pageContext -> request -> session -> application
            response/out
            config/exception

        2)web开发常用EL对象(总共11个):
            1>pageContext
                eg:
                    得到对应的web应用名称:
                        ${pageContext.request.contextPath}
            2>pageScope、requestScope、sessionScope、applicationScope
                eg:
                    <c:set var="username" value="jack" scope="page"/>
                    <c:set var="username" value="merry" scope="request">
                    用户名:
                        ${pageScope.username} <br/>  ==>jack
                        ${pageScope.username} <br/>  ==>merry
            3>param和paramValues(取得地址栏的请求参数)
                param:取得单个参数
                    eg:
                        <a href="${pageContext.request.contextPath}/param.jsp?username=jack&password=123">
                        ----------
                        用户名:${param.username}    =>jack
                        密码:${param.password}         =>123
                paramValues:取得所有的参数
                    eg:
                        <a href="${pageContext.request.contextPath}/param.jsp?like=sing&like=dancing&like=eat">
                        ---
                        爱好:${paramValues[0]}、${paramValues[1]}、${paramValues[2]}
            4)header和headerValues取得请求头字段
                eg:
                    浏览器相关信息:${header['User-Agent']}<br/>   ==>Mozlila/4.0...
                    字符编码相关信息:${headerValues['Accept-Encoding'][0]}  ==>gzip.deflate

            5)cookie取得Cookie的值
                eg:
                    <%
                        Cookie cookie  = new Cookie("username", "jack");
                        cookie.setMaxAge(5*60);
                        response.addCookie(cookie)
                    %>
                Cookie的名:${cookie.username.name} <br/>  ==>username
                Cookie的值:${cookie.username.value} <br/> ==>jack
            6)initParam获取web初始化参数
                WebName = ${initParam.webName} <br/>
                WebAuthor = ${initParam.webAuthor} <br/>

        补充:
            1)web初始化参数:配置web.xml获得
            <context-param>
                <param-name>webName</param-name>
                <param-value>day17</param-value>
            </context-param>
            <context-param>
                <param-name>webAuthor</param-name>
                <param-value>jack</param-value>
            </context-param>
            <context-param>
                <param-name></param-name>
                <param-value></param-value>
            </context-param>    
            2)jsp有9个内置对象

    四】EL自定义函数
        过程:
            1)写一个EL函数处理类,该类无需继承和实现任何类或接口
            2)在WEB-INF下创建一个tld文件,描述自定义函数和对应处理类的关系(函数声明一定要使用全类型)
            3)在需要使用的jsp文件中,通过<%@taglib%>指令引入对应的EL函数

        eg:将特殊字符进行转换EL自定义函数的实现

复制代码
             code:                public class Filter {                    public static String filter(String message) {                        ...转换代码....                    }                }            tld:                <tlib-version>1.0</tlib-version>                <short-name>skyel</short-name>                <uri>http://www.suse.com/jsp/jstl/el</uri>                <function>                    <name>filter</name>                    <function-class>com.suse.el.Filter</function-class>                    <function-signature>java.lang.String filter(java.lang.String)</function-signature> //函数的签名,参数和返回值一定要用全类型                </function>            jsp(使用过滤标签):                ${el:filter("<script>while(true){alert('haha');}</script>")}        eg2:验证函数:            ${el:checkPwd("123456", "123456")}            1,检查密码是否都填写了            2,检查密码是否为6位数字(正则表达式)            3,检查两次密码是否一致            code:                public class CheckFunction {                    //注意:此方法必须为static,否则会抛出空指针异常                    public static Boolean check(String password, String repassword) {                        if (password == null || password.trim().length() == 0) return false;                        if (password == null || repassword.trim().length() == 0) return false;                        if (!password.equals(repassword)) return false;                        return true;                    }                }            tld:            jsp(使用):
复制代码


    五】EL自定义函数和自定义标签有何不同?适合于什么样的场景?
        EL自定义函数:
            适合于与非web相关的应用,例如字符串相关的处理
            相关函数开发过程,参见《EL自定义函数开发步骤.txt》
        自定义标签:
            适用于与web相关的应用,因为标签中有pageContext对象

    六】EL内置函数(导入标签库functions)
        1)字符串转小写fn:toLowerCase("ABC") 、fn:toUpperCase("abc")
            eg:
                转大写:${fn:toUpperCase("abc")}
                转小写:${fn:toLowerCase("ABC")}
        2)删除字符串首位空格: trim
                eg:
                    ${fn:trim(" abc ")}
        3)求字符串长度: length
                eg:
                    ${fn:length(" abc ")}  ==>5
                    ${fn:length(fn.trim(" abc "))}  ==>3
        4)*分隔字符串,生成字符串数组: split
                eg:
                    ${cn:split("www.suse.com", ".")[0]} <br/> ==>www
                    ${cn:split("www.suse.com", ".")[1]} <br/> ===>suse
        5)*合并字符串数组为字符串
                eg:
                    <%
                        String[] sArr = {"www", "suse", "cn"};
                        pageContext.setAttribute("SARR", sArr);
                    %>
                    合并字符串:${fn:join(SARR, ".")} =>www.suse.cn
        6)子串在字符串的位置fn:indexOf
                eg:
                    ${fn.indexOf("www.suse.com", "suse")}  ==>4
                    ${fn.indexOf("www.suse.com", "sfds")}  ==>-1(找不到。返回-1)
        7)是否包含子串 fn:contains 和 fn:containsIgnoreCase
                eg:
                    ${fn:contains("www.suse.com", "suse")} ==>true
                    ${fn:contains("www.suse.com", "suSe")} ==>false
                    ${fn:containsIgnoreCase("www.suse.com", "suSe")} ==>true
        8)fn:startsWith 和 fn:endsWith  判断字符串是否按指定子串开始或者结束
                eg:
                    ${fn:startsWith("http://localhost:8080/.../a.jsp", "http")}  ==>true                        
                    ${fn:endsWith("http://localhost:8080/.../a.jsp", "jsp")}     ==>true
        9)替换字符串 fn:replaces
                eg:
                    ${fn:replaces("www suse com", " ", ".")}  ==>www.suse.com
        10)*截取子串:fn:substring 和 cn:subStringBefore  、 subStringAfter
                eg:
                    ${fn:substring("www.suse.com", 4, 8)} ==>suse
                    ${fn:substringAfter("www.suse.com", ".")} ==>suse.com
                    ${fn:subStringBefore("www.suse.com", ".")} ==>www.suse
三、国际化
    一】概念:
        1)不同的国家访问同一个jsp页面,显示的内容和样式是不同的,这样的jsp页面技术就叫做国际化。
        2)不能在jsp页面中写固定的内容
    二】国际化的特征
        1》固定数据:
            方法一:利用javase类:ResourceBundle
                ResourcesBundle:创建时指定资源包的基名和对应的Locale(语言和国家的集合:【通过浏览器的工具-->Internet选项-->语言-->添加】查看某个国家具体的集合信息。)
                步骤:
                    1)写一个本地化的jsp页面
                    2)再将本地化的jsp页面中的内容提出到资源包中,即:*.properties文件
                    3)通过ResourceBundle加载资源包
                注意:在找不到匹配的资源包时,会首先寻找操作系统语言标准的资源文件,若还没有找到,才会找默认的资源文件。
                eg1:
                    1)properties:
                    //en_US和zn_CN是按照标准写的
                    美国资源文件:    message_en_US.properties
                                    hello=Hello
                    中国资源文件:    message_zh_CN.properties
                                    hello=\u4F60\u597D
                    默认资源文件:    message.properties
                                    hello=xxxxx
                        注:当没有对应国家的配置文件时,先找操作系统所对应的语言的配置文件,再走默认的配置文件。
                    2)code:
                        //使用JavaAPI操作国际化资源
                        //a)绑定对应的资源文件
                        ResourceBundle rb = ResourceBundle.getBundle("com/suse/message", Locale.CHINA);
                        //b)从资源文件中获取对应的信息
                        String value = rb.getString("Hello");
                        //c)显示国际化信息
                        System.out.println("value=" + value);//中国
                        注意:Locale.US ==>hello
                              Locale.getDeafult() ==>你好
                        code: 

复制代码
                ResourceBundle rb = ResourceBundle.getBundle("com/suse/config/message", Locale.CHINA);                            String value = rb.getString("hello");                            System.out.println(value);//hello                            rb = ResourceBundle.getBundle("com/suse/config/message", Locale.US);                            value = rb.getString("hello");                            System.out.println(value);//你好                            rb = ResourceBundle.getBundle("com/suse/config/message", Locale.getDefault());                            System.out.println(value);//你好
复制代码

 

 


            方法二:web方案(通过ServletRequest可以获取到请求用户的Locale)
                    总结: 开发步骤:
                        1>写一个本地化的jsp页面
                        2>将本地化的jsp页面中的内容踢出到资源包中,即属性文件*.oproperties
                        3>通过ResourceBundle加载资源包

                    eg2:

复制代码
                    //properties                        //message_en_US.properties                        login.title=LOGIN IN                        login.username=USERNAME                        login.password=PASSWORD                        login.submit=SUBMIT                        //message_zh_CN.properties                        login.title=LOGIN IN                        login.username=USERNAME                        login.password=PASSWORD                        login.submit=SUBMIT                    //jsp                        <%                            ResourceBundle bundle = ResourceBundle.getBundle("com/suse/config/message",request.getLocale());                        %>                        <form action="" method="post">                            <table border="2" align="center">                                <caption><%= bundle.getString("login.title")%></caption>                                <tr>                                    <th><%= bundle.getString("login.username")%></th>                                    <td><input type="text" name="username"/></td>                                </tr>                                <tr>                                    <th><%= bundle.getString("login.password") %></th>                                    <td><input type="password" name="password"/></td>                                </tr>                                <tr>                                    <td colspan="2" align="center">                                        <input type="submit" value='<%= bundle.getString("login.submit")%>'/>                                    </td>                                </tr>                            </table>    
复制代码

 

 


            *方法三:国际化标签
                **需导入包:fmt
                注意:在web中。若请求找不到对应的Locale,即访问默认的.properties文件资源包。
              

复制代码
            //properties                        //message_en_US.properties                        login.title=LOGIN IN                        login.username=USERNAME                        login.password=PASSWORD                        login.submit=SUBMIT                        //message_zh_CN.properties                        login.title=LOGIN IN                        login.username=USERNAME                        login.password=PASSWORD                        login.submit=SUBMIT                    //jsp                        <%                            ResourceBundle bundle = ResourceBundle.getBundle("com/suse/config/message",request.getLocale());                        %>                        <form action="" method="post">                            <table border="2" align="center">                                <caption><%= bundle.getString("login.title")%></caption>                                <tr>                                    <th><%= bundle.getString("login.username")%></th>                                    <td><input type="text" name="username"/></td>                                </tr>                                <tr>                                    <th><%= bundle.getString("login.password") %></th>                                    <td><input type="password" name="password"/></td>                                </tr>                                <tr>                                    <td colspan="2" align="center">                                        <input type="submit" value='<%= bundle.getString("login.submit")%>'/>                                    </td>                                </tr>                            </table>
复制代码


            code:
                通过下拉框,由用户自己选择语言的实现原理:

                   

复制代码
            <script type="text/javascript">                        function refresh() {                            var myForm = document.getElementById("form");                            myForm.submit();                        }                    </script>                    <body>                        <fmt:setLocale value="${param.language}" />                        <fmt:setBundle basename="com/suse/config/message" />                        当前语言为:${param.language}                        <br />                        <fmt:message key="language.title" />                        <form id="form" action="/day17/practice/language.jsp" method="POST">                            <select name="language" onchange="refresh()">                                <option value="zh_CN"                                    ${('zh_CN'==param.language) ? 'selected' : ''}>                                    <fmt:message key="language.chinese" />                                </option>                                <option value="en_US"                                    ${('en_US'==param.language) ? 'selected' : ''}>                                    <fmt:message key="language.english" />                                </option>                            </select>                        </form>                    </body>
复制代码

 

 


            
        2》动态数据的国际化    
            一)format方法: 日期 ===> 字符串
                1)格式化日期时间————Calendar类(查文档)
                    Canlendar c = Canlendar.getInstance(Local.CHINA);
                    c.setTime(date);//设置Date对象
                    c.get(Canlendar.YEAR);//得到年份
                    c.get(Canlendar.MONTH);//得到月份
                    c.get(Canlendar.DAY_OF_MONTH);//得到一个月的第几天
                    c.get(Canlendar.DAY_OF_YEAR);//得到一年的第几天
                    c.get(Canlendar.DAY_OF_WEEK);//得到一周中的第几天
                    ...
                2)格式化日期时间————DateFormat类和SimpleDateFormat类(查文档)
                    日期:DataFormat dateFormat = DateFormat.getInstance(DateFormat.FULL, Locale.CHINA);
                          String time = dateForamt.format(new Date());
                        中国:                       美式:
                            SHORT:年-月-日                 SHORT:日/月/年      
                            DEFAULT:2014-12-11               DEFAULT:Dec 11, 2014
                            MEDIUM:2014-12-11               MEDIUM:Dec 11, 2014
                            FULL:2014年12月11日 星期四       FULL:Thursday, December 11, 2014
                        eg:   

复制代码
                       DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINA);                             String time = dateFormat.format(new Date());                             System.out.println(time);                                                        dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);                             time = dateFormat.format(new Date());                             System.out.println(time);                           ///////////////////////////////////////////////////////                              String time = "2014-12-11 下午09时58分07秒 CST";                              DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.FULL , Locale.CHINA);                              Date date = dateFormat.parse(time);                              Calendar c = Calendar.getInstance(Locale.CHINA);                              c.setTime(date);                              System.out.println(c.get(Calendar.YEAR));                              System.out.println(c.get(Calendar.MONTH));                              System.out.println(c.get(Calendar.DAY_OF_MONTH));
复制代码

 

 



                    时间:DateFormat dateFormat = DateFormat.getTimeInstance(DateForamt.FULL, Locale.CHINA)
                             String time = dateFormat.format(new Date());
                             结果省略.
                             DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.FULL , Locale.CHINA);
                          String date = dateFormat.format(new Date());
                          System.out.println(date); //2014-12-11 下午09时58分07秒 CST

                       使用SimpleDateFormat类,此类是DateFormat类的子类:
                           eg:
                               SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd", Locale.CHINA);
                               Date date = sdf.parse("2014-12-11");
                               ///////////////////////////////////////////////////////
                               SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
                               String str = sdf.format(new Date());
                               System.out.println(str);

                       总结:
                           1)DateFormat不适合对日期进行判断
                           2)DateFormat在多线程访问下不安全,需要使用ThreadLocale<DateFormat>类确保线程安全.
                二)parse方法: 字符串 ===> 日期
                    (略)
            **标签:<fmt:format> 和 <fmt:timeZone>
           

复制代码
            <%                //创建时区(默认+08:00时区,即北京时区)                TimeZone tz = TimeZone.getDefault();                pageContext.setAttribute("tz", tz);            %>            <jsp:useBean id="now" class="java.util.Date" scope="page" />            中国:            <fmt:timeZone value="${tz}">                <fmt:formatDate value="${now}" type="both" dateStyle="full" timeStyle="full"/>            </fmt:timeZone>            <hr />                        美国:            <fmt:timeZone value="GMT-08:00">                <fmt:formatDate value="${now}" type="both" dateStyle="full" timeStyle="full"/>            </fmt:timeZone>            <hr />            开罗:            <fmt:timeZone value="GMT+02:00">                <fmt:formatDate value="${now}" type="both" dateStyle="full" timeStyle="full"/>            </fmt:timeZone>            <hr />                        <fmt:formatDate value="${now}" pattern="yyyy-MM-dd-w-F-E hh:mm:ss" timeZone="GMT-08:00"/>
复制代码

 


            3)时区:timeZone
                 (略)

            4)格式化数字————NumberFormat类
                    format方法:将一个数值格式化为符合某个国家地区习惯的数值字符串
                    parse方法:将符合某个国家地区习惯的数值字符串解析为对应的数值
                    getIntegerInstance(Locale local)方法: 以本地的规范处理整数字符串信息为数字。
                    getCurrencyInstance(Locale locale)方法: 以本地的规范处理货币信息为数字。
                    getPercentInstance(Locale locale)方法: 以本地的规范处理百分比信息为数字。
                code:      

复制代码
            NumberFormat numberFormat = NumberFormat.getIntegerInstance(Locale.CHINA);                    Long number = numberFormat.parse("123").longValue();                    System.out.println("num = " + number);                    String str = numberFormat.format(123);                    System.out.println(str);                                        numberFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);                    str = numberFormat.format(123);                    System.out.println(str);//¥123.00                    number = numberFormat.parse("¥123.00").longValue();                    System.out.println(number);//123                                        numberFormat = NumberFormat.getPercentInstance(Locale.CHINA);                    Double i = numberFormat.parse("50.6%").doubleValue();                    System.out.println(i);//0.506                    str = numberFormat.format(0.506);                    System.out.println(str);//51%
复制代码

 

 


                 **标签:

复制代码
                   数字                        <fmt:formatNumber value="12.56" type="number"/>                        <hr/>                    货币                        <fmt:formatNumber value="1000" type="currency"/>                        <hr/>                    百分比                        <fmt:formatNumber value="0.25" type="percent"/>                        hr/>
复制代码

 


            5)格式化文本————MessageFormat类
                总结:
                    占位符的三种书写方式:
                        {argumentIndex}:0-9之间,表示要格式化对象数据在参数数组中的索引。
                        {argumentIndex, FormatType}: FormatType表示参数格式化类型
                        {argumentIndex, FormatType, FormatStyle}: FormatStyle格式化的5种样式,必须与格式化类型FormatType匹配
                code:
                

复制代码
               //模式字符串,即还有{n}占位符的字符串                    String pattern = "On {0}, a hurricance destroyed {1} houses and caused {2} of damage";                    MessageFormat messageFormat = new MessageFormat(pattern, Locale.US);                    String message = messageFormat.format(new Object[]{new Date(), 99, "$100000"});                    System.out.println(message);//On 12/12/14 11:00 PM, a hurricance destroyed 99 houses and caused $100000 of damage                    //其他样式的占位符                    pattern = "At {0, time, short} on {0, date}, {1} destroyed \n";                    messageFormat = new MessageFormat(pattern, Locale.US);                    message = messageFormat.format(new Object[]{new Date(), 99});                    System.out.println(message);//At 11:08 PM on Dec 12, 2014, 99 destroyed
复制代码

 

 

  补充:
    将中文转换成编码:
        1)可以通过eclipse的properties工具自动将中文转换成编码

        2)通过jdk的bin目录下的native2ascii工具:
            //将a.txt以gb2312的方式进行编码并传入message_zh_CN中
            进入cmd中:
                 d:\>native2ascii -encoding gb2312 a.txt message_zh_CN

原创粉丝点击