You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by bu...@apache.org on 2004/11/24 10:18:32 UTC

DO NOT REPLY [Bug 32376] New: - ActionMessagesTool.get(String,String) returns List of null objects if it can't find the resource key in the resources

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=32376>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=32376

           Summary: ActionMessagesTool.get(String,String) returns List of
                    null objects if it can't find the resource key in the
                    resources
           Product: Velocity
           Version: 1.4
          Platform: All
               URL: http://jakarta.apache.org/velocity/tools/javadoc/org/apa
                    che/velocity/tools/struts/ActionMessagesTool.html#get(ja
                    va.lang.String, java.lang.String)
        OS/Version: All
            Status: NEW
          Severity: minor
          Priority: P3
         Component: Source
        AssignedTo: velocity-dev@jakarta.apache.org
        ReportedBy: thoralf.rickert@cadooz.de


If someone adds a message into the HttpServletRequest as follows

ActionMessages messages = new ActionMessages();
ActionMessage message = new ActionMessage("blaa");
messages.add( ActionMessages.GLOBAL_MESSAGE, message );
saveMessages( request, messages );

and uses the VelocityTools ActionMessagesTool to get the messages in a 
Velocity template with getAll() or whatever the returning list contains null 
objects, if the resource key (in the example blaa) wasn't found in the 
resources. That could be okay, but the javadoc says 

> If the message resources don't contain a message for
> a particular key, the key itself is used as the message.

And that doesn't happen. If you look in the code you can see that there is a 
while loop that looks for every ActionMessage key, if the key is in the 
resources. If it isn't, the method sends a warn message to the log but adds 
the null message to the returning list, instead of saying message = msg.getKey
().

The code should be look like this:

while (msgs.hasNext())
{
  ActionMessage msg = (ActionMessage)msgs.next();
  
  String message = null;
  if (res != null)
  {
    message = 
      res.getMessage(this.locale, msg.getKey(), msg.getValues());

    if (message == null)
    {
      Velocity.warn("ActionMessagesTool: Message for key " + 
                    msg.getKey() + 
                    " could not be found in message resources.");
      // BUGFIX HERE!
      message = msg.getKey();
    }
  }
  else
  {
    // if the resource bundle wasn't found, use the key
    message = msg.getKey();
  }
  list.add(message);
}


or maybe better:


while (msgs.hasNext())
{
  ActionMessage msg = (ActionMessage)msgs.next();
  
  String message = null;
  if (res != null)
  {
    message = 
      res.getMessage(this.locale, msg.getKey(), msg.getValues());

    if (message == null)
    {
      Velocity.warn("ActionMessagesTool: Message for key " + 
                    msg.getKey() + 
                    " could not be found in message resources.");
    }
  }
  
  if (message == null)
  {
    message = msg.getKey();
  }

  list.add(message);
}

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org