1 16 package org.springframework.webflow.action; 17 18 import org.springframework.validation.BindException; 19 import org.springframework.validation.Errors; 20 import org.springframework.webflow.execution.RequestContext; 21 import org.springframework.webflow.execution.ScopeType; 22 23 40 public class FormObjectAccessor { 41 42 54 private static final String CURRENT_FORM_OBJECT_ATTRIBUTE = "currentFormObject"; 55 56 59 private static final String ERRORS_PREFIX = BindException.ERROR_KEY_PREFIX; 61 62 65 private RequestContext context; 66 67 71 public FormObjectAccessor(RequestContext context) { 72 this.context = context; 73 } 74 75 79 public static String getCurrentFormObjectName() { 80 return CURRENT_FORM_OBJECT_ATTRIBUTE; 81 } 82 83 87 public static String getCurrentFormErrorsName() { 88 return ERRORS_PREFIX + getCurrentFormObjectName(); 89 } 90 91 96 public Object getCurrentFormObject() { 97 Object formObject = getCurrentFormObject(ScopeType.REQUEST); 98 if (formObject != null) { 99 return formObject; 100 } 101 formObject = getCurrentFormObject(ScopeType.FLASH); 102 if (formObject != null) { 103 return formObject; 104 } 105 formObject = getCurrentFormObject(ScopeType.FLOW); 106 if (formObject != null) { 107 return formObject; 108 } 109 return getCurrentFormObject(ScopeType.CONVERSATION); 110 } 111 112 118 public Object getCurrentFormObject(ScopeType scopeType) { 119 return getFormObject(getCurrentFormObjectName(), scopeType); 120 } 121 122 128 public void setCurrentFormObject(Object formObject, ScopeType scopeType) { 129 scopeType.getScope(context).put(getCurrentFormObjectName(), formObject); 131 } 132 133 139 public Object getFormObject(String formObjectName, ScopeType scopeType) { 140 return scopeType.getScope(context).get(formObjectName); 141 } 142 143 151 public Object getFormObject(String formObjectName, Class formObjectClass, ScopeType scopeType) { 152 return scopeType.getScope(context).get(formObjectName, formObjectClass); 153 } 154 155 162 public void putFormObject(Object formObject, String formObjectName, ScopeType scopeType) { 163 scopeType.getScope(context).put(formObjectName, formObject); 164 setCurrentFormObject(formObject, scopeType); 165 } 166 167 173 public Errors getCurrentFormErrors() { 174 Errors errors = getCurrentFormErrors(ScopeType.REQUEST); 175 if (errors != null) { 176 return errors; 177 } 178 errors = getCurrentFormErrors(ScopeType.FLASH); 179 if (errors != null) { 180 return errors; 181 } 182 errors = getCurrentFormErrors(ScopeType.FLOW); 183 if (errors != null) { 184 return errors; 185 } 186 return getCurrentFormErrors(ScopeType.CONVERSATION); 187 } 188 189 195 public Errors getCurrentFormErrors(ScopeType scopeType) { 196 return getFormErrors(getCurrentFormObjectName(), scopeType); 197 } 198 199 205 public void setCurrentFormErrors(Errors errors, ScopeType scopeType) { 206 scopeType.getScope(context).put(getCurrentFormErrorsName(), errors); 207 } 208 209 217 public Errors getFormErrors(String formObjectName, ScopeType scopeType) { 218 return (Errors)scopeType.getScope(context).get(ERRORS_PREFIX + formObjectName, Errors.class); 219 } 220 221 227 public void putFormErrors(Errors errors, ScopeType scopeType) { 228 scopeType.getScope(context).put(ERRORS_PREFIX + errors.getObjectName(), errors); 229 setCurrentFormErrors(errors, scopeType); 230 } 231 } | Popular Tags |