JSF. Support bean call on every page load

here is my short situation.

I have a page with datatable and several buttons supported by bean. The bean must be initialized with some default properties. These properties can be changed depending on the action. I started with the annotated RequestScoped Bean and @PostConstruct method. but it seems that datatable only works well with the viewport (session). Now my setup looks like this:

@ManagedBean @ViewScoped public class ProductsTableBean implements Serializable { private LazyDataModel<Products> productsData; @Inject private ProductsFacade model; public void onPageLoad() { // here some defaults are set // ... System.err.println("onPageLoad called"); } public void addRow() { // andhere some defaults redefined // ... System.err.println("addRow called"); } ... 

and a snippet from the jsf page:

  <p:commandButton action="#{productsTableBean.addRow()}" title="save" update="@form" process="@form" > </p:commandButton> ... <f:metadata> <f:event type="preRenderView" listener="#{productsTableBean.onPageLoad}"/> </f:metadata> 

And here the main problem arises in the order of the call, I have the following output:

 onPageLoad called addRow called onPageLoad called <-- :( 

But I want addRow to be the last action to be called, for example:

 onPageLoad called addRow called 

Any simple solution here?

+6
source share
1 answer

Check out this link: http://www.mkyong.com/jsf2/jsf-2-prerenderviewevent-example/

You know that the event is a call for each request: ajax, validation completed .... You can check if this new request was as follows:

 public boolean isNewRequest() { final FacesContext fc = FacesContext.getCurrentInstance(); final boolean getMethod = ((HttpServletRequest) fc.getExternalContext().getRequest()).getMethod().equals("GET"); final boolean ajaxRequest = fc.getPartialViewContext().isAjaxRequest(); final boolean validationFailed = fc.isValidationFailed(); return getMethod && !ajaxRequest && !validationFailed; } public void onPageLoad() { // here some defaults are set // ... if (isNewRequest()) {...} System.err.println("onPageLoad called"); } 
+8
source

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


All Articles