Jean-Noel Ribette wrote:
> Erik,
> 
> You might want to have a look at the struts-layout taglib hosted at 
> http://struts.application-servers.com
> This opens source library implements (nearly) all the functionality you 
> described here, and much more !

Cool - I've looked at that library before but had forgotten about it. 
For some reason I like to implement this stuff myself though :)  Good 
stuff though - we don't really have all those layout needs so that 
framework might be a bit much for us.

> If you intend to share your code, I'm very interesting in the part 
> checking if a field is required in the validator resources.

Here's the relevant snipped from doStartTag():

         // Look up this key to see if its a field of the current form
         boolean requiredField = false;
         boolean validationError = false;
         ValidatorResources resources = (ValidatorResources) 
pageContext.getServletContext().getAttribute(ValidatorPlugIn.VALIDATOR_KEY);
         ActionMapping mapping = (ActionMapping) 
pageContext.getAttribute(Globals.MAPPING_KEY, PageContext.REQUEST_SCOPE);
         Locale locale = (Locale) 
pageContext.getAttribute(Action.LOCALE_KEY, PageContext.SESSION_SCOPE);

         // this is where there is some specific stuff
         // with our key conventions: SomeForm.someField would the key
         String formName = mapping.getAttribute();
         String fieldName = key.substring(formName.length() + 1);
         Form form = resources.get(locale, formName);
         if (form != null) {
             Field field = (Field) form.getFieldMap().get(fieldName);
             if (field != null) {
                 if (field.isDependency("required")) {
                     requiredField = true;
                 }
             }
         }

         // see if field is in error
         ActionErrors errors = (ActionErrors) 
pageContext.getAttribute(Action.ERROR_KEY, PageContext.REQUEST_SCOPE);
         if (errors != null) {
             Iterator errorIterator = errors.get(fieldName);
             if (errorIterator.hasNext()) {
                 validationError = true;
             }
         }

// lookup of message removed, same as MessageTag here


         message = "<span class=\"" + (validationError ? "labelerror" : 
"label") + "\">" + message + (requiredField ? "*" : "") + ":</span>";


If there is room for improvement here, by all means let me know.  I 
don't suppose there is much way to improve the performance of this, 
since its all pretty random access quick code, but "smells" like a bit 
much to do so many times in one page.

Again, if the requirement ever changes to make the <span>, "*", or style 
class different we can easily make that part dynamic based on some site 
configuration or other message resource lookups.  And of course its all 
in one place so we can affect the whole site at once.  We're "agile", ya 
see, and don't do more than necessary, but set it up so that we could 
easily extend if desired later :)

Again, let me know if anything I've code above is wrong, brittle, or not 
as good as it could get.  And feel free to incorporate this wherever you 
like.

        Erik


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to