struts1 struts2 國際化

struts1國際化

1,在struts-cofig.xml中進行配置

  <message-resources parameter="action/ApplicationResource"></message-resources>

2,在I18n.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8" import="java.util.Locale"%>

<!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>

<script language="javascript">

function changeLanguage()

{

document.myform.submit();

}

</script>

</head>

<% 

String language=request.getParameter("language");

if(language==null)

   language=(String)session.getAttribute("language");

if(null==language||"".equals(language))

language="chinese";

session.setAttribute("language",language);

 

if(language.equals("chinese"))

session.setAttribute("org.apache.struts.action.LOCALE", Locale.CHINESE);

else if(language.equals("english"))

    session.setAttribute("org.apache.struts.action.LOCALE", Locale.ENGLISH);

%>

<body>

<form name=myform method="post">

<select name="language" onchange="changeLanguage()">

<option value="chinese" <%=language.equals("chinese")?"selected":"" %>>中文</option>

<option value="english" <%=language.equals("english")?"selected":"" %>>英文</option>

</select>

</form>

</body>

</html>

3,在頁面輸出時:<bean:message key="相應資源文件的key"/>

 

 

 

struts2的國際化

1,在struts.xml中配置

<constant name="struts.custom.i18n.resources" value="com/lxit/ApplicationResource"></constant>

2,在I18n.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8" import="java.util.Locale"%>

    <%@taglib prefix="s" uri="/struts-tags"%>

<!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>

<script language="javascript">

function changeLanguage()

{

document.myform.submit();

}

</script>

</head>

<%

Locale locale=(Locale)session.getAttribute("WW_TRANS_I18N_LOCALE");

if(locale==null)

locale=Locale.CHINESE;

 

%>

<body>

<form name=myform method="post">

<select name="request_locale" onchange="changeLanguage()">

<option value=<%=Locale.CHINESE%> <%=locale.getLanguage().equals("zh")?"selected":""%>>中文</option>

<option value=<%=Locale.ENGLISH%> <%=locale.getLanguage().equals("en")?"selected":""%>>英文</option>

</select>

</form>

</body>

</html>


3在jsp頁面輸出時:

<s:text name="delete">

或<s:textfield label="%{getText('id')}"  readonly="true" name="相應資源文件的key"></s:textfield><br>

 

 

不過,所謂“沒有最好,只有更好”。Struts 2.0並沒有在這部分止步,而是在原有的簡單易用的基礎上,將其做得更靈活、更強大。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章