Re: Tomahawk jsCookMenu
Mongoose wrote:
Trying to use [Tomahawk] jsCookMenu and for some reason I'm having trouble
getting the id of the menu choice I choose in the action listener in
the backing bean. This can't be that hard but I'm new to the
menu . . . I sense it's something like this but it's not quite
right . . . because I get a class cast exception when I execute the
code below. Can someone assist me with this?
public void setItem(ActionEvent s)
{
String outcome2= (String) ((HtmlCommandNavigationItem)
s.getComponent()).getId();
System.out.println(outcome2);
}
You don't give much information. What is the exact text of the
ClassCastException message?
What are the fully-qualified names (FQNs) of the types 'ActionEvent' and
'HtmlCommandNavigationItem'?
It looks like either 's.getComponent()' isn't returning an
'HtmlCommandNavigationItem' or 'HtmlCommandNavigationItem#getId()' doesn't
return a 'String'.
When I look at
<http://myfaces.apache.org/tomahawk-project/tomahawk12/apidocs/org/apache/myfaces/custom/navmenu/htmlnavmenu/HtmlCommandNavigationItem.html>
on the off chance that that's the 'HtmlCommandNavigationItem' to which you
refer, I see the documentation for the 'getId()' method goes all the way back to
<http://myfaces.apache.org/core12/myfaces-api/apidocs/javax/faces/component/UIComponentBase.html>
<http://myfaces.apache.org/core12/myfaces-api/apidocs/javax/faces/component/UIComponentBase.html#getId()>
.. It returns 'String'. So I guess the cast to 'HtmlCommandNavigationItem'
failed.
Assuming that's correct, and you should have this answer anyway, on what basis
can the algorithm rely that the call to 's.getComponent()' will always return
an 'HtmlCommandNavigationItem'?
If you cannot rely on receiving only that type at that point in the code, you
must put in some sort of run-time check to do something reasonable when that
isn't the type.
There's a principle of programming I call "closure" - that any inputs to a
state change will induce a change to a valid state. A program should have
full closure. That means it must be prepared to handle any inputs. You did
not program for full coverage of possible inputs.
For the 'getId()' call, you really don't need the cast to
'HtmlCommandNavigationItem'. 's.getComponent()' is guaranteed to return a
'UIComponent', which in turn has the 'getId()' method, so you could just skip
that troublesome downcast unless it actually serves a purpose.
--
Lew