JSF Link to an entity

It’s very common to want to have a link which submits some form fields. Unfortunately, JSF/Facelets don’t, as far as I can tell, provide a tag which allows you to directly setup some beans and call an action when you click a link. The closest you can get is passing some string POST parameters, like this:

<h:commandLink id="editLink" action="#{employeeAction.prepareEdit}">
<h:outputText value="#{msg.edit}"/>
<f:param name="id" value="#{emp.id}"/>

And then you access that parameter like this:

FacesContext context = FacesContext.getCurrentInstance();
Map map = context.getExternalContext().getRequestParameterMap();
String employeeID = (String) map.get("id");

This is fairly normal compared to other frameworks, perhaps, but it’s not as nice as I’d expect from JSF for something so simple.

There’s a page describing some alternative methods for use in Apache MyFaces here.


Comments are closed.

%d bloggers like this: