zoukankan      html  css  js  c++  java
  • JavaWeb-国际化之fmt标签

    JSTL的fmt标签完成的国际化,后面使用框架提供的标签完成

    实现中文、英文切换:

      》提供两个超简洁,携带不同的变量值

      》根据变量值确定对应的Locale对象

      》把Locale对象放入到session中

      》绑定Locale对应的资源文件。

    <%@ page import="java.util.Locale" %>
    <%@ page import="java.util.Date" %><%--
      Created by IntelliJ IDEA.
      User: dell
      Date: 2019/7/23
      Time: 16:36
      To change this template use File | Settings | File Templates.
    --%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>c
    </head>
    <body>
        <%
            Date date = new Date();
            request.setAttribute("date",date);
            request.setAttribute("salary",12345.67);
        %>
    
        <%
            String code = request.getParameter("code");
    
            if (code != null){
                if ("en".equals(code)){
                    session.setAttribute("locale", Locale.US);
                }else if ("zh".equals(code)){
                    session.setAttribute("locale",Locale.CHINA);
                }
            }
        %>
        <c:if test="${sessionScope.locale != null}">
            <fmt:setLocale value="${sessionScope.locale}"/>
        </c:if>
        <fmt:setBundle basename="i18n"/>
            <fmt:message key="key"/>:
            <fmt:formatDate value="${date }" dateStyle="FULL" />
            <fmt:message key="salary"/>:
            <fmt:formatNumber value="${salary }" type="currency"/>
        <br><br>
    
        <a href="xxx.jsp?code=en">English</a>
        <a href="xxx.jsp?code=zh">中文</a>
        
        
    </body>
    </html>
    

      

  • 相关阅读:
    详解JavaScript中的this
    java静态代理与动态代理简单分析
    BZOJ1263 [SCOI2006]整数划分
    BZOJ1258 [CQOI2007]三角形
    BZOJ1237 [SCOI2008]配对
    BZOJ1257 [CQOI2007]余数之和
    BZOJ1103 [POI2007]大都市
    BZOJ1061 [NOI2008]志愿者招募
    BZOJ1050 [HAOI2006]旅行
    BZOJ1055 [HAOI2008]玩具取名
  • 原文地址:https://www.cnblogs.com/yangHS/p/11232857.html
Copyright © 2011-2022 走看看