Implementing the article editing page in JSF

I am creating a JSF application. I have some elements (e.g. products) from a database, and I want to create a JSF page to edit individual elements, namely:

  • it should display the selected properties of the element and allow the user to edit them,
  • I want to be able to view this item using some link,
  • I want JSF to somehow remember that I am editing a specific element (for example, after editing its data, it should display this page).

I have a problem with storing / passing the id of the element being edited. I saw that in the sample JSF CarDemo application they store the item (car) viewed in the session. I don’t want to do this because I want the user to be able to edit different elements in separate browser tabs.

I tried several approaches:

  • using some itemIdGET parameter (for example, ) in the URL, but this makes it difficult to return to the page of the element after editing the data (the field to-view-idin faces-config.xml can contain only constants),
  • using some backup bean of a managed property and passing its value in each hyperlink and in forms (by adding a hidden field)

, - , , , (, f:validateLength) , , . , - (, , ), - .

.

+3
2

Tomahawk t:saveState , . - :

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

"" :

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

(I do Tomahawk, , JSF, t:dataList, t:dataTable preserveDataModel="true", t:selectOneRadio layout="spread", t:inputFileUpload ..), <h:inputHidden> ( <input type="hidden">). , - , . binding value.

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());
}

JSF :

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

, .

+3

:

. , ( , JSP Facelets). . , , , . . URL-.


, bean :

//pseudo-stubs
//request scope: #{editor}
public class Editor {
  public Integer getId();
  public void setId(Integer id);
  public String save();
}

( , .)

faces-config:

<managed-bean>
  <managed-bean-name>editor</managed-bean-name>
  <managed-bean-class>foo.Editor</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
    <property-name>id</property-name>
    <property-class>java.lang.Integer</property-class>
    <value>#{param.id}</value> 
  </managed-property>
 //etc

id:

<h:inputHidden value="#{editor.id}" />
<h:commandLink action="#{editor.save}" value="save" />

; , - id. , editor bean save.

+4

Source: https://habr.com/ru/post/1724167/


All Articles