0

Which is the best way to implement the Primefaces commandButton for saving a form in a CRUD application handling the browser refresh and back buttons in the most intuitive manner?

PS: The CRUD code is credited to BalusC Recommended JSF 2.0 CRUD frameworks But this uses standard <h:commandButtons>

Here is relevant code from BalusC's post

<p:commandButton value="Save_A" action="#{CRUD.saveEdit()}" ajax="true" update="@all"/>
<p:commandButton value="Save_B" action="#{CRUD.saveEdit()}" ajax="false"/>

<p:commandButton value="Save_C" action="#{CRUD.saveEdit2()}" ajax="true" />
<p:commandButton value="save_D" action="#{CRUD.saveEdit2()}" ajax="false"/>

--

@ManagedBean
@ViewScoped
public class CRUD implements Serializable {

public void saveEdit() {
    dao.update(item);
    this.item = new ChecklistClass();
    edit = false;
}

public void saveEdit2() {
    dao.update(item);
    this.item = new ChecklistClass();
    edit = false;
    util.redirectWithGet();  // <- Eliminates popup with browser refesh
}

Save_A and B I don't like because after a successful submit, if you refresh the browser, you get the popup about resending information previously submitted.

Save_C and D I prefer, they both seem to work intuitively. Refreshing the browser does not give you the popup asking to resend data and the back button works somewhat intuitively.

But would one be better for network requests? Since saveEdit2() has a redirect should I not use the p:commandButton with Ajax="true"? In other words would that do a double redirect?

Community
  • 1
  • 1
jeff
  • 3,618
  • 9
  • 48
  • 101
  • 1
    did you mean you did not like button D? Your question says you don't like and prefer B – Mahendran Ayyarsamy Kandiar Dec 18 '15 at 16:15
  • @MahendranAyyarsamyKandiar yes, thanks I edited my Q – jeff Dec 18 '15 at 16:36
  • If you like C and D for refresh popup issue i will use C with process="inputA inputB CalendarC passwordD" as D will process the whole form that holds the button – Mahendran Ayyarsamy Kandiar Dec 18 '15 at 16:44
  • Why do folks always prefer verbose session scoped beans quite unnecessarily for performing CRUD while using JSF 2.x? Doing Ajaxical posts, using the PRG pattern or using a request based token depends upon how you want to perform them and your GUI interface design. Ajaxical posts have more user friendliness as compared to others, using a request based token results in awkward behaviour with regard to browser history. It depends, there is no only one standard way to perform CRUD. – Tiny Dec 18 '15 at 16:45
  • @Tiny can you show example of better approach? Thanks! – jeff Dec 18 '15 at 17:14
  • I prefer the Ajaxical approach almost always as demonstrated on the PrimeFaces showcase in most examples. – Tiny Dec 18 '15 at 17:16
  • @Tiny, my mistake, BalusCs code uses ViewScoped in his bean. I got SessionScope from here http://www.codeproject.com/Articles/1030872/A-Simple-CRUD-Example-with-JSF which is where I picked up the redirectWithGet() utility. I see Primefaces showcase uses ViewScoped so I'll try to work through this using ViewScoped – jeff Dec 18 '15 at 18:54
  • 2
    The author on codeproject links the same example on stackoverflow with a conclusion, "*JSF has a strong ASP.NET Web Form flavor, **it heavily relies on "POSTBACK" for its functionalities.** (If you click the "Continue" button and proceed with the refresh, you will notice that the student just added gets added again. This is definitely not a good behavior. **This kind of behavior is due to the JSF's "POSTBACK" nature.**)*" This is not in particular related to JSF. This is how internet works. This same thing will happen with or without using JSF. – Tiny Dec 18 '15 at 19:13
  • I think you'll find this Q&A and all links therein sufficient enough to answer your own real problem: http://stackoverflow.com/questions/8459903/creating-master-detail-pages-for-entities-how-to-link-them-and-which-bean-scope – BalusC Dec 19 '15 at 14:07
  • @BalusC Since that is a newer post than your other post on CRUD here: http://stackoverflow.com/questions/3180400/recommended-jsf-2-0-crud-frameworks/3180885#3180885 is it then preferred to have two separate backing beans, ViewItemLists and EditItems using a converter verses using the single backing bean and passing the entity through the action, action="#{singleBean.edit(item)"} ?? – jeff Dec 21 '15 at 13:50

0 Answers0