1.首先编写了一个ChangeLanguageAction.java:
package
com.codedestiny.struts;

import
java.util.Locale;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.Globals;
import
org.apache.struts.action.Action;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;


public
class
ChangeLanguageAction
extends
Action
...
{


public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
String lan = request.getParameter("lan");
Locale currentLocale = null;

if ("zh".equals(lan))...{
currentLocale = new Locale("zh", "CN");

} else if ("en".equals(lan)) ...{
currentLocale = new Locale("en", "US");
}

if (currentLocale != null) ...{
request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);
}
return mapping.findForward("success");
}
}
<
a
href
="changelan.do?lan=zh"
>
切换成中文
</
a
><
br
>
<
a
href
="changelan.do?lan=en"
>
切换成英文
</
a
><
br
>
# -- standard errors --
errors.header
=
<UL>
errors.prefix
=
<LI>
errors.suffix
=
</LI>
errors.footer
=
</UL>
# -- validator --
errors.invalid
=
{
0
} is invalid.
errors.maxlength
=
{
0
} can not be greater than {
1
} characters.
errors.minlength
=
{
0
} can not be less than {
1
} characters.
errors.range
=
{
0
} is not in the range {
1
} through {
2
}.
errors.required
=
{
0
} is required.
errors.byte
=
{
0
} must be an byte.
errors.date
=
{
0
} is not a date.
errors.double
=
{
0
} must be an double.
errors.float
=
{
0
} must be an float.
errors.integer
=
{
0
} must be an integer.
errors.long
=
{
0
} must be an long.
errors.short
=
{
0
} must be an short.
errors.creditcard
=
{
0
} is not a valid credit card number.
errors.email
=
{
0
} is an invalid e-mail address.
# -- other --
errors.cancel
=
Operation cancelled.
errors.detail
=
{
0
}
errors.general
=
The process did not complete. Details should follow.
errors.token
=
Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title
=
Struts Blank Application
welcome.heading
=
Welcome!
welcome.message
=
To get started on your own application
,
copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's
"
webapp
"
folder (or equivalent)
,
and let your container auto-deploy the application. Edit the skeleton configuration files as needed
,
restart your container
,
and you are on your way! (You can find the application.properties file with this message in the /WEB-INF/src/java/resources folder.)
drp.user.name
=
User Name
drp.user.password
=
User Password
drp.user.login
=
Please Login
drp.user.login.success
=
Login Success!
drp.user.not.found
=
Not Found User
dpr.user.password.error
=
Password Error
drp.user.name
=
User Name
drp.user.password
=
User Password
drp.user.login
=
Please Login
drp.user.login.success
=
Login Success
!
drp.user.not.found
=
Not Found User
dpr.user.password.error
=
Password Error
package
com.codedestiny.struts;

import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;

import
org.apache.struts.action.Action;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.action.ActionMessage;
import
org.apache.struts.action.ActionMessages;


public
class
LoginAction
extends
Action
...
{


public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
LoginActionForm lag = (LoginActionForm)form;
String username = lag.getUsername();
String password = lag.getPassword();

ActionMessages messages = new ActionMessages();

try ...{
//调用助手类进行合法性检查
UserHelper.validate(username, password);
//创建消息
ActionMessage msg = new ActionMessage("drp.user.login.success");
messages.add("loginsuccess", msg);

//传递消息
this.saveMessages(request, messages);
return mapping.findForward("success");

}catch(UserNotFoundException unfe) ...{
unfe.printStackTrace();
ActionMessage msgError = new ActionMessage("drp.user.not.found");
messages.add("usernotfound", msgError);
//传递错误消息
this.saveErrors(request, messages);
//this.saveMessages(request, messages);

}catch(PasswordErrorException pee) ...{
pee.printStackTrace();
ActionMessage msgError = new ActionMessage("dpr.user.password.error");
messages.add("passworderror", msgError);
//传递错误消息
this.saveErrors(request, messages);
//this.saveMessages(request, messages);
}
return mapping.findForward("error");
}
}
<%
...
@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"
%>

<%
...
@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"
%>

<%
...
@ taglib prefix="html" uri="http://struts.apache.org/tags-html"
%>
<!
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=GB18030"
>
<
title
></
title
>
</
head
>
<
body
>
<
hr
>
<
html:errors
/>
<
form
action
="login.do"
>
<
bean:message
key
="drp.user.name"
/>
:
<
input
type
="text"
name
="username"
><
br
>
<
bean:message
key
="drp.user.password"
/>
:
<
input
type
="password"
name
="password"
><
br
>
<
hr
>
<
input
type
="submit"
name
="btn_login"
value
="<bean:message key="
drp.user.login"
/>
">
</
form
>
</
body
>
</
html
>
<%
...
@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"
%>

<%
...
@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"
%>

<%
...
@ taglib prefix="html" uri="http://struts.apache.org/tags-html"
%>
<!
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=GB18030"
>
<
title
></
title
>
</
head
>
<
body
>
<
html:messages
id
="msg"
message
="true"
property
="loginsuccess"
>
<
bean:write
name
="msg"
/>
</
html:messages
>
</
body
>
</
html
>
package
com.codedestiny.struts;
import
java.util.Locale;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.Globals;
import
org.apache.struts.action.Action;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;

