Show dialog after successful operation in jsf? - jsf

I am adding some value to database in jsf. I have a manage bean for this purpose. Now when insertion is successful then I want to show a success alert dialog. please tell me how to do it?

I would suggest to add a dialog component in your page (the same page that calls your bean to insert a value into the DB for instance)
p:dialog id="dialog" widgetVar="widgetVarOfDialog"
Then let assume that you have either a commandButton or a commandLink (you don't provide enough information in your first post) that triggers the insert action in your bean. Just add an oncomplete callback:
p:commandButton action="#{bean.insertValue}" oncomplete="PF('widgetVarOfDialog').show()"
You could also display a p:dialog right from your bean method by using:
RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('widgetVarOfDialog').show()");

Related

onclick for p:outputLabel works on loading/refreshing page but doesn't work when clicking

I have some problems with calling a bean method when clicking on Label.
When page is loading or refreshing click handler function pokus() is called, but when the label is clicked it isn't.
Part of my web page:
<h:form id="pokus">
<p:outputLabel id="pokus2" value="klikni" onclick="#pozadavkyBean.pokus(5)}"/>
</h:form>
and a method in bean:
public void pokus(int i){
System.out.printf("kliknuto sloupec:%d",i);
}
I've also tried it with:
<p:ajax event="click" listener="....
with the same result - method called on loading/refreshing but not when clicking
also tried others events: mousedown, mouseup, .... with same result
using PrimeFaces 5.0
If you will check official document of Primeface regarding Tag outputLabel.You can easily get attribute onclick used for
Client side callback to execute when component is clicked.
But here you are trying to run Managed bean method directly from onclick attribute while onclick used to call JavaScript functions.
As #Mahendran Ayyarsamy Kandiar mentioned and its simple thing outputLabel is not used to call any bean method. Its Just Simply used to show something in the page.In mean time for your requirement you can use CommandButton,CommandLink or some other component but its all depend upon your requirment etc.

JSF component is not getting reloaded for page refresh

I am new to JSF framework and Facelets as view, I am stuck with a problem now. I have got a page where i show some dropdown menu using <h:selectOneMenu> tag. On each selection i used to fire an ajax event using <f:ajax> event it all are working fine. But my problem is, if i select an option 2 on a select box and if I reloads the page again that particular select box will be selected with option 2 by default. I dont need this. I have to reload the select boxes along with page refresh. Please help me to solve this issue.
The selectbox shows the option that is set in the backing bean (and bound by the value attribute of <h:selectOneMenu>). The behavior after a page refresh depends on the scope of your backing bean. If it is session scoped, a page refresh doesn't reset the bean. You could change the scope to #ViewScoped to get the desired behavior. The bean then will be recreated after a full request.
Just set null to backing bean property that used in selectonemenu value after the selected action or set default value in property get method.

Backing Bean Initialization

I have a variable in backing bean that needs to get reset to null whenever the associated page is opened using the relevant menu link. Is there a way to run a initialization code in the backing bean whenever the relevant menu link is clicked? Contsructor runs only the first time the menu link is clicked. I guess the bean is then retained in the jsf context and is not getting recreated. Is there a way to ensure a new object of that backing bean is created each time the menu link is clicked? Thanks!
Couldn't you just put the bean in request scope?
Another option would be to use a setpropertyactionlistener on the menu. When the menu is clicked, set the value to "null".
You have the following options:
1 . Change the bean to the request-scoped bean
2 . Use the action attribute to call the method on the backing bean to run the initialization code whenever the link is clicked , something like this:
<h:commandLink action="#{myBean.init}" value="My Link" />
And myBean.init() contains the initialization code

ActionListener phases in JSF

HI,
I have a doubt on calling the ActionListener method in the JSF beans. For example every request or submission of JSF form is gone through the life cycle of six phases. But, when we are triggering the particular event like action listener or value change listener, is there any lifecycle associated with that request?
Please clarify me.
Any action listener is invoked during invoke action phase, before the real action method. Which action listener methods are to be invoked are determined based on the actionListener attribute of the UICommand component which is associated with the submit.
Any value change listener is invoked during validations phase (or apply request values phase when immediate="true" for the particular UIInput component) after a succesful conversion/validation of the submitted value and only when the submitted value differs from the initial value. Which value change listener methods are to be invoked are determined based on the valueChangeListener attribute of the UIInput components which are associated with the submit.
And no, they do not have their own lifecycle. When they finish executing and return, it's still inside the same phase of the lifecycle. After invoking the valueChangeListener, JSF will continue with conversion/validation of the next UIInput component, or if there are none, then proceed to the next phase. After invoking the actionListener, JSF will continue with the next actionListener or if there are none, invoke the real action method.
Update: after reading your comments again, I think that I now see your doubt about particularly the value change listener. You seem to think that it by default immediately fires a brand new request to the server side during the client side change event. It does that not by default. You can only achieve this by adding a little piece of JavaScript code which submits the entire HTML form during the change event of the HTML input field.
onchange="this.form.submit()"
This part has nothing to do with JSF. It's a simple HTML attribute. Open the page in webbrowser, rightclick and choose View Source. You'll see that it's there. Disable JavaScript in your browser or remove it in JSF code and you'll see that it won't work anymore. You would need to press the submit button yourself to get it all to run.

Problem With JSF 1.1 and PopUp

I am trying to popup a window when someone clicks a button on the data table.
<h:commandButton
action="#{cacheController.popupDetails}"
immediate="false"
onclick="popup()"
value="View Details"
styleClass="submit">
</h:commandButton>
The associated popup function is
function popup() {
window.open('RDDetails.jsf','popupWindow', 'dependent=yes, menubar=no, toolbar=no, height=500, width=400');
}
Now in the new 'RDDetails.jsf" file, I am trying to access the same managedBean cacheController. But the problem is, the pop-up window and JSF lifecycle is not in sync. As a result, the popup first displays blank and when I refresh, it pulls out the proper data.
Is there anyway I can click on a button which will do some processing in the managed bean and then opens a pop up which rerieves the processed data from the managed bean.
I am using JSF 1.1.
You're here basically firing two independent requests: one associated with the form submit and other which opens the RDDetails.jsf in a popup. You'll need to combine this in one request. You can achieve this in basically two ways:
Get rid of the onclick and just add target="_blank" to the <h:form> so that it get submitted into a new window/tab.
Block the default action by adding return false; to the onclick and do the business logic in the constructor of the bean associated with RDDetails.jsf. The only (major) caveat is here that the model won't be updated with the form fields. Thus, you'll need to pass the form fields as request parameters of the popup URL manually with help of JavaScript. You can then make use of managed property entries in the faces-config.xml to inject the GET request parameters into the model.
First way is obviously the easiest, but this doesn't give you a "fullworthy" popup/modal dialog. The second way is a bit harder (unless you've already a good grasp on both JavaScript and JSF). I would then consider to look for a component library which provides a ready-to-use popup component.
See my example:
<h:commandLink action="#{controller.myAction}" onmousedown="document.forms['idform'].target='_blank';">
I'm using jsf 1.1

Resources