1 18 19 package org.apache.struts.validator; 20 21 import java.io.Serializable ; 22 import java.util.Map ; 23 24 import javax.servlet.ServletContext ; 25 import javax.servlet.http.HttpServletRequest ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.commons.validator.Validator; 30 import org.apache.commons.validator.ValidatorException; 31 import org.apache.commons.validator.ValidatorResults; 32 import org.apache.struts.action.ActionErrors; 33 import org.apache.struts.action.ActionForm; 34 import org.apache.struts.action.ActionMapping; 35 36 50 public class ValidatorForm extends ActionForm implements Serializable { 51 52 55 private static Log log = LogFactory.getLog(ValidatorForm.class); 56 57 61 protected ValidatorResults validatorResults = null; 62 63 66 protected int page = 0; 67 68 72 public int getPage() { 73 return page; 74 } 75 76 80 public void setPage(int page) { 81 this.page = page; 82 } 83 84 96 public ActionErrors validate(ActionMapping mapping, 97 HttpServletRequest request) { 98 99 ServletContext application = getServlet().getServletContext(); 100 ActionErrors errors = new ActionErrors(); 101 102 String validationKey = getValidationKey(mapping, request); 103 104 Validator validator = Resources.initValidator(validationKey, 105 this, 106 application, request, 107 errors, page); 108 109 try { 110 validatorResults = validator.validate(); 111 } catch (ValidatorException e) { 112 log.error(e.getMessage(), e); 113 } 114 115 return errors; 116 } 117 118 125 public String getValidationKey(ActionMapping mapping, 126 HttpServletRequest request) { 127 128 return mapping.getAttribute(); 129 } 130 131 137 public void reset(ActionMapping mapping, HttpServletRequest request) { 138 super.reset(mapping, request); 139 page = 0; 140 validatorResults = null; 141 } 142 143 148 public ValidatorResults getValidatorResults() { 149 return validatorResults; 150 } 151 152 157 public void setValidatorResults(ValidatorResults validatorResults) { 158 this.validatorResults = validatorResults; 159 } 160 161 168 public Map getResultValueMap() { 169 return (validatorResults != null ? validatorResults.getResultValueMap() : null); 170 } 171 } 172 | Popular Tags |