package onlyfun.caterpillar;主要看看validate()方法,在这边从request中取得了 MessageResources ,这是为了能取得讯息资源文件中的讯息设定,程序中使用一个Map对象来记录我们所发现的错误讯息,最后将之设定给request。
import java.util.*;
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) {
Map errModel = new HashMap();
MessageResources messageResources =
(MessageResources) request.getAttribute(
Globals.MESSAGES_KEY);
if(getUsername() == null ||
getUsername().length() < 1) {
String msg =
messageResources.getMessage(
"error.invalidUsername");
errModel.put("invalidUsername", msg);
}
if(getPassword() == null ||
getPassword().length() < 1) {
String msg =
messageResources.getMessage(
"error.invalidPassword");
errModel.put("invalidPassword", msg);
}
if(errModel.get("invalidUsername") == null &&
errModel.get("invalidPassword") == null) {
// no error happened
// return null to proceed the Action
return null;
}
else {
request.setAttribute("errors", errModel);
// fake codes, just tell RequestProcessor
// not to invoke Action
ActionErrors errors = new ActionErrors();
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage(""));
return errors;
}
}
}
<?xml version="1.0" encoding="ISO-8859-1" ?>您必须设定<action>的validate属性为true,这样RequestProcessor才会执行validate()方法, <action>的input属性则是让您设定当验证错误时,应该导向的页面。
<!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="userForm"
type="onlyfun.caterpillar.UserForm"/>
</form-beans>
<action-mappings>
<action
path="/login"
type="onlyfun.caterpillar.LoginAction"
name="userForm"
validate="true"
input="/WEB-INF/pages/fail.jsp">
<forward
name="helloUser"
path="/WEB-INF/pages/hello.jsp"/>
<forward
name="loginFail"
path="/WEB-INF/pages/fail.jsp"/>
</action>
</action-mappings>
<message-resources parameter="resources/messages"/>
</struts-config>
package onlyfun.caterpillar;这边使用了PropertyUtils辅助类别,这可以很方便的帮您取出窗体对象中的属性。这边还需要一个讯息档案来管理讯息:
import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.util.MessageResources;
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");
}
Map msgModel = new HashMap();
MessageResources messageResources =
(MessageResources) getResources(request);
msgModel.put("namePasswordMismatched",
messageResources.getMessage(
"message.namePasswordMismatched"));
request.setAttribute("messages", msgModel);
return mapping.findForward("loginFail");
}
}
# errors来看看fail.jsp,这边并没有使用Struts标签,所以只要如下取出讯息即可:
error.invalidUsername=使用者名称不得为空!
error.invalidPassword=密码不得为空!
# messages
message.namePasswordMismatched=使用者名称或密码输入错误!
<html>form.htm与hello.jsp就使用 使用 ActionForm 中介绍过的即可。
<head>
<title>Sorry!</title>
</head>
<body>
<H1>
${errors["invalidUsername"]}<br>
${errors["invalidPassword"]}<br>
${messages["namePasswordMismatched"]}
</H1>
<p>
<a href='/strutsapp/html/form.htm'>Login</a>
</body>
</html>