PrimeFaces poll does not work - jsf

I need to use <p:poll /> in some other work. So I was trying out the PrimeFaces ShowCase code:-
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:rich="http://richfaces.org/rich"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:a4j="http://richfaces.org/a4j">
<h:form>
<h:outputText id="txt_count" value="#{counterView.number}" />
<p:poll interval="3" listener="#{counterView.increment()}" update="txt_count" />
</h:form>
</html>
And the backing bean is as below:-
package com.poll;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean (name="counterView")
#ViewScoped
public class CounterView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int number;
public int getNumber() {
return number;
}
public void increment() {
number++;
System.out.println(number);
}
}
It works as such: in browser number shows as 0 and doesn't change. In console I can see it printing as 1 once and then nothing.
What is wrong here? I am using PrimeFaces 3.4.2 on JSF 2.1

The p:poll tag does work for me with the following content, and I am on Primefaces 5.2.
NOTE: <h:head/> tag is needed and without that it does load primefaces related js files that are needed. I believe that is the reason why the refresh is not working in your case.
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head/>
<h:body>
<h:form>
<h:outputText id="txt_count" value="#{counterView.number}" />
<p:poll interval="3" listener="#{counterView.increment}" update="txt_count" />
</h:form>
</h:body>
</html>
And the managed bean:
#ManagedBean(name="counterView")
#ViewScoped
public class CounterView implements Serializable {
private int number = 100;
public int getNumber() {
return number;
}
public void increment() {
System.out.println("Incrementing....");
number++;
}
}
Can you try with these and compare if it works.
UPDATE: Final Solution
To summarize the solution, along the <h:head/> tag, upgrading to newer version (5.x) of Primefaces helped resolve the issue, as indicated in the comments section.

Related

f:param value is returning null both with commandButton and also commandLink

I am doing a simple navigation example in jsf as i am a beginner. i am always getting null when accessing the f:param value in the managedBean using ManagedProperty
home.xhtml
<!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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/facelets">
<head>
<title>JSF Tutorial!</title>
</head>
<body>
<h3>Using JSF outcome</h3>
<h:form>
<h:commandButton action="#{navigation.show}" value="Page1">
<f:param name="pageId" value="1" />
</h:commandButton>
<h:commandLink action="#{navigation.show}" value="Page2">
<f:param name="pageId" value="2" />
</h:commandLink>
<h:commandLink action="#{navigation.show}" value="Home">
<f:param name="pageId" value="3" />
</h:commandLink>
</h:form>
Navigation.java
package com.jason.jsf;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
#ManagedBean(name = "navigation", eager = true)
#RequestScoped
public class Navigation {
#ManagedProperty(value = "#{param.pageId}")
private String pageId;
public String show() {
System.out.println("page id" + value);
if (pageId == null) {
return "home";
}
if (pageId.equals("1")) {
return "page1";
} else if (pageId.equals("2")) {
return "page2";
} else {
return "home";
}
}
public String getPageId() {
return pageId;
}
public void setPageId(String pageId) {
System.out.println("page id set" + pageId);
this.pageId = pageId;
}
}
How is this caused and how can I solve it? I am using jsf2.2 Mojarra 2.0.3.there are other sample page1.xhtml and page2.xhtml just for navigation with me
Thanks in advance
Look closer at the XML namespace prefix and the URI and compare with whatever is shown in a decent JSF book/tutorial/resource:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/facelets">
Yes, the XML namespace URI for the f: prefix is wrong. You declared it to be the one of Facelets tags which have usually ui: prefix. This basically causes those tags to not be properly interpreted at all. It's being misinterpreted as an <ui:param> which has an entirely different meaning than the real <f:param>.
Fix the taglib URI. It needs to be http://java.sun.com/jsf/core. Here's the complete set:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
See also:
Our JSF wiki page - contains a Hello World
Unrelated to the concrete problem, Mojarra 2.0.3 is not JSF 2.2. It's JSF 2.0. And a rather old implementation too, over 5 years already. You can get latest Mojarra 2.2 (currently 2.2.11) at http://javaserverfaces.java.net. After that, you can change the domain in taglib URIs from java.sun.com to xmlns.jcp.org:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

p:graphicimage not working

