zoukankan      html  css  js  c++  java
  • javaWEB国际化(jsp中使用)

    在jsp页面中使用国际化方法,首先将jstl开源架包:jstl.jar,standard.jar导进去

    并在src目录下建立以test开头,.properties结尾的文件:test_en_US.properties,test_zh_CN.properties,文件内容分别为:

    date=date,salary=salary /// date=u65E5u671F,salary=u5DE5u8D44

    <%@page import="java.util.Locale"%>
    <%@page import="java.util.Date"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        
    <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
        
    <!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>Insert title here</title>
    </head>
    <body>
        <!-- 在jsp中设置,date和salary,并获取 -->
        <%
            Date date=new Date();
            request.setAttribute("date", date);
            
            double salary=321321.323;
            request.setAttribute("salary", salary);
        %>
        <br><br>
        
        date:<%=date %><br>
        salary:<%=salary %>
        
        <br><br>
        
        <!-- 利用jstl标签库,获取开头为test的.properties文件里的能容 ,
        其默认为中文的,即test_zh_CN.properties文件的值
        message为//MessageFormet:可以格式化模块字符串,formatDate和formetNumber...
        -->
        
        <fmt:bundle basename="test">
            <fmt:message key="date"></fmt:message>
            <fmt:formatDate value="${date }"/>
            <fmt:message key="salary"></fmt:message>
            <fmt:formatNumber value="${salary }"></fmt:formatNumber>
        </fmt:bundle>
        <br><br>
        
        <!-- 动态获取,显示中英文切换 -->
        
        <%
            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);
                }
            }
            
        %>
        
        <!-- 在session范围内设置locale,便于在jsp中获取locale -->
        <c:if test="${sessionScope.locale!=null }">
            <fmt:setLocale value="${sessionScope.locale }"/>
        </c:if>
        
        <fmt:setBundle basename="test"/>
        
        <fmt:message key="date"></fmt:message>
        <fmt:formatDate value="${date }"/>
        <fmt:message key="salary"></fmt:message>
        <fmt:formatNumber value="${salary }"></fmt:formatNumber>
        
        <br><br>
        
        <a href="index.jsp?code=en">英文显示</a>
        <br><br>
        
        <a href="index.jsp?code=zh">中文显示</a>
        <br><br>
    </body>
    </html>
  • 相关阅读:
    成为高级 React 开发你需要知道的知识点
    Socket 连接问题之大量 TIME_WAIT
    x == (x = y) 不等于 (x = y) == x ?
    「工具」三分钟了解一款思维导图工具:XMind Zen
    Touch Bar 废物利用系列 | 在触控栏上显示 Dock 应用图标
    vim中delete(backspace)键不能向左删除
    Vue2.0学习(四)--组件的继承与扩展
    quasar+cordova+zbar实现Android扫描条形码
    PWA技术理论+实战全解析
    分页请求时,有新数据加入时,下一页会出现重复数据问题
  • 原文地址:https://www.cnblogs.com/lxnlxn/p/5845785.html
Copyright © 2011-2022 走看看