I am creating a JSF application. I have some items (e.g. products) from database and I want to create a JSF page for editing particular items, that is:
- it should display selected item properties and enable user to edit them,
- I want to be able to view this item with some link,
- I want JSF to somehow remember that I’m editing particular item (e.g. after editing its data it should display this item page)
I have a problem with storing/passing id of item being edited. I saw that in sample JSF CarDemo application they store item (car) being viewed in session. I don’t want to do this because I want user to be able to edit different elements in separate browser tabs.
I tried several approaches:
- using some (e.g.
itemId) GET parameter in URL, but it makes it difficult to go back to item page after editing data (to-view-idfield in faces-config.xml can only contain constants), - using some backing bean managed-property and passing its value in each hyperlink and in forms (by adding hidden field)
The problem I still can’t eliminate is that if after editing some item properties I try to save them and validation (e.g. f:validateLength) fails the page is reloaded but id of item being edited is lost. I think it is quite standard task when creating web applications (e.g. user edition, store products edition) so there certainly should be some solution.
Thanks in advance.
Tomahawk‘s
t:saveStatedoes exactly what you want. Just have something like in your page:or if you want to cover all “uncovered” item values:
If you don’t want to add another component library for some unknown reasons (I do recommend Tomahawk though, it adds more flexible components on top of standard JSF implementation, e.g.
t:dataList,t:dataTable preserveDataModel="true",t:selectOneRadio layout="spread",t:inputFileUpload, etcetera), then you could also use the standard<h:inputHidden>component to pass hidden parameters from request to request (it renders an<input type="hidden">). One caveat is that you will still lost the values when the validation phase fails. But this can be workarounded by using the componentbindinginstead of thevalue.and in the JSF page have the following in the same form:
Hope this helps.