1 20 21 package com.methodhead.util; 22 23 import javax.servlet.http.HttpServletRequest ; 24 25 import org.apache.log4j.Logger; 26 27 import org.apache.struts.Globals; 28 import org.apache.struts.action.ActionMessage; 29 import org.apache.struts.action.ActionMessages; 30 import org.apache.struts.action.ActionError; 31 import org.apache.struts.action.ActionErrors; 32 import org.apache.struts.action.ActionMapping; 33 import org.apache.struts.action.ActionForward; 34 35 import org.apache.struts.util.MessageResources; 36 import com.methodhead.MhfException; 37 38 41 public class StrutsUtil { 42 43 45 47 49 51 60 public static void addMessage( 61 HttpServletRequest request, 62 String property, 63 String msgKey, 64 Object arg0, 65 Object arg1, 66 Object arg2 ) { 67 68 if ( property == null ) { 72 property = ActionMessages.GLOBAL_MESSAGE; 73 } 74 75 ActionMessages messages = 79 ( ActionMessages )request.getAttribute( Globals.MESSAGE_KEY ); 80 81 if ( messages == null ) { 82 messages = new ActionMessages(); 83 request.setAttribute( Globals.MESSAGE_KEY, messages ); 84 } 85 86 MessageResources messageResources = 90 ( MessageResources )request.getAttribute( Globals.MESSAGES_KEY ); 91 92 if ( messageResources == null ) 93 throw new MhfException( "Message resources is null." ); 94 95 String message = messageResources.getMessage( msgKey ); 96 97 if ( message == null ) 98 logger_.warn( msgKey + " resource message is not defined." ); 99 100 ActionMessage actionMessage = null; 104 if ( arg0 == null ) 105 actionMessage = new ActionMessage( msgKey ); 106 else if ( arg1 == null ) 107 actionMessage = new ActionMessage( msgKey, arg0 ); 108 else if ( arg2 == null ) 109 actionMessage = new ActionMessage( msgKey, arg0, arg1 ); 110 else 111 actionMessage = new ActionMessage( msgKey, arg0, arg1, arg2 ); 112 113 messages.add( property, actionMessage ); 117 } 118 119 128 public static void addError( 129 HttpServletRequest request, 130 String property, 131 String msgKey, 132 Object arg0, 133 Object arg1, 134 Object arg2 ) { 135 136 if ( property == null ) { 140 property = ActionErrors.GLOBAL_ERROR; 141 } 142 143 ActionErrors messages = 147 ( ActionErrors )request.getAttribute( Globals.ERROR_KEY ); 148 149 if ( messages == null ) { 150 messages = new ActionErrors(); 151 request.setAttribute( Globals.ERROR_KEY, messages ); 152 } 153 154 MessageResources messageResources = 158 ( MessageResources )request.getAttribute( Globals.MESSAGES_KEY ); 159 160 String message = messageResources.getMessage( msgKey ); 161 162 if ( message == null ) 163 logger_.warn( msgKey + " resource message is not defined." ); 164 165 ActionError actionMessage = null; 169 if ( arg0 == null ) 170 actionMessage = new ActionError( msgKey ); 171 else if ( arg1 == null ) 172 actionMessage = new ActionError( msgKey, arg0 ); 173 else if ( arg2 == null ) 174 actionMessage = new ActionError( msgKey, arg0, arg1 ); 175 else 176 actionMessage = new ActionError( msgKey, arg0, arg1, arg2 ); 177 178 messages.add( property, actionMessage ); 182 } 183 184 188 public static void addMessage( 189 HttpServletRequest request, 190 String msgKey, 191 Object arg0, 192 Object arg1, 193 Object arg2 ) { 194 195 addMessage( 196 request, ActionMessages.GLOBAL_MESSAGE, msgKey, arg0, arg1, arg2 ); 197 } 198 199 205 public static void addMessage( 206 HttpServletRequest request, 207 MessageResources messageResources, 208 String property, 209 String msgKey, 210 Object arg0 ) { 211 212 ActionMessages messages = 213 ( ActionMessages )request.getAttribute( Globals.MESSAGE_KEY ); 214 215 if ( messages == null ) 216 messages = new ActionMessages(); 217 218 String message = messageResources.getMessage( msgKey ); 219 if ( message == null ) 220 logger_.warn( msgKey + " resource message is not defined." ); 221 222 messages.add( 223 property, new ActionMessage( msgKey, arg0 ) ); 224 225 request.setAttribute( Globals.MESSAGE_KEY, messages ); 226 } 227 228 234 public static void addMessage( 235 HttpServletRequest request, 236 MessageResources messageResources, 237 String property, 238 String msgKey, 239 Object arg0, 240 Object arg1 ) { 241 242 ActionMessages messages = 243 ( ActionMessages )request.getAttribute( Globals.MESSAGE_KEY ); 244 245 if ( messages == null ) 246 messages = new ActionMessages(); 247 248 String message = messageResources.getMessage( msgKey ); 249 if ( message == null ) 250 logger_.warn( msgKey + " resource message is not defined." ); 251 252 messages.add( 253 property, new ActionMessage( msgKey, arg0, arg1 ) ); 254 255 request.setAttribute( Globals.MESSAGE_KEY, messages ); 256 } 257 258 264 public static void addMessage( 265 HttpServletRequest request, 266 MessageResources messageResources, 267 String property, 268 String msgKey, 269 Object arg0, 270 Object arg1, 271 Object arg2 ) { 272 273 ActionMessages messages = 274 ( ActionMessages )request.getAttribute( Globals.MESSAGE_KEY ); 275 276 if ( messages == null ) 277 messages = new ActionMessages(); 278 279 String message = messageResources.getMessage( msgKey ); 280 if ( message == null ) 281 logger_.warn( msgKey + " resource message is not defined." ); 282 283 messages.add( 284 property, new ActionMessage( msgKey, arg0, arg1, arg2 ) ); 285 286 request.setAttribute( Globals.MESSAGE_KEY, messages ); 287 } 288 289 293 public static ActionForward findForward( 294 ActionMapping mapping, 295 String name ) { 296 297 ActionForward forward = mapping.findForward( name ); 298 299 if ( forward == null ) 300 throw new MhfException( "Couldn't find \"" + name + "\" forward" ); 301 302 return forward; 303 } 304 305 309 public static Object getPolicy( 310 ActionMapping mapping ) { 311 312 try { 313 return 314 Class.forName( mapping.getParameter() ).newInstance(); 315 } 316 catch ( Exception e ) { 317 throw new MhfException( 318 "Unexpected exception while instantiating \"" + 319 mapping.getParameter() +"\":" + e.toString() ); 320 } 321 } 322 323 325 327 private static Logger logger_ = Logger.getLogger( "StrutsUtil" ); 328 } 329 | Popular Tags |