Primefaces dialog from bean only shows once - jsf

I'm trying to show a dialog from a bean as described in this PrimeFaces ShowCase. The thing is everything works as expected and dialog shows up, BUT if I close the dialog and then press the button again, the dialog won't show up unless the page is refreshed.
This is not the behavior shown in the example where every time the button is pressed the dialog shows up.
The only difference I have in my code is I have used CDI alternatives instead of managed beans package, because javax.faces.bean package will be deprecated. I mean:
javax.inject.Named instead of javax.faces.bean.ManagedBean
javax.faces.view.ViewScoped instead of javax.faces.bean.ViewScoped
In any case I've also tried with managed bean package but still the same wrong behavior.
This is what I have so far:
index.xhtml
<?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">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<p:commandButton value="Open" actionListener="#{viewDialogMB.viewDialog()}"/>
</h:form>
</h:body>
</html>
ViewDialogMB.java
import java.util.HashMap;
import java.util.Map;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.context.RequestContext;
#Named(value = "viewDialogMB")
#ViewScoped
public class ViewDialogMB {
public void viewDialog() {
Map<String,Object> options = new HashMap<>();
options.put("modal", true);
options.put("resizable", true);
RequestContext.getCurrentInstance().openDialog("dialog", options, null);
}
}
dialog.xhtml
<?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">
<h:head>
<title>Dialog Title</title>
</h:head>
<h:body>
<p:outputLabel value="Hello from Dialog!" />
</h:body>
</html>
faces-config.xml (as per Dialog Framework documentation)
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<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>
</faces-config>
By the way my platform (if makes any difference) is:
Glassfish 4
JSF 2.2
JDK 1.7 - 64 bits
Java EE 7
PrimeFaces 5.0 (community version)
I have tried with Mozilla Firefox, Google Chrome and MS IE11.

This bug was reported here Dialog Framework regression bug in PF 4.0.10 and higher and Issue 6915: PF 5.0 error when closing DF window opened from DataTable
As a workaround solution, use taconic's solution which is to add a panel inside the body.
Your dialog.xhtml would look like this :
<?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">
<h:head>
<title>Dialog Title</title>
</h:head>
<h:body>
<p:panel>
<p:outputLabel value="Hello from Dialog!" />
</p:panel>
</h:body>
</html>

Try using < p:dialog> they are really handy. Or you can do as "dic19" said and disable ajax as follows
<p:commandButton ajax="false" value="Open" actionListener="#{viewDialogMB.viewDialog()}"/>

Related

How to use JSF templates stored in a shared jar?

I am trying to store a set of JSF templates and other resources in a .jar file, which several webapps will draw from. I found this SO answer, which seems to provide instructions for the very thing I'm trying to do. However, while I can load images from the shared jar, I cannot get JSF templates to work. I get a file not found error.
I'm using Open Liberty 18.0.0.4, and the jar is stored in the ${server.config.dir}/lib/global directory, which I understand makes the contents available to all webapps.
Here is the output of jar tf of the shared jar.
META-INF/MANIFEST.MF
META-INF/resources/common/images/ufo.png
META-INF/resources/common/jsf/template.xhtml
This is the template.xhtml from the shared jar.
<?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">
<h:head>
<title>Title from template</title>
</h:head>
<h:body>
This is content from the template<br/>
</h:body>
</html>
Turning to my webapp, here is the directory layout.
/WEB-INF
/WEB-INF/classes
/WEB-INF/web.xml
/META-INF
/imageTest.xhtml
/localTemplate.xhtml
/localJSFTest.xhtml
/sharedJarJSFTest.xhtml
The imageTest.xhtml simply displays the ufo.png from the shared jar, and this facelet works without a problem.
<?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">
<h:head>
<title>Test Image</title>
</h:head>
<h:body>
I can see this image.<br/>
<h:graphicImage library="common" name="images/ufo.png" alt="ufo"/>
</h:body>
</html>
The localJSFTest.xhtml uses the localTemplate.xhtml, which is part of the webapp. This also works.
localJSFText.html
<?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">
<h:body>
<ui:composition template="localTemplate.xhtml" />
</h:body>
</html>
localTemplate.xhtml
<?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">
<h:head>
<title>Title from local template</title>
</h:head>
<h:body>
This is content from the LOCAL template<br/>
</h:body>
</html>
The problem comes when sharedJarJSFText.xhtml tries to use template.xhtml (posted above) from the shared jar.
sharedJarJSFText.xhtml
<?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">
<h:body>
<ui:composition template="/common/jsf/template.xhtml" />
</h:body>
</html>
I get the following error when I try to access sharedJarJSFText.xhtml in the browser.
/common/jsf/template.xhtml Not Found in ExternalContext as a Resource
viewId=/sharedJarJSFTest.xhtml
location=/home/jmac/Programming/NetBeansProjects/TestJSFWebApp/src/main/webapp/sharedJarJSFTest.xhtml
phaseId=RENDER_RESPONSE(6)
Caused by:
java.io.FileNotFoundException - /common/jsf/template.xhtml Not Found in ExternalContext as a Resource
at org.apache.myfaces.view.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:300)
I think I'm following the instructions in the above linked SO answer, so I'm not sure why this isn't working.
In another SO answer, user BalusC explains that since the Servlet 3.0 spec writing a custom ResourceResolver is unnecessary. This version of Open Liberty uses the Servlet 4.0 spec.
If helpful, here is the feature list from the server.xml for this Open Liberty server.
<featureManager>
<feature>jsp-2.3</feature>
<feature>jsf-2.3</feature>
</featureManager>
Thanks in advance for any suggestions.

