JSF. Load and invoke supported bean methods on each page

See the English answer > invoke JSF managed bean action on page load4

I have pages with datasheets and several bean - supported buttons The bean should be initialized with some default properties You can change the properties according to the operation I start with the requestscoped bean and @ postconstruct annotation methods However, it seems that datatable is only applicable to the view (session) scope Now my settings look 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 fragments from JSF pages:

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

The following are the main problems in the call sequence. I have the following output:

onPageLoad called
addRow called
onPageLoad called <-- :(

However, I want addrow to be the last called action, as shown below:

onPageLoad called
addRow called

Here are simple solutions

Solution

Check this link:

You know that events are called on every request: Ajax, validation failure You can check whether it is such a new request:

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");
    }
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
分享
二维码
< <上一篇
下一篇>>