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

[2303 byte] By [thatricharda] at [2007-11-27 11:26:28]
# 1

I think either is a fine approach and not at all hackish. I prefer the second, although I couldn't tell you why (just a gut feeling). That said, I haven't tried either so there may be hidden problems. My main concern would be what happens when the request is submitted from the page, that the Restore View phase is done correctly so that the proper event is fired.

RaymondDeCampoa at 2007-7-29 16:10:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...