p:remoteCommand won't fire bean method - jsf

I have simple primefaces remoteCommand in composite component that won't trigger method in back bean.
<h:form id="frm1" prependId="false">
<p:remoteCommand name="rc1"
actionListener="#{Bean1.preloadMenu()}"
process="#this"
onstart="alert('xxx')"
autoRun="true"
/>
...
RemoteCommand works, it runs onstart client-side script but it doesn't trigger method in bean (never steps into method, as if it doesn't exist). I get no error on client or server side.

I'll close this topic. I resolved this issue by putting code in initialize (#PostConstruct) methode. Didn't want to do it like that but this dialog isn't used too often and bean is associated just with this component so it shouldn't do much damage.

Related

Why JSF postAddToView event listener method is called during postback calls?

I added postAddtoView event listener to below component. I was expecting the
orderView.populateForm listener method is being called once during creation of the initial view (https://javaserverfaces.java.net/nonav/docs/2.2/javadocs/javax/faces/event/PostAddToViewEvent.html
).
For any postback call where view is restored I do not expect a new component instance is added to view. Hence no event to fire.
However, event listener method is being called for each postback call.
<h:form id="orderForm">
<p:panelGrid id="attributePanel" columns="#{orderView.numberOfGridColumns}">
<f:event type="postAddToView" listener="#{orderView.populateForm}" />
</p:panelGrid>
<p:commandButton ajax="true" value="Create" process="#form" update="#form"/>
</h:form>
OrderView is a view scoped CDI bean. As far as I understand view scoped beans are stored in the partial view state and by default partial view state saving is enabled. (BalusC gives a nice explanation here about the issue Should PARTIAL_STATE_SAVING be set to false?) To see it has an effect, I disabled/enabled partial state saving. It did not help.
Thanks in advance.
PS: I am running on Mojarra 2.2.12/ Glassfish 4.1

Synchronous dialog invocation from managed bean

Is there a way to use PrimeFaces RequestContext to call a dialog defined in the JSF from a managed bean, which has a form, but synchronously, meaning that the managed bean wait its thread execution until the user submits the form?
Currently, I am successfully invoking a dialog from my managed bean but the call is asynchronous, meaning the dialog is popped open but the managed bean thread continues on without waiting for the user to supply the needed additional data via the dialog.
So, in my JSF, I have the dialog defined as follows:
<p:dialog header="My Dialog" widgetVar="myDialog" modal="false" height="100">
<h:form>
<h:outputLabel for="inputData" value="Input Data:"/>
<p:inputText id="inputData" title="Input Data"
maxlength="16" required="true" ... >
</p:inputText>
<h:commandButton value="Submit"/>
</h:form>
</p:dialog>
In my managed bean, I call the dialog conditionally if some criteria is met:
...
if(noteReqd) {
RequestContext requestContext = RequestContext.getCurrentInstance();
requestContext.execute("PF('myDialog').show();");
//here I want the managed bean to stop until the user supplies the extra data needed
//but it just proceeds downstream without the data the user enters
}
...
RELATED:
Prompting overlay for extra data by managed bean
Calling a JavaScript function from managed bean
No it's not possible. The Primfaces's RequestContext can only do his job then the JSF lifecycle continues and sends the javascript information (PF('myDialog').show();, e.g., what to do) back to the client (browser).
A solution would be change your logic in some way.
user supplies some data
check if data was enough
if not, reopen the same form (or another one)
do step 2 + 3 until enough data is supplied
continue with your logic

ajax="false" vs process="#this"

I'm get confused with this 2 concepts ajax="false" and process="#this". Is the resulting view the same?, is there any order of execution?, will both execute the validation phase? something else should I care about?
ajax="false" means that your UICommand e.g. <p:commandButton> will perform a request/response process without using ajax. This is an attribute for PrimeFaces UICommands.
process="#this" means that only the current component value will be send to the server instead of sending the data from the whole <h:form>. This attribute belongs to PrimeFaces components that have ajax functionality embedded. It is similar to execute attribue in <f:ajax> from JSF.
More info:
What is the function of #this exactly?

