Best approach to using command link from within a custom component
I have created a navigation component "Menu" , used as follows:
<custom:menu />
The contents of menu are stored in an xml file and the Menu component delegates the rendering to a custom renderer, which basicaly writes out html links. (Actually, the component does a lot more, but I'm simplifying to get to the point.).
Sometimes, I need to render a command link instead of a regular html link. I don't want to re-invent the wheel here, so I want to delegate this to the standard command link components.
Approach 1: create child components within the Menu component constructor, e.g.
HtmlCommandLink c =new HtmlCommandLink();
MethodExpression e = FacesContext.getCurrentInstance().getApplication().getExpressionFactory().
createMethodExpression(FacesContext.getCurrentInstance().getELContext(),
"#{registrationEditor.enterSubmission}",
String.class,
new Class[]{});
c.setActionExpression(e);
c.setValue("TestLink");
c.setTransient(true);
getChildren().add(c);
Approach 2: delegate to the CommandLinkRenderer within my custom renderer, e.g.CommandLinkRenderer delegate =new CommandLinkRenderer();
HtmlCommandLink link =new HtmlCommandLink();
ExpressionFactory elFactory =
FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
MethodExpression actionExpression =
elFactory.createMethodExpression(context.getELContext(), c.getExpression(), String.class,new Class[]{});
link.setActionExpression(actionExpression);
link.setParent(component.getParent());
link.setValue(c.getLabel());
delegate.encodeBegin(context, link);
delegate.encodeChildren(context, link);
delegate.encodeEnd(context, link);
Is either of these a respectable approach - they feel a little hackish. If so, which do you prefer. If not, what do you recommend?
Thanks
Richard

