Java – implement the project version page in JSF

I am creating a JSF application I have some projects from the database (such as products). I want to create a JSF page for editing a specific project, that is:

>It should display the selected item properties and allow users to edit them, > I want to be able to view this item through some links, > I want JSF to remember in some way that I am editing a specific item (for example, after editing its data, it should display this item page)

I'm having trouble storing / passing the ID of the item being edited I see in the sample JSF cardemo application that they store items (cars) that are being viewed in a session I don't want to do this because I want users to be able to edit different elements in separate browser tabs

I tried several methods:

>Use some get parameters (such as itemid) in the URL, but it is difficult to return to the project page after editing the data (the to view ID field in faces-config.xml can only contain constants), > use some bean managed properties and pass their values in each hyperlink and form (by adding hidden fields)

The problem I still can't eliminate is that if I try to save some item properties after editing them and the validation (such as F: validatelength) fails, the page will be reloaded, but the ID of the item being edited will be lost I think this is a very standard task when creating web applications (such as user version, store product version), so there must be some solutions

Thank you in advance

Solution

Tomahawk's t: savestate fully meets your requirements Just add something like this to your page:

<t:saveState value="#{bean.item.id}" />

Or if you want to cover all "uncovered" item values:

<t:saveState value="#{bean.item}" />

If you don't want to add other component libraries for unknown reasons (I do recommend Tomahawk, which will add more flexible components on the standard JSF implementation, such as t: datalist, t: datatable preservedatamodel = "true", t: selectoneradio layout = "spread", t: inputfileupload, etcetera), Then you can also use the standard < H: inputhidden > to pass hidden parameters from the request to the requested component (it renders < input type = "hidden" >) It should be noted that when the validation phase fails, you will still lose the value But this can be achieved by using component bindings instead of values

private HtmlInputHidden itemId = new HtmlInputHidden();
private Item item = new Item();

public void editItem() { // Action method when selecting an item for edit.        
    itemId.setValue(item.getId());
}

public void saveItem() { // Action method when saving edited item.
    item.setId((Integer) itemId.getValue());
}

It has the following same form in the JSF page:

<h:inputHidden binding="#{bean.itemId}" />

I hope this will help

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>