如何使用JSTL標簽做頁面資源國際化
2024-07-21 02:15:03
供稿:網友
 
  1 web應用開發,如何使用jstl 標簽做頁面資源國際化需解決問題描述:1 項目中的文本要實現國際化
  
  2 希望達到按模塊分開編寫國際化資源文件解決方案:
  
  jstl 標簽支持國際化的標簽為
  <fmt:bundle> <fmt:message> <fmt:setbundle><fmt:param>
  <fmt:bundle> 功能:指定消息資源使用的文件
  <fmt:message>功能:顯示消息資源文件中指定key的消息,支持帶參數消息
  <fmt:param> 功能:給帶參數的消息置參數值
  <fmt:setbundle> 功能:設置消息資源文件
  
  一個支持按模塊的多資源文件的國際化例子
  
  步驟1 :定義兩個資源文件,分別為
  resources/iamresources_zh_cn.properties, 內容為
  # 測試使用
  test.common.message = test.common.message1 {0}
  
  resources/usersynresources_zh_cn.properties 內容為
  # 測試使用
  test.usersyn.message = test.usersyn.message1 {0}
  
  步驟2:定義一個公用的jsp文件 includetld.jsp,其內容為
  <%-- struts taglib --%>
  <%@ taglib uri="/web-inf/struts-bean.tld" prefix="bean" %>
  <%@ taglib uri="/web-inf/struts-html.tld" prefix="html" %>
  <%@ taglib uri="/web-inf/struts-logic.tld" prefix="logic" %>
  <%@ taglib uri="/web-inf/struts-nested.tld" prefix="nested" %>
  <%@ taglib uri="/web-inf/struts-template.tld" prefix="template" %>
  <%@ taglib uri="/web-inf/struts-tiles.tld" prefix="tiles" %>
  <%-- jstl taglib --%>
  <%@ taglib prefix="c" uri="/web-inf/c.tld" %>
  <%@ taglib prefix="fmt" uri="/web-inf/fmt.tld" %>
  <%@ taglib prefix="x" uri="/web-inf/x.tld" %>
  <%@ taglib prefix="sql" uri="/web-inf/sql.tld" %>
  
  <%-- set common messageresource --%>
  <fmt:setbundle basename="resources.iamresources" var="commonbundle"/>
  <%-- set usersyn messageresource --%>
  <fmt:setbundle basename="resources.usersynresources" var="usersynbundle"/>
  
  步驟3 : 在需要國際化的jsp頁面使用按如下編寫
  <%@page contenttype="text/html; charset=utf-8"%>
  <%@include file="/includetld.jsp"%>
  
  <fmt:message key="test.common.message" bundle="${commonbundle}">
  <fmt:param value="liaowufeng"/>
  </fmt:message>
  
  <fmt:message key="test.usersyn.message" bundle="${usersynbundle}">
  <fmt:param value="liaowufeng"/>
  </fmt:message>