Component library HTML basic does not contain datatable

I am creating a datatable using JSF pages but when I am trying to create one I am getting an error Component library HTML basic does not contain datatable, I have no idea why it doesn't contain this feature. I couldn't find anything to help this error through other sources.
<?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">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<ui:composition template="/Template.xhtml" >
<ui:define name="content">
<h:panelGrid>
<h:datatable value ="#{//somecode}">
</h:panelGrid>
</ui:define>
</ui:composition>
</h:body>
</html>
I was using datatable instead of the component dataTable

metadata tag not working in jsf

I am trying to set a variable with viewparam but I can't seem to get the code to compile in eclipse. It seems like it's not finding the tags.
I have the mojarra 2.2 used and I am inlcuding jsf-api-2.2.4 and impl also.
<?xml version="1.0" encoding="UTF-8" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" version="2.0">
<jsp:directive.page language="java"
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" />
<jsp:text>
<![CDATA[ <?xml version="1.0" encoding="UTF-8" ?> ]]>
</jsp:text>
<jsp:text>
<![CDATA[ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ]]>
</jsp:text>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<f:view>
test
<f:metadata>
<f:viewParam name="id" value="#{bowlingEvent.ID}" />
</f:metadata>
<h:form>
<h:inputText id="id" />
<h:commandButton id="button" value="Spara event" action="update">
</h:commandButton>
</h:form>
</f:view>
</body>
</html>
</jsp:root>
org.apache.jasper.JasperException: /update.jsp (line: 25, column: 13) No tag "metadata"
defined in tag library associated with uri "http://java.sun.com/jsf/core"
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
Your major mistake is that JSP is a deprecated view technology and is clearly a wrong tool for new projects. It has been succeeded by facelets, which is the default view technology for JSF 2.0+ projects.
Some tags, namely the ones used by you like <f:metadata> and <f:viewParam> are not available in JSPs (see sections 10.4.1.3 and 2.5.5 of JSF 2.2 specification (JSR-344) respectively).
The solution is straightforward: switch to facelets as the view technology.
It is also requested to switch to using the new namespaces that have been proposed since JSF 2.2, namely http://java.sun.com should now become http://xmlns.jcp.org (see Preface, page 8 of JSF 2.2 specification (JSR-344)), though both namespaces will work. Also see BalusC's comment to this answer and BalusC's answer to a similar question.

JSF Getters work, Setters not

I have a little problem with JSF,
I've made simple JSF page to learn:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<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:h="http://java.sun.com/jsf/html">
<h:head>
<title>register</title>
<meta http-equiv="Content-Type"
content="application/xhtml+xml; charset=UTF-8" />
</h:head>
<h:body>
<h:form>
<h:outputText value="Hello."/>
<h:inputText value="#{login.name}"/>
<h:outputText value="Password"/>
<h:inputText value="#{login.password}"/>
<h:button value="Getgreeeting" outcome="welcome"/>
</h:form>
</h:body>
</html>
And another page to show the values inserted to bean:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<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:h="http://java.sun.com/jsf/html">
<h:head>
<title>welcome</title>
<meta http-equiv="Content-Type"
content="application/xhtml+xml; charset=UTF-8" />
</h:head>
<h:body>
<h:outputText value="#{login.name}"></h:outputText>
<h:outputText value="Yours password #{login.password}"></h:outputText>
</h:body>
</html>
I've made some System.out.println() methods and they present that only getters in my beans works. Can somebody explain me why? What is solutionof my problem?
The <h:button> is not a submit button. It's a navigation button. Look closer at the Hello World example in your book/tutorial/resource (if you have any ..). You need a <h:commandButton> instead.
See also:
Difference between h:button and h:commandButton

Calling another page in JSF

I have a simple JSF application with 2 .xhtml files. When I run the application, the 1st page displayed is welcome.xhtml:
<?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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Final Project</title>
</h:head>
<h:body bgcolor="white">
<div align="center" style="border:5px outset blue;">Welcome to the Product Inventory Application</div>
<br></br>
<br></br>
<h:commandButton value="View All Products" action="allProducts"/>
</h:body>
It displays fine, but when I press the View All Products button, I expect it to display the allProducts.xhtml facelet. But when I click the button, nothing happens at all, no exception or anything. The allProducts.xhtml page is just:
<?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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>All Products</title>
</h:head>
<h:body bgcolor="white">
<h3>Test</h3>
</h:body>
</html>
The problem is that an UICommand (<h:commandButton>, <h:commandLink> and similars) must be inside a form i.e. <h:form>. Change your welcome.xhtml page to:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Final Project</title>
</h:head>
<h:body bgcolor="white">
<div align="center" style="border:5px outset blue;">Welcome to the Product Inventory Application</div>
<br></br>
<br></br>
<h:form>
<h:commandButton value="View All Products" action="allProducts"/>
</h:form>
</h:body>
More info:
h:commandLink / h:commandButton is not being invoked, reason 1. IMO, I suggest you to mark this question as a favorite if you're going to work a lot with JSF :).
I don't know the precise detail by heart, but here is the general idea. The action attribute of the button on the welcome page is referring to an 'allProducts' method of the backing bean. That method would have to return the string 'allProducts.xhtml' in order to have JSF present the products page.
So you have to introduce a backing bean for the welcome page and endow that class with a method 'allProducts'.

Resources