Struts框架(三)
Struts标签库的使用
1) 常用的struts tag lib
<bean:write>
<logic:empty>和<logic:notEmpty>
<logic:present>和<logic:notPresent>
<logic:iterator>
举例:
<body> <h1>测试迭代标签</h1> <hr> <table border="1"> <tr> <td>姓名</td> <td>年龄</td> <td>所属组</td> </tr> <logic:empty name="user_list"> <tr> <td colspan="3">没有符合条件的数据</td> </tr> </logic:empty> <logic:notEmpty name="user_list"> <logic:iterate id="user" name="user_list"> <tr> <td> <bean:write name="user" property="username"/> </td> <td> <bean:write name="user" property="age"/> </td> <td> <bean:write name="user" property="group.name"/> </td> </tr> </logic:iterate> </logic:notEmpty> </table> </body>
2)配置国际化支持,在struts-config.xml文件中加入如下配置:
<message-resources parameter="MessageResources" />
最好提供国际化资源文件(MessageResources.properties),将该文件放到src下
消息文本的国际化处理,共有三个步骤:
创建国际化消息
传递国际化消息
显示国际化消息
实例:
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm laf = (LoginActionForm)form;
String username = laf.getUsername();
String password = laf.getPassword();
ActionMessages messages = new ActionMessages();
try {
UserManager.getInstance().login(username, password);
//创建国际化消息文本
ActionMessage message = new ActionMessage("user.login.success", username);
//ActionMessage message = new ActionMessage("user.login.success", new Object[]{username});
messages.add("loginSuccess1", message);
ActionMessage message1 = new ActionMessage("user.login.success", username);
messages.add("loginSuccess2", message1);
//传递国际化消息文本
this.saveMessages(request, messages);
return mapping.findForward("success");
}catch(UserNotFoundException unfe) {
unfe.printStackTrace();
//创建国际化消息文本
ActionMessage message = new ActionMessage("user.not.found", username);
messages.add("error1", message);
//传递国际化消息文本
this.saveErrors(request, messages);
}catch(PasswordErrorException pee) {
pee.printStackTrace();
//创建国际化消息文本
ActionMessage message = new ActionMessage("user.password.error");
messages.add("error2", message);
//传递国际化消息文本
this.saveErrors(request, messages);
}
return mapping.findForward("error");
}
}
// 设置Locale
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 currentLocale = Locale.getDefault(); //默认情况下取得本机的语言
if ("zh".equals(lang)) {
currentLocale = new Locale("zh", "CN");
}else if("en".equals(lang)) {
currentLocale = new Locale("en", "US");
}
//request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);
this.setLocale(request, currentLocale);
return mapping.findForward("index");
}
}
国际化资源文件 MessageResources.properties
# -- standard errors -- errors.header=<UL> errors.prefix=<LI><font color="red"> errors.suffix=</font></LI> errors.footer=</UL> user.title=User Login user.username=User Name user.password=Password user.button.login=Login user.login.success={0},Login Success user.not.found=User Not Found,UserName[{0}] user.password.error=Password Error user.login.error=Login Error
MessageResources_en_US.properties 英文
errors.header=<UL> errors.prefix=<LI><font color="red"> errors.suffix=</font></LI> errors.footer=</UL> user.title=User Login user.username=User Name user.password=Password user.button.login=Login user.login.success={0},Login Success user.not.found=User Not Found,UserName[{0}] user.password.error=Password Error user.login.error=Login Error
MessageResources_zh_CN.properties 中文
# -- standard errors -- errors.header=<UL> errors.prefix=<LI><font color="red"> errors.suffix=</font></LI> errors.footer=</UL> user.title=\u7528\u6237\u767b\u5f55 user.username=\u7528\u6237 user.password=\u5bc6\u7801 user.button.login=\u767b\u5f55 user.login.success={0},\u767b\u5f55\u6210\u529f user.not.found=\u7528\u6237\u4e0d\u80fd\u627e\u5230\uff0c\u7528\u6237\u540d\u79f0=[{0}] user.password.error=\u5bc6\u7801\u9519\u8bef user.login.error=\u767b\u5f55\u5931\u8d25
struts-config.xml 配置文件
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-bean name="loginForm" type="com.bjsxt.struts.LoginActionForm"/> </form-beans> <action-mappings> <action path="/login" type="com.bjsxt.struts.LoginAction" name="loginForm" scope="request" validate="false" > <forward name="success" path="/login_success.jsp"/> <forward name="error" path="/login.jsp"/> </action> <action path="/changelang" type="com.bjsxt.struts.ChangeLanguageAction" //转换语言 > <forward name="index" path="/index.jsp"/> </action> </action-mappings> <message-resources parameter="res.MessageResources" /> //配置国际化资源文件必须要添加 </struts-config>