I have been looking for a solution to my JSF problem a couple of days now and tried out all possible solutions. Nothing worked.
I would like to implement a jsf galleria, like this one. The code did not work, nothing was displayed in the page.
Here's my bean:
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
#ManagedBean(name="myGallery")
#ApplicationScoped
public class GalleriaBean {
private List<String> images;
private String effect = "fade";
#PostConstruct
public void init() {
images = new ArrayList<String>();
for(int i=1;i<=4;i++) {
images.add("gallery" + i + ".jpg");
}
}
public List<String> getImages() {
return images;
}
public String getEffect() {
return effect;
}
public void setEffect(String effect) {
this.effect = effect;
}
}
And the following code would be my xhtml content:
<!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"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:panelGroup>
<p:galleria effect="#{myGallery.effect}" var="image" effectSpeed="1000" styleClass=".ui-galleria-image-thumb-nav-left">
<ui:repeat value="#{myGallery.images}" var="image">
<h:graphicImage value="resources/images/#{image}" title="#{image}"/>
</ui:repeat>
</p:galleria>
</h:panelGroup>
</h:body>
</html>
As you might have noticed, I am using instead of . With this setting, all my images are thrown into the page, in a vertical list, with no fade transition or any other gallery type. If I replace with , my page is completely blank and the images are NOT displayed at all.
What could be the reason for that? What do I have to add, to make the gallery look like the one on the page above?
Okay, the problem seemed to be the fact that I did not put the PrimeFaces library (jar) in the lib folder in WEB-INF. Furthermore, I made sure I have setters and getters for all properties in the GalleriaBean. Hope this helps anyone.
hy,
with this code is run :)
<!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"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:panelGroup>
<p:galleria effect="#{myGallery.effect}" value="#{myGallery.images}" var="image" effectSpeed="1000" styleClass=".ui-galleria-image-thumb-nav-left">
<p:graphicImage name="pathDirectory/#{image}" title="#{image}"/>
</p:galleria>
</h:panelGroup>
</h:body>
</html>
you can the example in site PrimeFaces

javax.el.PropertyNotFoundException JSF 2.1 Apach Tomcat Eclipse Kepler [duplicate]

This question already has answers here:
Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable
(18 answers)
Closed 7 years ago.
I am following some tutorial about Java EE, trying to do an simple example on my own. Even I type exactly the same in my code, it won't work. The difference is (between mine and the example in the tutorial) that i am using Eclipse Kepler (not NetBeans) and Apache Tomcat (not GlassFish).
HTTP Status 500 - javax.el.PropertyNotFoundException: Target Unreachable, identifier 'greetingManager' resolved to null
Here's the code: of the index.xhtml:
<!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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Yo Digga Yo</title>
</h:head>
<h:body>
<h:form>
<h:inputText value="#{greetingManager.name}" />
<h:commandButton action="greet" value="click" />
</h:form>
</h:body>
</html>
that's the GreetingManager class:
package com.maja.greeting;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
#Named
#SessionScoped
public class GreetingManager implements Serializable{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGreeting() {
return "...";
}
}
ant finally, that's the greet.xhtml:
<!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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head></h:head>
<h:body>
<h2>#{greetingManager.greeting}</h2>
</h:body>
</html>
I've already imported the required .jar files for the #Named and #SessionScoped annotations, because tomcat doesn't provide them (?)
Ps. This tutorial is about CDI, so I kinda "have to" do this with the #Named Annotation :) And the code is not finished !
Your greetingManager class should be a managed bean in order to access it from your .xhtml file. To do so, you need to add the
#ManagedBean
annotation.

POST http://localhost:8080/Languages/WEB-INF/inventory.xhtml 404 (No Encontrado)

