web应用国际化问题

1.项目清单


2.资源文件测试

package com.lcl.test;

import java.util.Locale;
import java.util.ResourceBundle;

public class Test {
public static void main(String[] args) {
	Locale l = new Locale("zh", "CN");
	Locale l2 = new Locale("en", "US");
	ResourceBundle rb = ResourceBundle.getBundle("mm", l2);
	String un = rb.getString("un");
	System.out.println(un);
	
}
}
package com.lcl.test;

import java.util.Locale;
import java.util.ResourceBundle;

public class Test01 {
public static void main(String[] args) {
	Locale lz = new Locale("zh", "CN");
	Locale lu = new Locale("en", "US");
	
	
	ResourceBundle rbz = ResourceBundle.getBundle("mm", lz);
	ResourceBundle rbu = ResourceBundle.getBundle("mm", lu);
	
	ResourceBundle r = ResourceBundle.getBundle("mm", Locale.CHINA);
	System.out.println(rbz.getString("un"));
	System.out.println(rbu.getString("un"));
	System.out.println(r.getString("un"));
}
}

3.普通java代码实现国际化(jsp)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <%
  Locale l = new Locale("zh","CN");
  ResourceBundle rb = ResourceBundle.getBundle("mm",l);
  
   %>
  <body>
 
  
  <form action="" method="post">
  
   <div style="float: right;">
  <select onchange="locl(this)">
  <option>中文</option>
  <option>English</option>
  </select>
  </div>
  </form>
  
  
    <%=rb.getString("un") %>:<input type="text"><br/>
    <%=rb.getString("pwd") %>:<input type="text">
  </body>
  <script type="text/javascript">
  function locl(slt){
  var f = slt.parentNode;
  f.submit;
  }
  
  
  </script>
</html>

4.struts2资源文件实现国际化

   ①struts.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="mm"/>
<constant name="struts.i18n.encoding" value="gbk"/>
<constant name="struts.action.extension" value="do,action" />
<package name="sys" namespace="/locl1" extends="struts-default">
<action name="locAction" class="com.lcl.test.Action">
<result name="ok">/index3.jsp</result>
</action>
</package>
</struts>

   ②页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="ss" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <body>
  <ss:text name="un"></ss:text>
  <ss:text name="pwd"></ss:text>
</body>
</html>

   ①web.xml配置

<filter>
  <filter-name>Struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>Struts2</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>

5.jstl标签fmt实现国际化(页面)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
<%
String l = "zh_CN";
 %>
  <body>
<fmt:setLocale value="<%=l %>"/>
<fmt:setBundle basename="mm"/>
<fmt:message key="un"></fmt:message>
<hr/>
<fmt:setLocale value="zh_CN"/>
<fmt:setBundle basename="mm" scope="session" var="b"/>
<fmt:message key="pwd" bundle="${sessionScope.b}"></fmt:message>
<hr/>
<fmt:bundle basename="mm">
<fmt:message key="un"></fmt:message>
</fmt:bundle>

  </body>

</html>



转载于:https://my.oschina.net/gaoguofan/blog/753220

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值