Hi I have a managed bean with some functions , based on some condition in that function I will like to call a dialog box
Managed bean function goes as
public String editStudent(){
setReadOnly(false);
setButton(true, true, true, false, true, true,true);
LockItem lItem;
if(selectStudent !=null){
lItem = (LockItem) services.getbyId("LockItem", condition);
if (lItem == null){
System.out.println("Student Avalibale for process :::");
studentRedirect();
return "studentEdit.jsf?faces-redirect=true";
} else {
//To show dialog from here
System.out.println("Student Not Avalibale : Locked By " + lItem.getLockedBy());
}
} else {
FacesMessage msg;
msg = new FacesMessage("Please select the record.");
FacesContext.getCurrentInstance().addMessage(null, msg);
return show("menu");
}
}
Is there any method using which we can call the dialog box from such managed function
You can, by using the RequestContext (or PrimeFaces if you are using the version 6.2 or higher) class.
Suppose you have the following:
<p:dialog id="myDialogID" widgetVar="myDialogVar">
....
</p:dialog>
So the way you do in the facelet itself, i.e. onclick=myDialogVar.show();, the same can be done in your managed bean like so:
For PrimeFaces <= 3.x
RequestContext context = RequestContext.getCurrentInstance();
context.execute("myDialogVar.show();");
For PrimeFaces >= 4.x to PrimeFaces < 6.2 (as per #dognose and #Sujan)
RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('myDialogVar').show();");
For PrimeFaces >= 6.2
PrimeFaces current = PrimeFaces.current();
current.executeScript("PF('myDialogVar').show();");
This is for using targeted dialogs. If you just need to show a message without giving preference to any custom dialog, then you can do it this way:
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message Title", "Message body");
// For PrimeFaces < 6.2
RequestContext.getCurrentInstance().showMessageInDialog(message);
// For PrimeFaces >= 6.2
PrimeFaces.dialog().showMessageDynamic(message);
You can pass in arguments and set callbacks as well. Refer to the showcase examples in the link.
See also:
Primefaces Dialog framework
If you are on primeface 4.0 Or Above:
RequestContext.getCurrentInstance().execute("PF('yourdialogid').show()");
Vrushank's solution is correct.
There is also another way: in your dialog, bind rendered attribute to a boolean value of your bean, and set the visible attribute to true, like this:
<p:confirmDialog
widgetVar="myDialog"
visible="true"
rendered="#{myBean.showMyDialog}">
In action listener in your bean, you just call setShowMyDialog(true), and a dialog will show (assuming your dialog is being updated by this action). This approach is useful if for some reason you don't want your dialog's HTML to be rendered when it is not visible to the user. This way, in your bean you have access to information, whether your dialog is visible.
Another benefit of this solution is, that your dialog is not hidden on ajax update (of dialog or its parent).
Related
So, here is the jsf component:
<h:selectBooleanCheckbox id="cb#{index}" value="#{backingBean.value}" />
And here is a part of the backing bean java:
/**
* getValue is a method which checks if a checkbox is selected or not, using the checkbox ID
*/
public boolean getValue() {
//TODO: get the checkbox id
String checkboxID = ??
if (getCheckedIDs().contains(checkboxID)) {
return true;
}
return false;
}
When the page is loading the checkboxes, I want to check this way if the checkbox is selected or not. So the question is, what to write instead of ?? to get the ID of the checkbox who called the method? It's very important that I can use only JSF 1.1, so there are many solutions which won't work with this version.
Another very important thing is, that I cannot use the setter/getter in backing bean like here: https://stackoverflow.com/a/48006066/9158590, because I need to store the value of the checkbox immediately after it's checked or unchecked, not only after submit. I have already resolved the storing in backing bean right after checking, I only need to send back true or false when loading page.
This is because I use a page navigation, and for example, when I check a box in page 1, and go to another page, and then go back, the box isn't selected anymore (only in backing bean).
FacesContext context = FacesContext.getCurrentInstance();
UIComponent comp = context.getViewRoot().findComponent("Parent Element
id of HtmlSelectBooleanCheckbox ");
for(UIComponent c : comp.getChildren())
if(c instanceof HtmlSelectBooleanCheckbox)
{
// do something
}
Coming to your Question :
the value of the variable "#{backingBean.value}" is true then the checkbox will be selected
I have the same requirement as mentioned in "
primefaces close dialog on init don't work" but the only answer is not a working solution, becaue in Dialog Framework i don't have a widgetVar.
My #PostConstruct method looks similar:
#PostConstruct
private void init() {
Tree tree = initTree();
if (tree.getChildCount() == 1) {
// #todo close dialog if tree has only 1 child
// this does not close the dialog
RequestContext.getCurrentInstance().closeDialog(true);
// this does not work, because there is no widgetVar
//RequestContext.getCurrentInstance().execute("yourDialogWidgetVar.close()");
}
}
So, how can I close a DialogFramework xhtml page from its #PostConstruct method ?
I'm using Primefaces 6.0 on Wildfly 10.0.0.Final
Is it possible to tell which component was clicked that caused the ViewExpiredException to be thrown when using AJAX? I'd like to avoid showing the session expired page if the user simply tried to log out and show him the normal log out page instead.
I was thinking of overriding shouldHandleExceptionRootCause but I cannot find anything to identify if the logout button was pressed or not.
I'm using Mojarra JSF 2.2 and Omnifaces 2.5.1.
Solution
#Override
protected boolean shouldHandleExceptionRootCause(FacesContext context, Throwable exception)
{
UIComponent source = Components.getCurrentActionSource();
if (source != null && "logout".equals(source.getId()))
{
NavigationHandler nh = context.getApplication().getNavigationHandler();
nh.handleNavigation(context, null, "logout.xhtml?faces-redirect=true");
return false;
}
else
{
return super.shouldHandleExceptionRootCause(context, exception);
}
}
The Components utility class offers several methods to identify the currently submitted form, the currently invoked command and the source component of the action event (which can be the command component itself, but also an input component in case of an ajax event).
UIForm form = Components.getCurrentForm();
UICommand command = Components.getCurrentCommand();
UIComponent source = Components.getCurrentActionSource();
Take your pick. You could for example check the id of the form, command or source to see if it's the desired one.
My Idea/Requirement is to render dynamically generated jsf page and bind/capture the user filled data and save into db. Dynamically rendering part is working fine but not able to capture the user filled data back to my managedbean. I am using Jsf 2.1.
LoginManagedBean.java (requestscoped)
private String fieldName;
//getters and setters
public String trytry()
{
HtmlInputText inputText = (HtmlInputText) FacesContext.getCurrentInstance().getApplication().createComponent(HtmlInputText.COMPONENT_TYPE);
inputText.setId("it");
inputText.setValueExpression("value", FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createValueExpression(FacesContext.getCurrentInstance().getELContext(),"#{loginmb.keyValueMap['" + inputText.getId() + "']}", String.class));
panelGroupChilds.add(inputText);
grid.getChildren().add(panelGroupTextbox);
return "done";
}
public String save() {
System.out.println("fieldName --- " + this.fieldName);
return "save";
}
done.xhtml (dynamically rendered page)
<h:panelGrid binding="#{loginmb.docGridElems}"></h:panelGrid>
<h:commandButton id="cndB" value="SAVE" action="#{loginmb.save()}"></h:commandButton>
I am able to see the rendered textbox in done.xhtml but on submit the save button, I am not able to get the value of "feildName" (it is null). I am not able to find where i went wrong.
I even tried to bind the Map but could not able to get it.
I have created jsf form at backing bean. I have created form, it is being shown on screen with values successfully. When I click sumbit button, backing bean method is invoked with ActionEvent but updated input values are not submitted to backend. Backign bean entity object values are not updated.
Is there any wrong? Thanks so much for your helps,
Br.
Ramazan
HtmlForm form = new HtmlForm();
form.setId(appletWrapper.getName()+"_FORM");
PanelGrid panelGrid = (PanelGrid)createComponent(PanelGrid.COMPONENT_TYPE);
form.getChildren().add(panelGrid);
panelGrid.setColumns(4);
panelGrid.setId(appletWrapper.getName()+"_FORM_PANEL");
for (FieldWrapper fieldWrapper : appletWrapper.getFields()) {
panelGrid.getChildren().add(new UIFieldLabel(fieldWrapper).component(entities));
panelGrid.getChildren().add(newFieldComponent(fieldWrapper, entities));
}
HtmlPanelGroup panelGroup = new HtmlPanelGroup();
panelGroup.setId(appletWrapper.getName()+"_PANEL_GROUP");
panelGrid.getFacets().put("footer", panelGroup);
CommandButton commandButton = new CommandButton();
panelGroup.getChildren().add(commandButton);
commandButton.setId(appletWrapper.getName()+"_UPDATE");
commandButton.setAjax(true);
commandButton.setValue("update");
commandButton.setProcess("#this");
commandButton.setType("submit");
MethodExpression updateEntityME = createMethodExpression("#{mainBean.entityUpdateListener}", null, new Class[] {ActionEvent.class });
commandButton.addActionListener(new MethodExpressionActionListener(updateEntityME));
return form;
Edited: I gave all components a fixed id.
One of my coponents genarated like that;
InputText inputText = (InputText)createComponent(InputText.COMPONENT_TYPE);
inputText.setId(fieldAlphanumericWrapper.getName());
inputText.setValueExpression("value", createValueExpression("#{mainBean.selection."+fieldAlphanumericWrapper.getProperty()+"}", Object.class));
return inputText;
My backing bean target method;
public void entityUpdateListener(ActionEvent actionEvent) {
TAccount tAccount = (TAccount)getSelection();
System.out.println("tAccount.gettWebsite():"+tAccount.gettWebsite());
}
The main problem is that actually; When I press commandButton mainBean.setSelection is not invoked, so backing bean object is not updated. I cant take updated object instance through actionEvent instance.
I found solution, cause of problem was this line;
commandButton.setProcess("#this");
I have changed to this and problem solved.
commandButton.setProcess("#form");
Br.
Ramazan