ITeye博客

struts1 struts2 国际化

liuyuru · 2010-12-02 11:21 · Java web开发 · 企业架构 · Markdown

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并没有在这部分止步,而是在原有的简单易用的基础上,将其做得更灵活、更强大。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。