public
class
ChangeLanguageAction
extends
Action
...
{

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
String lan = request.getParameter("lan");
Locale currentLocale = null;
if ("zh".equals(lan))...{
currentLocale = new Locale("zh", "CN");
} else if ("en".equals(lan)) ...{
currentLocale = new Locale("en", "US");
}
if (currentLocale != null) ...{
request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);
}
return mapping.findForward("success");
}
}
2.这样在显示层增加如下链接,通过点击即可实现切换Locale:
<
a
href
="changelan.do?lan=zh"
>
切换成中文
</
a
><
br
>
<
a
href
="changelan.do?lan=en"
>
切换成英文
</
a
><
br
>
3.MessageResources.properties(前面的代码和Struts默认的相同):
# -- standard errors --
errors.header
=
<UL>
errors.prefix
=
<LI>
errors.suffix
=
</LI>
errors.footer
=
</UL>
# -- validator --
errors.invalid
=
{
0
} is invalid.
errors.maxlength
=
{
0
} can not be greater than {
1
} characters.
errors.minlength
=
{
0
} can not be less than {
1
} characters.
errors.range
=
{
0
} is not in the range {
1
} through {
2
}.
errors.required
=
{
0
} is required.
errors.byte
=
{
0
} must be an byte.
errors.date
=
{
0
} is not a date.
errors.double
=
{
0
} must be an double.
errors.float
=
{
0
} must be an float.
errors.integer
=
{
0
} must be an integer.
errors.long
=
{
0
} must be an long.
errors.short
=
{
0
} must be an short.
errors.creditcard
=
{
0
} is not a valid credit card number.
errors.email
=
{
0
} is an invalid e-mail address.
# -- other --
errors.cancel
=
Operation cancelled.
errors.detail
=
{
0
}
errors.general
=
The process did not complete. Details should follow.
errors.token
=
Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title
=
Struts Blank Application
welcome.heading
=
Welcome!
welcome.message
=
To get started on your own application
,
copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's
"
webapp
"
folder (or equivalent)
,
and let your container auto-deploy the application. Edit the skeleton configuration files as needed
,
restart your container
,
and you are on your way! (You can find the application.properties file with this message in the /WEB-INF/src/java/resources folder.)
drp.user.name
=
User Name
drp.user.password
=
User Password
drp.user.login
=
Please Login
drp.user.login.success
=
Login Success!
drp.user.not.found
=
Not Found User
dpr.user.password.error
=
Password Error
4.MessageResources_zh_CN.properties用来支持中文(只贴出和上面不同的部分,中文已转为ISO-8859-1):
drp.user.name
=
User Name
drp.user.password
=
User Password
drp.user.login
=
Please Login
drp.user.login.success
=
Login Success
!
drp.user.not.found
=
Not Found User
dpr.user.password.error
=
Password Error
5.LoginAction.java:
package
com.codedestiny.struts;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.Action;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.action.ActionMessage;
import
org.apache.struts.action.ActionMessages;

public
class
LoginAction
extends
Action
...
{

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
LoginActionForm lag = (LoginActionForm)form;
String username = lag.getUsername();
String password = lag.getPassword();
ActionMessages messages = new ActionMessages();
try ...{
//调用助手类进行合法性检查
UserHelper.validate(username, password);
//创建消息
ActionMessage msg = new ActionMessage("drp.user.login.success");
messages.add("loginsuccess", msg);
//传递消息
this.saveMessages(request, messages);
return mapping.findForward("success");
}catch(UserNotFoundException unfe) ...{
unfe.printStackTrace();
ActionMessage msgError = new ActionMessage("drp.user.not.found");
messages.add("usernotfound", msgError);
//传递错误消息
this.saveErrors(request, messages);
//this.saveMessages(request, messages);
}catch(PasswordErrorException pee) ...{
pee.printStackTrace();
ActionMessage msgError = new ActionMessage("dpr.user.password.error");
messages.add("passworderror", msgError);
//传递错误消息
this.saveErrors(request, messages);
//this.saveMessages(request, messages);
}
return mapping.findForward("error");
}
}
6.login.jsp:
<%
...
@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"
%>

<%
...
@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"
%>

<%
...
@ taglib prefix="html" uri="http://struts.apache.org/tags-html"
%>
<!
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=GB18030"
>
<
title
></
title
>
</
head
>
<
body
>
<
hr
>
<
html:errors
/>
<
form
action
="login.do"
>
<
bean:message
key
="drp.user.name"
/>
:
<
input
type
="text"
name
="username"
><
br
>
<
bean:message
key
="drp.user.password"
/>
:
<
input
type
="password"
name
="password"
><
br
>
<
hr
>
<
input
type
="submit"
name
="btn_login"
value
="<bean:message key="
drp.user.login"
/>
">
</
form
>
</
body
>
</
html
>
7.login_success.jsp:
<%
...
@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"
%>

<%
...
@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"
%>

<%
...
@ taglib prefix="html" uri="http://struts.apache.org/tags-html"
%>
<!
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=GB18030"
>
<
title
></
title
>
</
head
>
<
body
>
<
html:messages
id
="msg"
message
="true"
property
="loginsuccess"
>
<
bean:write
name
="msg"
/>
</
html:messages
>
</
body
>
</
html
>
8.测试截图:
中文:

英文:

登录成功:
本文介绍了一个使用Struts框架实现应用国际化的具体案例。通过编写特定的动作类切换语言环境,并展示如何在不同语言环境下正确加载资源文件及显示页面。

被折叠的 条评论
为什么被折叠?



