Reputation: 1895
I have a Window where user can search for all the cars in the database and able to edit the details of the car, when user click the edit button (code below) on a given car the following JavaScript is called to open a New window:
<!--EDIT BUTTON -->
<h:commandLink value=""
onclick="return CarEditCrud(this);" title="Edit Car">
<t:graphicImage rendered="true" value="/imgs/edit.gif"
styleClass="edit" />
<f:param value="#{car.carPK}" name="carCode" />
</h:commandLink>
<!--FUNCTION TO OPEN NEW WINDOW TO EDIT CAR DETAIL -->
function CarEditCrud(val) {
....
window.open("EditCarCrud.faces?carPK=" + pk);
}
So new window open with URL of localhost:80/jsp/EditCarCrud.faces?carPK=" + pk
(NOTE: PK would be a number like 935533)
and in constructor of EditCarCrud Backbean i do the following to retrieve the information of the car and populate the fields:
Map requestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String code = (String) requestMap.get("carCode");
That's all good. Problem is this Backbean has Request
scope when user press Submit, i lose all that information (carCode) so the URL is now localhost:80/jsp/EditCarCrud.faces
.
Additionally, there is selectOneItem
which has
....
valueChangeListener="#{carEditCurd.countrycodechange}"
onchange="submit()"
Is there a way for me to keep the carCode in the URL as it was when window originally opened with the PK in the URL?
Upvotes: 0
Views: 1039
Reputation: 1109402
I assume that you're still using JSF 1.x.
You need to retain the request parameter in the subsequent forms by <f:param>
:
<h:commandLink value="Submit" ...>
<f:param name="carCode" value="#{param.carCode}" />
</h:commandLink>
This does not end up in the URL, but it is available as POST request parameter the usual way. The <managed-property>
is by the way a more clean way to let JSF set arbitrary request parameters as bean properties.
An alternative is to add a plain HTML <input type="hidden">
to the form, so that it also get submitted when a dropdown with onchange="submit()"
inside the same form get changed:
<input type="hidden" name="carCode" value="<h:outputText value="#{param.carCode}" />" />
(yes, with an ugly <h:outputText>
nested as value, it's just to prevent possible XSS attacks)
Note that a <h:inputHidden>
is not useable as it loses its value when validation on the form fails.
Upvotes: 3