Can't get PrimeFaces RequestContext.getCurrentInstance().openDialog() to work - jsf

Can't get PrimeFaces RequestContext.getCurrentInstance().openDialog() to work. I lifted the example code right out of the primefaces showcase, but I never get a dialog to open up. I'm using PF 5.1 running on Wildfly 8.2.0.Final. Any ideas what's up?
DFView.java
#ManagedBean(name = "dfView")
public class DFView {
public void chooseCar() {
RequestContext.getCurrentInstance().openDialog("selectCar");
}
public void onCarChosen(SelectEvent event) {
Car car = (Car) event.getObject();
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Car Selected", "Id:" + car.getId());
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
and my dialogplay.xhtml
<!DOCTYPE html>
<ui:composition xmlns="http://www.w3.org/1999/xhtml" template="/WEB-INF/templates/template.xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<ui:define name="body">
<h:form>
<p:growl id="growl" showDetail="true" />
<p:commandButton value="Select Car" icon="ui-icon-extlink" actionListener="#{dfView.chooseCar}">
<p:ajax event="dialogReturn" listener="#{dfView.onCarChosen}" update="growl" />
</p:commandButton>
</h:form>
</ui:define>
</ui:composition>

Please check "selectCar" is a valid Navigation Rule in your faces-config which references dialogplay.xhtml. (or use wittakarn's solution, which is easier)
If that is the Case, check that your faces-config contains the Dialog Framework Configuration (Page 519 in the Primefaces 5.1 Users Guid, it's easy to miss):
<application>
<action-listener>
org.primefaces.application.DialogActionListener
</action-listener>
<navigation-handler>
org.primefaces.application.DialogNavigationHandler
</navigation-handler>
<view-handler>
org.primefaces.application.DialogViewHandler
</view-handler>
</application>

Related

using javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL causes viewParam to be null

I'm trying to switch an application from WildFly 13 (Java EE 7, JSF 2.2.15) to WildFly 16 (Java EE 8, JSF 2.3.9). Tried both PrimeFaces 6.2 and 7.0
In web.xml javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL is set.
JSF 2.2 version working fine, switching to WildFly 16 this different behavior. A simple example follows:
Instructions:
access: http://localhost:8080/primefaces-test/?tipo=U&test=Bah
click first button, click second button. after ajax the viewParam value in the bean is null, even it is filled within the url.
Sample project (Jetty): https://github.com/erickdeoliveiraleal/primefaces-test/tree/update
XHTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html">
<f:metadata>
<f:viewParam name="test" value="#{testView.testString}" />
<f:viewAction action="#{testView.inicializar}" />
</f:metadata>
<h:head>
<title>PrimeFaces Test</title>
</h:head>
<h:body>
<h:form id="cadastro">
<h:commandButton value="click me"
action="#{testView.inserirNaLista()}">
<f:ajax execute="#form" render="#form" />
</h:commandButton>
<p:commandButton
value="click me - 2 (this button causes the null value)"
action="#{testView.inserirNaLista()}" />
</h:form>
</h:body>
</html>
Bean
#ManagedBean(name = "testView")
#ViewScoped
public class TestView implements Serializable {
private String testString;
public void inicializar() {
System.out.println("initializing: " + testString);
}
public void inserirNaLista() {
System.out.println(testString);
}
public String getTestString() {
return testString;
}
public void setTestString(String testString) {
this.testString = testString;
}
}
fixed in WildFly 21 https://github.com/eclipse-ee4j/mojarra/issues/4550 and still pending official release in eclipse-ee4j

commandLink only works with a h and p version

I'm having this really weird issue where my commandLinkonly work if I have BOTH links in the same view.
Works fine, both links:
<ui:composition template="../../template.xhtml" xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<ui:define name="content">
<h:form>
<p:dataTable var="v" value="#{myBean.myList}">
<p:column headerText="Name">
<h:commandLink value="Go to next page" action="#{myBean.doThing(v)}" />
<p:commandLink value="Go to next page" action="#{myBean.doThing(v)}" />
</p:column>
</p:dataTable>
<p:commandButton value="Submit" ajax="false" actionListener="#{myBean.submit}" />
</h:form>
</ui:define>
</ui:composition>
But if I remove either link, it doesn't even reach the method doThing() and reloads an empty page.
My bean:
#ManagedBean(name="myBean")
#RequestScoped
public class MyBean {
private List<Object> myList = new ArrayList<>();
public MyBean() {
}
public String doThing(Object obj) {
// This never prints if I remove one of the links
System.out.println("You're doing the thing!");
return "nextView?faces-redirect=true"
}
// Getters, setters etc.
}
I'm using JSF 2.0 and GlassFish 4.0.
How can I fix this and why is it happening?
I have a commandLink in a p:dataTable in another view and it's working fine.
EDIT:
Added the complete XML for the composition part of the view. The partial is inside a template:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<!-- css imports, titles etc -->
</h:head>
<h:body>
<ui:insert name="menu">
<ui:include src="./default/content.xhtml" />
</ui:insert>
</h:body>
</html>

Primefaces 5.0 tabs close listener not working

The TabCloseEvent is not getting fired if tabs generated from backing bean.
TabCloseEvent is working fine with normal xhtml tabs.
Here is my code
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Test Title</title>
</h:head>
<h:body>
<h:form id="form">
<p:growl id="growl" showDetail="true" />
<p:tabView id="tabview"
value="#{TestBean.tabList}"
var="tab"
widgetVar="tabviewV">
<p:ajax event="tabClose" listener="#{TestBean.onTabCloseAction}" update=":form:growl"/>
<p:tab id="tab" title="#{tab}" closable="true" >
<h:outputText value="#{tab}"/>
</p:tab>
</p:tabView>
</h:form>
</h:body>
</html>
the managed bean
#ManagedBean(name = "TestBean")
#ViewScoped
public class TestBean {
private List<String> tabList = new ArrayList<String>();
public TestBean() {
tabList.add("Test Tab 1");
tabList.add("Test Tab 2");
tabList.add("Test Tab 3");
tabList.add("Test Tab 4");
}
public void onTabCloseAction(TabCloseEvent event) {
System.out.println("Tab Closed Event : " + event.getTab().getTitle());
FacesMessage msg = new FacesMessage("Tab Closed", "Closed tab: " + event.getTab().getTitle());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public List<String> getTabList() {
return tabList;
}
public void setTabList(List<String> tabList) {
this.tabList = tabList;
}
}
I'm using Primefaces 5.0. also I tried using process="#this" in side p:ajax but result is same.
I tried the way it was mentioned in the showcase as well as in your code but does not work fully so i tried another approach lets have look
Facelets
<p:growl id="growl" showDetail="true" />
<p:tabView id="tabView" binding="#{homeBean.tabView}" >
<p:ajax event="tabClose" listener="#{homeBean.onTabClose}" update=":form:growl"/>
</p:tabView>
Managed Bean
private TabView tabView = new TabView();
Tab tab1 = new Tab();
tab1.setClosable(true);
tab1.setTitle("Business Partner");
Tab tab2 = new Tab();
tab2.setClosable(true);
tab2.setTitle("Manage Favorites");
tabView.getChildren().add(tab1);
tabView.getChildren().add(tab2);
now it seems to be working.
Note : I used Primefaces 5.0
This is a known bug.
It has been fixed in 5.0.2 version.
So you should just update your library to 5.0.2 or 5.0.3

JSF command button not working within included JSF page

I'm try to build application with Primefaces 4.0 and JSF 2.2.5. I need to load content dynamically in accordance with choosen menu item.
Here is my main page:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Hello, world!</title>
</h:head>
<h:body>
<p:layout fullPage="true">
<p:layoutUnit position="west" size="15%">
<h:form>
<p:commandButton value="List1" action="#{backingBean.setCurrentPage('included.xhtml')}"
update=":mypanel_id" process="#this"/><br/>
<p:commandButton value="List2"/>
</h:form>
</p:layoutUnit>
<p:layoutUnit position="center">
<p:panel id="mypanel_id">
<ui:include src="#{backingBean.page}"/>
</p:panel>
<p:messages autoUpdate="true" showDetail="true" showSummary="true"/>
</p:layoutUnit>
</p:layout>
</h:body>
</html>
And this is included page:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Hello,world</title>
</h:head>
<h:body>
<h:outputText value="Included page"/>
<h:form>
<ui:repeat value="#{backingBean.items}" var="item">
<p:fieldset legend="item" toggleable="true">
<h:outputText value="#{item}"/>
<ui:param name="it" value="#{item}"/>
<p:commandButton value="Click" style="margin-left: 50px;"
actionListener="#{backingBean.actionListener}"/>
</p:fieldset>
</ui:repeat>
<p:commandButton value="Test" action="#{backingBean.actionListener}"/>
</h:form>
</h:body>
</html>
Command buttons not work. Not with action nor with actionListener. What i'm doing wrong? How to build page with conditionally rendered elements, such as command buttons and fieldsets?
P.S. Forget to say, that my bean have request scope.
Updated:
public class BackingBean {
private List<String> items;
private String currentItem;
private String page;
public BackingBean() {
items = new ArrayList<String>();
}
public List<String> getItems() {
if (items.size() == 0) {
this.fillAndUpdate();
}
return items;
}
public void setItems(List<String> items) {
this.items = items;
}
public void fillAndUpdate() {
for (int i = 0; i < 5; i++) {
items.add("Item " + String.valueOf(i));
}
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public void setCurrentPage(String page) {
this.page = page;
}
public void actionListener() {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Test"));
}
}
UPDATED
Ok. I found the error (if someone interested). Because my bean has request scope, when i click the button in right panel my view was updated, but list of items was updated too and became empty (request scope). Now i keep selected menu in the session bean, and return that number when view rendered.
try to add ajax="false" in your command buttons

Primefaces p:messages does not display

Hi I have problem with display p:messages
I have this page:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form prependId="false">
<p:messages id="msgs" showDetail="true"/>
<p:commandButton value="Info" actionListener="#{messagesController.addInfo}" update="msgs"/>
<p:commandButton value="Warn" actionListener="#{messagesController.addWarn}" update="msgs"/>
<p:commandButton value="Error" actionListener="#{messagesController.addError}" update="msgs"/>
<p:commandButton value="Fatal" actionListener="#{messagesController.addFatal}" update="msgs"/>
</h:form>
</h:body>
and this is my bean to manage the messages
#ManagedBean(name = "beanMessageManager")
#SessionScoped
public class BeanMessageManager {
public void addInfo(ActionEvent actionEvent) {
FacesContext.getCurrentInstance().addMessage("form1:msgs", new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces rocks!"));
}
public void addWarn(ActionEvent actionEvent) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"Sample warn message", "Watch out for PrimeFaces!"));
}
public void addError(ActionEvent actionEvent) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Sample error message", "PrimeFaces makes no mistakes"));
}
public void addFatal(ActionEvent actionEvent) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Sample fatal message", "Fatal Error in System"));
}
}
When I click on some button then message isn't display.
What I'am doing bad?
Many thanks for response
Looks like you have the wrong bean name in actionListener.
The bean is named #ManagedBean(name = "beanMessageManager") but in the xhtml page you try to call actionListener="#{messagesController.addInfo}".
Maybe this would work:
<p:commandButton value="Info" actionListener="#{beanMessageManager.addInfo}" update="msgs"/>
<p:commandButton value="Warn" actionListener="#{beanMessageManager.addWarn}" update="msgs"/>
<p:commandButton value="Error" actionListener="#{beanMessageManager.addError}" update="msgs"/>
<p:commandButton value="Fatal" actionListener="#{beanMessageManager.addFatal}" update="msgs"/>
EDIT
Also you are missing the </html> tag at the end of you xhtml file. Also I think you should use msgs instead of "form1:msgs". Unfortunately I am not 100% sure on how primefaces works since I never used it.
The problem is that here actionListener="#{messagesController.addInfo}" you are calling a messageController that may not exists. You should be using the beanMessageManager instead.

Resources