01: package com.mockrunner.example.struts;
02:
03: import javax.servlet.http.HttpServletRequest;
04:
05: import org.apache.struts.action.ActionErrors;
06: import org.apache.struts.action.ActionForm;
07: import org.apache.struts.action.ActionMapping;
08: import org.apache.struts.action.ActionMessage;
09: import org.apache.struts.action.ActionMessages;
10:
11: /**
12: * The <code>ActionForm</code> for the {@link AuthenticationAction}.
13: * The {@link #validate} method will check if an username and a password
14: * is present and generates the approriate <code>ActionErrors</code>.
15: * See {@link AuthenticationActionTest}.
16: */
17: public class AuthenticationForm extends ActionForm {
18: private String username;
19: private String password;
20:
21: public String getPassword() {
22: return password;
23: }
24:
25: public String getUsername() {
26: return username;
27: }
28:
29: public void setPassword(String password) {
30: this .password = password;
31: }
32:
33: public void setUsername(String username) {
34: this .username = username;
35: }
36:
37: public ActionErrors validate(ActionMapping mapping,
38: HttpServletRequest request) {
39: ActionErrors errors = new ActionErrors();
40: if (null == username || 0 == username.length()) {
41: addMissingValueError(errors, "username");
42: }
43: if (null == password || 0 == password.length()) {
44: addMissingValueError(errors, "password");
45: }
46: return errors;
47: }
48:
49: private void addMissingValueError(ActionErrors errors, String field) {
50: ActionMessage error = new ActionMessage("field.value.missing",
51: field);
52: errors.add(ActionMessages.GLOBAL_MESSAGE, error);
53: }
54: }
|