Strust如何支持國(guó)際化?
(一) 頁(yè)面 ( jsp )靜態(tài)信息的 國(guó)際化
我們以登錄這個(gè)例子來(lái)說(shuō)明。
通過(guò)點(diǎn)擊中文或英文,實(shí)現(xiàn)登錄界面語(yǔ)言信息的改變
主要步驟:
1、創(chuàng)建國(guó)際化資源文件
*與上一篇中提到的創(chuàng)建方式一致
屬性文件內(nèi)容
MessagesBoundle_zn_CN.properties
login.form.field.username=\u7528\u6237 ----如果為GBK編碼,這里是“用戶名”
login.form.field.password=\u5BC6\u7801 “密碼”
login.form.button.login=\u767B\u5F55 “登錄”
我們看到并非為中文,而是unicode編碼。
這是采用JAVA_HOME/bin/native2ascii工具轉(zhuǎn)換的,不然會(huì)出現(xiàn)亂碼
MessagesBoundle_en_US.properties
login.form.field.username=user name
login.form.field.password=password
login.form.button.login=login
2、需要在struts配置文件中指定資源屬性文件的位置和名稱,如<message-resources parameter="MessageResources" />
*Parameter=國(guó)際化資源文件的basename即可
3、在JSP頁(yè)面中使用Struts標(biāo)簽庫(kù)來(lái)實(shí)現(xiàn)顯示,如<bean:message key=“key string”/>來(lái)輸出文本
*需要引入相應(yīng)的標(biāo)簽庫(kù)<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
利用這個(gè)原理,我們可以用編程的方式來(lái)手工切換整個(gè)應(yīng)用系統(tǒng)的語(yǔ)言
示例:
相關(guān)action
<action path="/changeLanguage" type="com.jialin.ChangeLanguageAction" scope="request"> <forward name="login" path="/index.jsp" /> </action>
public class ChangeLanguageAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String lang = request.getParameter("lang"); Locale locale = Locale.getDefault(); if ("zh".equals(lang)) { locale = new Locale("zh", "CN"); }else if ("en".equals(lang)) { locale = new Locale("en", "US"); } this.setLocale(request, locale);//將語(yǔ)言信息設(shè)置到session中 return mapping.findForward("login"); } }
登錄JSP
<body> <form action="login.do" method="post"> <bean:message key="login.form.field.username" /> <input type="text" name="name" /> <br /> <bean:message key="login.form.field.password" /> <input type="password" name="password" /> <input type="submit" value="<bean:message key="login.form.button.login"/>" /> </form> <a href="changeLanguage.do?lang=zh">中文</a> <a href="changeLanguage.do?lang=en">英文</a> </body>
頁(yè)面信息的國(guó)際化還可以給我們帶來(lái)另一個(gè)好處:我們可以通過(guò)修改國(guó)際化資源文件更改頁(yè)面顯示的內(nèi)容,今天叫登錄,明天可以改為開始等等。
(二)動(dòng)態(tài)
消息的
國(guó)際化
對(duì)于消息提示,異常信息等這些動(dòng)態(tài)信息,我們?nèi)绾螌?shí)現(xiàn)國(guó)際化呢?
我們依然用上面的例子,在登錄驗(yàn)證中加入國(guó)際化
更改MessagesBoundle_zn_CN.properties
errors.header=<UL>
errors.prefix=<font color="red"><LI>
errors.suffix=</LI></font>
errors.footer=</UL>
login.form.field.username=\u7528\u6237
login.form.field.password=\u5BC6\u7801
login.form.button.login=\u767B\u5F55
login.success={0},\u767B\u5F55\u6210\u529F --登錄成功
login.user.not.found=\u7528\u6237\u4E0D\u80FD\u627E\u5230\uFF0C\u7528\u6237\u540D\u79F0\=\u3010{0}\u3011 --用戶未找到,用戶=
login.password.error=\u5BC6\u7801\u9519\u8BEF --密碼錯(cuò)誤
更改MessagesBoundle_en_US.properties
errors.header=<UL>
errors.prefix=<font color="red"><LI>
errors.suffix=</LI></font>
errors.footer=</UL>
login.form.field.username=user name
login.form.field.password=password
login.form.button.login=login
login.success={0},Login Success
login.user.not.found=user not found,user name=[{0}]
login.password.error=password error
Struts配置文件
<struts-config> <!-- set ActionForm info--> <form-beans> <form-bean name="userForm" type="com.jialin.UserActionForm" /> </form-beans> <global-forwards> <forward name="login" path="/index.jsp" /> <forward name="success" path="/LoginSuccess.jsp" /> <forward name="fail" path="/LoginFail.jsp" /> </global-forwards> <action-mappings> <!-- Set path,action,actionform,scope,forward info --> <action path="/login" type="com.jialin.LoginAction" name="userForm" scope="request" validate="true" attribute="uf"> </action> <action path="/changeLanguage" type="com.jialin.ChangeLanguageAction" scope="request"> </action> </action-mappings> <message-resources parameter="com.jialin.resource.MessagesBoundle"></message-resources> </struts-config>
LoginAction
第一步:在這個(gè)LoginAction中,我們將根據(jù)業(yè)務(wù)邏輯需要獲取國(guó)際化消息文本ActionMessage,然后將他保存起來(lái)到ActionMessages里
第二步:保存這個(gè)ActionMessages,用于傳遞
首先我們要判斷要傳遞的消息是普通消息還是錯(cuò)誤消息?
*普通消息:即普通的消息文本
*錯(cuò)誤消息:即異常消息文本
本質(zhì)上,這兩種消息沒(méi)有什么區(qū)別,都是消息文本,但是如果一個(gè)頁(yè)面同時(shí)需要顯示普通的消息文本和錯(cuò)誤消息文本的時(shí)候,就需要進(jìn)行區(qū)分了,比如不同類型的消息文本可能要用不同的樣式來(lái)顯示
/** * 登錄action * @author jialin *作用:取得表單數(shù)據(jù),調(diào)用model層業(yè)務(wù)邏輯,返回轉(zhuǎn)向信息 */ public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UserActionForm userForm=(UserActionForm)form; String userName=userForm.getName(); int password=userForm.getPassword(); //這里不用我們手動(dòng)強(qiáng)轉(zhuǎn)類型了。 UserManage userManage=new UserManage(); User user=new User(); user.setName(userName); user.setPassword(password); ActionMessages messages = new ActionMessages(); try { userManage.ValidateUser(user); //創(chuàng)建國(guó)際化消息文本 ActionMessage message=new ActionMessage("login.success", userName); messages.add("login_message_1",message); //傳遞國(guó)際化消息,普通消息 this.saveMessages(request, messages); return mapping.findForward("success"); } catch (UserNotFoundException e) { //創(chuàng)建國(guó)際化消息文本 ActionMessage message=new ActionMessage("login.user.not.found", userName); messages.add("login_error_1",message); //傳遞國(guó)際化消息,異常消息 this.saveErrors(request, messages); }catch (PasswordErrorException e) { //創(chuàng)建國(guó)際化消息文本 ActionMessage message=new ActionMessage("login.password.error", userName); messages.add("login_error_2",message); //傳遞國(guó)際化消息,異常消息 this.saveErrors(request, messages); } return mapping.findForward("fail"); } }
UserManage
/** * MODEL層業(yè)務(wù)邏輯 * * @author jialin 判斷用戶是否合法 */ public class UserManage { public void ValidateUser(User user) { // 判斷用戶名密碼是否正確 if (!"jialin".equals(user.getName())) { //"用戶不存在!用戶名為:" + user.getName()"會(huì)被Struts用于填充占位符 throw new UserNotFoundException("用戶不存在!用戶名為:" + user.getName()); } else if (user.getPassword() != 123456) { //"密碼錯(cuò)誤“會(huì)被Struts用于填充占位符 throw new PasswordErrorException("密碼錯(cuò)誤"); } } }
自定義異常類PasswordErrorException,UserNotFoundException略
登錄失敗jsp
<body> <font color="red"> <html:messages id="msg" property="login_error_1"> <bean:write name="msg" /> </html:messages> </font> <font color="blue"> <html:messages id="msg" property="login_error_2"> <bean:write name="msg" /> </html:messages> </font> </body>
別忘記引入taglib
<%@ taglib uri="
http://struts.apache.org/tags-bean
" prefix="bean"%>
<%@ taglib uri="
http://struts.apache.org/tags-html
" prefix="html"%>
也可以用<html:errors />代替<font color="red"> <html:messages id="msg"property="login_error_1"><bean:write name="msg" /></html:messages> </font><font color="blue"> <html:messages id="msg"property="login_error_2"><bean:write name="msg" /></html:messages> </font>顯示異常信息
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
登錄成功jsp
<body> <html:messages id="msg" message="true"> <bean:write name="msg" /> </html:messages> </body>
效果就貼圖了,下篇接著說(shuō)struts對(duì)異常的處理
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
