In addition to my question \"Creating an “Edit my Item”-page in Java Server Faces with Facelets\" I would liek to cover an issue that this provided.
When I press the com
This solved my problem
<h:commandLink action="#{beanWithId.save}" value="">
<f:verbatim><input type="button" value="Save"/></f:verbatim>
<f:param name="id" value="#{beanWithId.id}"/>
</h:commandLink>
Works like a Charm, it does however remove the visible GET-parameter, but it is still stored so that the faces-config can access param.id.
There are several ways to preserve the ID from the original GET URL. I'm not attempting to be comprehensive.
Add a param to a commandLink
<h:commandLink action="#{beanWithId.save}" value="Save">
<f:param name="ID" value="#{param.ID}" />
</h:commandLink>
Any time the link is clicked, the ID will be set from parameter.
Use a hidden field
<h:form>
<h:inputHidden value="#{beanWithId.id}" />
<p>ID: <h:outputText value="#{beanWithId.id}" /></p>
<p>Info: <h:inputText value="#{beanWithId.info}" /></p>
<p><h:commandButton action="#{beanWithId.save}" value="Save" /></p>
</h:form>
Any time the form is posted, the ID will be set from the form.
Preserving the URL
Since the form URL does not include the original query, a post will remove the ID from the URL in the browser bar. This can be rectified by use of a server-side redirect after the action has been performed.
public String save() {
System.out.println("Saving changes to persistence store: id=" + id);
redirect();
return null; // no navigation
}
private void redirect() {
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext ext = context.getExternalContext();
UIViewRoot view = context.getViewRoot();
String actionUrl = context.getApplication().getViewHandler().getActionURL(
context, view.getViewId());
try {
// TODO encode id value
actionUrl = ext.encodeActionURL(actionUrl + "?ID=" + id);
ext.redirect(actionUrl);
} catch (IOException e) {
throw new FacesException(e);
}
}
You can set the property by using f:setPropertyActionListener if you use JSF 1.2 or newer.
<h:commandButton value="Save" action="#{beanWithId.save}">
<f:setPropertyActionListener target="#{beanWithId.id}" value="100" />
</h:commandButton>
If you use JSF 1.1 or prior you can use
<f:param name="reqId" value="100" />
but this time you have to get the parameter and set it manually in the action like such:
public String save() {
String idParam
=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("reqId");
setId(idParam);
return null;
}