1 20 21 package com.methodhead.tag; 22 23 import java.io.IOException ; 24 import java.util.Iterator ; 25 import javax.servlet.jsp.tagext.TagSupport ; 26 27 import org.apache.struts.Globals; 28 import org.apache.struts.action.ActionErrors; 29 import org.apache.struts.action.ActionError; 30 import org.apache.struts.util.MessageResources; 31 import org.apache.commons.lang.exception.ExceptionUtils; 32 33 public class InvalidTag 34 extends 35 TagSupport { 36 37 39 41 43 45 public int doStartTag() { 46 try { 47 invalid_ = false; 48 49 ActionErrors errors = 50 ( ActionErrors )pageContext.getRequest().getAttribute( 51 Globals.ERROR_KEY ); 52 53 MessageResources messages = 54 ( MessageResources )pageContext.getRequest().getAttribute( 55 Globals.MESSAGES_KEY ); 56 57 if ( property_ == null ) 58 property_ = ActionErrors.GLOBAL_ERROR; 59 60 String style = " class=\"formcell\""; 61 String message = ""; 62 63 if ( ( errors != null ) && 64 ( messages != null ) && 65 ( property_ != null ) ) { 66 67 Iterator iter = errors.get( property_ ); 68 69 if ( iter.hasNext() ) { 70 71 invalid_ = true; 72 pageContext.getOut().println( "<div class=\"invalid\"><div class=\"invalid-message\">" ); 73 74 while ( iter.hasNext() ) { 75 ActionError error = ( ActionError )iter.next(); 76 77 pageContext.getOut().println( 78 messages.getMessage( error.getKey(), error.getValues() ) ); 79 } 80 81 pageContext.getOut().println( "</div>" ); 82 } 83 } 84 } 85 catch ( IOException e ) { 86 throw new RuntimeException ( 87 "Unexpected IOException: " + ExceptionUtils.getStackTrace( e ) ); 88 } 89 90 return EVAL_BODY_INCLUDE; 91 } 92 93 public int doEndTag() { 94 try { 95 if ( invalid_ ) 96 pageContext.getOut().println( "</div>" ); 97 } 98 catch ( IOException e ) { 99 } 100 101 return EVAL_PAGE; 102 } 103 104 106 public void setProperty( 107 String property ) { 108 property_ = property; 109 } 110 111 113 protected String property_ = null; 114 115 private boolean invalid_ = false; 116 } 117 | Popular Tags |