1 /* 2 * Copyright (c) 2002-2006 by OpenSymphony 3 * All rights reserved. 4 */ 5 package com.opensymphony.xwork2; 6 7 import com.opensymphony.xwork2.util.ValueStack; 8 import com.opensymphony.xwork2.util.logging.Logger; 9 import com.opensymphony.xwork2.util.logging.LoggerFactory; 10 11 import java.io.Serializable; 12 import java.util; 13 14 15 /** 16 * Provides a default implementation for the most common actions. 17 * See the documentation for all the interfaces this class implements for more detailed information. 18 */ 19 public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable { 20 21 protected static Logger LOG = LoggerFactory.getLogger(ActionSupport.class); 22 23 private final transient TextProvider textProvider = new TextProviderFactory().createInstance(getClass(), this); 24 private final ValidationAwareSupport validationAware = new ValidationAwareSupport(); 25 26 27 public void setActionErrors(Collection<String> errorMessages) { 28 validationAware.setActionErrors(errorMessages); 29 } 30 31 public Collection<String> getActionErrors() { 32 return validationAware.getActionErrors(); 33 } 34 35 public void setActionMessages(Collection<String> messages) { 36 validationAware.setActionMessages(messages); 37 } 38 39 public Collection<String> getActionMessages() { 40 return validationAware.getActionMessages(); 41 } 42 43 /** 44 * @deprecated Use {@link #getActionErrors()}. 45 */ 46 @Deprecated public Collection<String> getErrorMessages() { 47 return getActionErrors(); 48 } 49 50 /** 51 * @deprecated Use {@link #getFieldErrors()}. 52 */ 53 @Deprecated public Map<String, List<String>> getErrors() { 54 return getFieldErrors(); 55 } 56 57 public void setFieldErrors(Map<String, List<String>> errorMap) { 58 validationAware.setFieldErrors(errorMap); 59 } 60 61 public Map<String, List<String>> getFieldErrors() { 62 return validationAware.getFieldErrors(); 63 } 64 65 public Locale getLocale() { 66 ActionContext ctx = ActionContext.getContext(); 67 if (ctx != null) { 68 return ctx.getLocale(); 69 } else { 70 LOG.debug("Action context not initialized"); 71 return null; 72 } 73 } 74 75 public boolean hasKey(String key) { 76 return textProvider.hasKey(key); 77 } 78 79 public String getText(String aTextName) { 80 return textProvider.getText(aTextName); 81 } 82 83 public String getText(String aTextName, String defaultValue) { 84 return textProvider.getText(aTextName, defaultValue); 85 } 86 87 public String getText(String aTextName, String defaultValue, String obj) { 88 return textProvider.getText(aTextName, defaultValue, obj); 89 } 90 91 public String getText(String aTextName, List<Object> args) { 92 return textProvider.getText(aTextName, args); 93 } 94 95 public String getText(String key, String[] args) { 96 return textProvider.getText(key, args); 97 } 98 99 public String getText(String aTextName, String defaultValue, List<Object> args) { 100 return textProvider.getText(aTextName, defaultValue, args); 101 } 102 103 public String getText(String key, String defaultValue, String[] args) { 104 return textProvider.getText(key, defaultValue, args); 105 } 106 107 public String getText(String key, String defaultValue, List<Object> args, ValueStack stack) { 108 return textProvider.getText(key, defaultValue, args, stack); 109 } 110 111 public String getText(String key, String defaultValue, String[] args, ValueStack stack) { 112 return textProvider.getText(key, defaultValue, args, stack); 113 } 114 115 public ResourceBundle getTexts() { 116 return textProvider.getTexts(); 117 } 118 119 public ResourceBundle getTexts(String aBundleName) { 120 return textProvider.getTexts(aBundleName); 121 } 122 123 public void addActionError(String anErrorMessage) { 124 validationAware.addActionError(anErrorMessage); 125 } 126 127 public void addActionMessage(String aMessage) { 128 validationAware.addActionMessage(aMessage); 129 } 130 131 public void addFieldError(String fieldName, String errorMessage) { 132 validationAware.addFieldError(fieldName, errorMessage); 133 } 134 135 public String input() throws Exception { 136 return INPUT; 137 } 138 139 public String doDefault() throws Exception { 140 return SUCCESS; 141 } 142 143 /** 144 * A default implementation that does nothing an returns "success". 145 * <p/> 146 * Subclasses should override this method to provide their business logic. 147 * <p/> 148 * See also {@link com.opensymphony.xwork2.Action#execute()}. 149 * 150 * @return returns {@link #SUCCESS} 151 * @throws Exception can be thrown by subclasses. 152 */ 153 public String execute() throws Exception { 154 return SUCCESS; 155 } 156 157 public boolean hasActionErrors() { 158 return validationAware.hasActionErrors(); 159 } 160 161 public boolean hasActionMessages() { 162 return validationAware.hasActionMessages(); 163 } 164 165 public boolean hasErrors() { 166 return validationAware.hasErrors(); 167 } 168 169 public boolean hasFieldErrors() { 170 return validationAware.hasFieldErrors(); 171 } 172 173 /** 174 * Clears field errors. Useful for Continuations and other situations 175 * where you might want to clear parts of the state on the same action. 176 */ 177 public void clearFieldErrors() { 178 validationAware.clearFieldErrors(); 179 } 180 181 /** 182 * Clears action errors. Useful for Continuations and other situations 183 * where you might want to clear parts of the state on the same action. 184 */ 185 public void clearActionErrors() { 186 validationAware.clearActionErrors(); 187 } 188 189 /** 190 * Clears messages. Useful for Continuations and other situations 191 * where you might want to clear parts of the state on the same action. 192 */ 193 public void clearMessages() { 194 validationAware.clearMessages(); 195 } 196 197 /** 198 * Clears all errors. Useful for Continuations and other situations 199 * where you might want to clear parts of the state on the same action. 200 */ 201 public void clearErrors() { 202 validationAware.clearErrors(); 203 } 204 205 /** 206 * Clears all errors and messages. Useful for Continuations and other situations 207 * where you might want to clear parts of the state on the same action. 208 */ 209 public void clearErrorsAndMessages() { 210 validationAware.clearErrorsAndMessages(); 211 } 212 213 /** 214 * A default implementation that validates nothing. 215 * Subclasses should override this method to provide validations. 216 */ 217 public void validate() { 218 } 219 220 @Override public Object clone() throws CloneNotSupportedException { 221 return super.clone(); 222 } 223 224 /** 225 * <!-- START SNIPPET: pause-method --> 226 * Stops the action invocation immediately (by throwing a PauseException) and causes the action invocation to return 227 * the specified result, such as {@link #SUCCESS}, {@link #INPUT}, etc. 228 * <p/> 229 * <p/> 230 * The next time this action is invoked (and using the same continuation ID), the method will resume immediately 231 * after where this method was called, with the entire call stack in the execute method restored. 232 * <p/> 233 * <p/> 234 * Note: this method can <b>only</b> be called within the {@link #execute()} method. 235 * <!-- END SNIPPET: pause-method --> 236 * 237 * @param result the result to return - the same type of return value in the {@link #execute()} method. 238 */ 239 public void pause(String result) { 240 } 241 242 }