问题
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 commandButton the ID=100 is removed and the page is refreshed, and this is Before it even runs the method, right, so this means that I do not have the ID when i press the button.
How do you solve this?
By having this Managed Bean
public class BeanWithId implements Serializable {
private String id;
private String info;
private void populateInfo() {
info = "Some info from data source for id=" + id;
}
public String getId() { return id; }
public void setId(String id) {
this.id = id;
populateInfo();
}
public String getInfo() { return info; }
public void setInfo(String info) { this.info = info; }
public String save() {
System.out.println("Saving changes to persistence store");
return null; // no navigation
}
}
And adding
<p><h:commandButton action="#{beanWithId.save}" value="Save" /></p>
To my facelet-page. Now I also have the correct information in my faces-config.xml and when I access my page using ?ID=100 I do get the correct Item returned.
回答1:
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);
}
}
回答2:
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;
}
回答3:
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.
来源:https://stackoverflow.com/questions/1530671/using-managed-property-with-commandbutton-in-java-server-faces