1 package example; 2 3 import javax.servlet.http.HttpServletRequest ; 4 5 import org.apache.struts.action.*; 6 7 public class GuestbookEntryForm extends ActionForm { 8 private String name; 9 private String email; 10 private String message; 11 12 public GuestbookEntryForm() { 13 } 14 15 public String getEmail() { 16 return email; 17 } 18 19 public void setEmail(String email) { 20 this.email = normalizeString(email); 21 } 22 23 public String getMessage() { 24 return message; 25 } 26 27 public void setMessage(String message) { 28 this.message = normalizeString(message); 29 } 30 31 public void setName(String name) { 32 this.name = normalizeString(name); 33 } 34 35 public String getName() { 36 return name; 37 } 38 39 public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1) { 40 ActionErrors errs = new ActionErrors(); 41 if (name.length() == 0) { 42 errs.add("name", new ActionError("errors.required", "name")); 43 } 44 if (message.length() == 0) { 45 errs.add("message", new ActionError("errors.required", "message")); 46 } 47 return errs.size() == 0 ? null : errs; 48 } 49 50 private static String normalizeString(String s) { 51 if (s == null) return ""; 52 return s.trim(); 53 } 54 } | Popular Tags |