JSF ViewScope and Bean creation

I have a problem that i don't understand:
I request a new site. A site has a link that opens a dialog. The link is inside a form.
The dialog is not inside the form.
A reduced code example:
<p:outputPanel id="layout-center" >
<h:form>
<p:commandLink id="option_field_user_profile" actionListener="#{controllerBean.getBean('userProfileBean', component).init}" oncomplete="#{controllerBean.getBean('userProfileBean', component).show}" >
<h:outputText value="#{msg.mProfile}"/>
</p:commandLink>
</h:form>
</p:outputPanel>
<p:dialog header="#{userPreferencesBean.header}" widgetVar="#{userPreferencesBean.widgetVar}" appendToBody="#{userPreferencesBean.appendToBody}" resizable="#{userPreferencesBean.resizable}" id="#{userPreferencesBean.xhtmlId}" dynamic="#{userPreferencesBean.dynamic}" modal="#{userPreferencesBean.modal}" closable="#{userPreferenceBean.closable}">
<ui:include src="/WEB-INF/templates/modification/userPreferences.xhtml" />
</p:dialog>
UserPreferencesBean is in ViewScope. My problem is now that the #PostConstruct method from the UserPreferencesBean is called twice with the non-postback request i.e. the Bean is constructed twice although it should be the same view. If i move the dialog inside the form for testing purposes it is called once, like expected. But since the dialog has its own form this is not a solution, for sure.
When the site is loaded and I hit F5, the PostConstruct method is executed once.
Has somebody an idea?
This is caused because you referenced a view scoped bean property in the view build time attribute id of the <p:dialog>. If you fix the id to be static, or to reference a request or application scoped bean property instead, then your view scoped bean will behave as expected.
See also:
JSTL in JSF2 Facelets... makes sense? - for some background explanation on view build time and view render time; the id and binding attributes of UI components are evaluated during view build time.

p:commandButton does not fire action

Here is the problem: actionlistener does not want to be fired
#ManagedBean(name="hotelsController")
#SessionScoped
public class HotelsController implements Serializable {
public void requestHotelAvail(ActionEvent event) {
request = new Request(df.format(arrivalDate), df.format(departureDate));
}
}
and xhtml
<h:panelgroup id="rooms"/>
<h:form id="hotelSearch">
<p:commandButton actionListener="#{hotelsController.requestHotelAvail}" value="submit" update="rooms" />
</h:form>
I have tried everything I could search of changed #managedbean to #component set import to import javax.faces.event.ActionEvent;
But it still does not fire anything.
Form is in a p:accordion and when used with h:commandbutton it works fine
EDIT: sorry for mislead. rooms updates after click but actionListener is not fired. so rooms will not get any new data. Important code in requestHotelAvail needs to be fired before updating rooms and its not.
EDIT2: PrimeFaces 2.2.1 - I've read whole manual to primefaces but theres no explanation to this as I've done all that it states
I've tried using action instead of actionListener without ActionEvent but it never do anything. using <h:commandbutton action="#{hotelscontroller.requestHotelAvail}"/> works great but I want that ajax engine to refresh only that rooms panelgroup
UPDATE: Now it works. Form couldn't be in <p:accordion> but why and how to enable it there? Form now I'll work without it.
I suspect the different behavior from h:commandLink comes from ajax/non-ajax processing.
By default - if you don't use f:ajax - h:commandLink is non-ajax and entire page is rerendered. Primefaces p:commandLink is using ajax and you indicate rooms as component to be updated. In your case rooms is outside form so it should rather be addressed as :rooms (mind the colon) instead of just rooms.
update: have you tried ajax with h:commandLink? It would be:
<h:commandButton action="#{hotelscontroller.requestHotelAvail}" value="submit">
<f:ajax render=":rooms"/>
</h:commandButton>
Also I'm not that familiar with primefaces but maybe you can try to explicitly indicate the component to process with additional process="#this" - although I would assume this to be default as in basic library.
You try to inspect the response:
Open Chrome or Firefox -> Inspect Element -> Network and follow the ajax call.

Resources