I am using facelets in my views with JSF 2.0. I have two backing beans, two xhtml view files and another xhtml template file. When I have the second view in the webapp directory and I change the language all is Ok but when I put mi second view into the WEB-INF folder and I change the language I have the following error in the chrome network console:
POST http://localhost:8080/Languages/WEB-INF/inventory.xhtml 404 (No Encontrado)
These are my backings beans:
LanguageBean.java:
package com.testapp;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
#ManagedBean(name = "language")
#SessionScoped
public class LanguageBean implements Serializable {
private static final long serialVersionUID = 1L;
private String localeCode;
private static final String DEFAULT_LOCAL_CODE = "es";
private static Map<String, Object> countries;
static {
countries = new LinkedHashMap<String, Object>();
countries.put("English", Locale.ENGLISH); // label, value
countries.put("Français", Locale.FRENCH);
countries.put("Español", new Locale("es"));
}
public Map<String, Object> getCountriesInMap() {
return countries;
}
public String getLocaleCode() {
if(localeCode == null) {
localeCode = DEFAULT_LOCAL_CODE;
}
return localeCode;
}
public void setLocaleCode(String localeCode) {
this.localeCode = localeCode;
}
public void changeLanguage() {
// loop a map to compare the locale code
for (Map.Entry<String, Object> entry : countries.entrySet()) {
if (entry.getValue().toString().equals(localeCode)) {
FacesContext.getCurrentInstance().getViewRoot().setLocale((Locale) entry.getValue());
}
}
}
}
InventoryBean.java:
package com.testapp;
import java.io.Serializable;
import java.util.Locale;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
#ManagedBean(name = "inventory")
#ViewScoped
public class InventoryBean implements Serializable {
private static final long serialVersionUID = 4576404491584185639L;
Logger logger = LoggerFactory.getLogger(InventoryBean.class);
// Failing path
private static final String INVENTORY_MAIN_VIEW = "/WEB-INF/inventory";
// Working path
// private static final String INVENTORY_MAIN_VIEW = "/views/inventory";
public String findProducts() {
try {
System.err.println("In inventory backing bean");
} catch (Exception exception) {
}
return INVENTORY_MAIN_VIEW;
}
public void changeLanguage() {
FacesContext.getCurrentInstance().getViewRoot().setLocale((Locale.ENGLISH));
}
}
These are the view files:
default.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/WEB-INF/header.xhtml">
<ui:define name="contentBody">
<f:view locale="#{language.localeCode}">
<h:form id="contentForm">
<h:outputText value="#{msg['internationalization.test']}" />
<p:commandButton id="gettingProducts"
value="Getting the products..." action="#{inventory.findProducts}" />
</h:form>
</f:view>
</ui:define>
</ui:composition>
inventory.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/WEB-INF/header.xhtml">
<ui:define name="contentBody">
<f:view locale="#{language.localeCode}">
<h:form id="contentForm">
<h:outputText value="#{msg['internationalization.test']}" />
</h:form>
</f:view>
</ui:define>
</ui:composition>
And this is the template xhtml file (header.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"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view contentType="text/html">
<h:head>
</h:head>
<h:body>
<div id="content">
<h:form id="headerForm">
<h:panelGrid columns="2">
<p:outputLabel value="Language :" for="languages" />
<h:selectOneMenu required="true" id="languages"
value="#{language.localeCode}">
<f:selectItems value="#{language.countriesInMap}" />
<p:ajax event="change" update="#all"
listener="#{language.changeLanguage}" />
</h:selectOneMenu>
</h:panelGrid>
</h:form>
<ui:insert name="contentBody" />
</div>
</h:body>
</f:view>
</html>
What is happening?
A HTTP 404 error simply means "Page Not Found". And indeed, files inside /WEB-INF folder are not publicly accessible. Put files which are intented to be publicly accessible outside /WEB-INF folder.
Note that this has further completely nothing to do with internationalization. You'd have had exactly the same problem when not doing anything with regard to internationalization (e.g. a simple navigation).
See also:
Which XHTML files do I need to put in /WEB-INF and which not?

Not able to access method of a managedbean from a xhtml page

Here is my project structure:
eclipse project structure
this is my HelloWorld.java
package com.tutorialspoint.test;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean(name = "helloWorld")
#RequestScoped
public class HelloWorld
{
public HelloWorld()
{
System.out.println("HelloWorld started!");
}
public String getMessage()
{
return "JSF2!";
}
}
and this is my index.xhtml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en"
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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<h:body>
HEllo form <h:outputLabel value="#{helloWorld.getMessage()}" />
</h:body>
</h:body>
</html>
the output that i get after entering localhost:8080/demojsf/ is HEllo form and not
HEllo from jsf2.
What is wrong here?
Below is the way of using h:outputLabel using for attribute of h:outputLabel
public class HelloWorld
{
public String message= "JSF2!";
public HelloWorld()
{
System.out.println("HelloWorld started!");
}
public String getMessage()
{
return message;
}
}
<h:outputLabel for="msgID" value="HEllo form " />
<h:outputText id="msgID" value="#{helloWorld.message}"/>
The h:outputLabel will be calling the getter method of the attribute. So change the code as below,
<h:outputLabel value="#{helloWorld.message}" />
Please find below a working code sample for me,
<!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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<head>
<title>JSF Hello World</title>
</head>
<body>
<f:view>
<h:form>
<h2>
<h:outputLabel value="#{jsfHelloWorldBean.message}" />
</h2>
</h:form>
</f:view>
</body>
</html>
And my bean
package devmanuals;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean(name="jsfHelloWorldBean")
#RequestScoped
public class JsfHelloWorldBean {
//String message;
public String getMessage(){
return "JSF!2";
}
}
your code works fine on my side. I used JSF2.1, JDK7, and Netbeans 7.3, well except for the double </h:body>

Resources