package onlyfun.caterpillar;ActionMessage中的字符串(例如 error.invalidUsername)对应于讯息档案中的键值。LoginAction.java可以修改如下:
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.Globals;
import org.apache.struts.util.MessageResources;
public class UserForm extends ActionForm {
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void reset(ActionMapping mapping,
HttpServletRequest req) {
username = null;
password = null;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if(getUsername() == null ||
getUsername().length() < 1) {
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("error.invalidUsername"));
}
if(getPassword() == null ||
getPassword().length() < 1) {
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("error.invalidPassword"));
}
return errors;
}
}
package onlyfun.caterpillar;最后配合<html:messages>标签,可以输出ActionMessages的内容,来修改一下fail.jsp:
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.commons.beanutils.PropertyUtils;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String username = (String)
PropertyUtils.getSimpleProperty(form, "username");
String password = (String)
PropertyUtils.getSimpleProperty(form, "password");
request.setAttribute("username", username);
if(username.equals("caterpillar") &&
password.equals("1234")) {
return mapping.findForward("helloUser");
}
ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("message.namePasswordMismatched")
);
addMessages(request, messages);
return mapping.findForward("loginFail");
}
}
<%@ taglib uri="/tags/struts-html" prefix="html" %><html:messages>的message属性如果不设定为true,会输出ActionErrors中所储存的讯息,Error代表的是一个操作方面的错误,例如错误操作导致使用者名称或密码为空(当然也许也是故意的)。
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<html>
<head>
<title>Sorry!</title>
</head>
<body>
<H1>
<html:messages id="error">
<bean:write name="error"/>
</html:messages>
<html:messages id="message" message="true">
<bean:write name="message"/>
</html:messages>
</H1>
<p>
<a href='/strutsapp/html/form.htm'>Login</a>
</body>
</html>
Locale locale = new Locale("zh", "CN"); setLocale(request, locale);如上设定,<html:messages>会找messages_zh_CN.properties中的简体中文讯息来显示。现在的问题是,那ActionForm中的讯息呢?来看一下setLocale()中的原始码您就知道怎么作了:
protected void setLocale(HttpServletRequest request,Locale locale) { HttpSession session = request.getSession(); if (locale == null) { locale = Locale.getDefault(); } session.setAttribute(Globals.LOCALE_KEY, locale); }所以如果您要一开始进行 ActionForm 验证时就使用区域化讯息,则可以在reset()或validate()方法中加入:
HttpSession session = request.getSession(); session.setAttribute(Globals.LOCALE_KEY, locale);
或者您可以在必要的地方加入以上这段讯息,像是Servlet Filter等地方,这就看您的需求而定了。