Target Unreachable, identifier 'helloBean' resolved to null [duplicate] - jsf

This question already has answers here:
Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable
(18 answers)
Closed 6 years ago.
am new to Java EE and trying to get a simple JSF 2 tutorial example to work.
I am using the dynamic web project in eclipse and publishing to a Glassfish 3 server (run -> run on server). The first index.xhtml page loads correctly, but when I have to access a managed bean, the following error displays:
/index.xhtml #14,48 value="#{helloBean.name}": Target Unreachable, identifier 'helloBean' resolved to null
I've had a look at the various other discussions on this topic, however the solutions never seem to work for me (e.g. adding beans.xml, giving the managed bean a name etc, following naming conventions).
Any help would be appreciated, as I have been stuck with this for a while.
Here is the code I am currently working with:
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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body>
<h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
<h:form>
<h:inputText value="#{helloBean.name}"></h:inputText>
<h:commandButton value="Welcome Me" action="response"></h:commandButton>
</h:form>
</h:body>
</html>
response.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://java.sun.com/jsf/html">
<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body bgcolor="white">
<h3>JSF 2.0 Hello World Example - welcome.xhtml</h3>
<h4>Welcome #{helloBean.name}</h4>
</h:body>
</html>
Managed bean :
package java.hello1;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
#ManagedBean
#SessionScoped
public class HelloBean implements Serializable {
private static final long serialVersionUID = 1L;
private String name = "Ricardo";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Change the "#ManagedBean" by "#Named"

In case anyone stumbled on this old thread, I got the code to work by replacing .name with .getName() // the variable is private while getName() is public

Related

c:if and c:choose fail to conditionally build ui:composition [duplicate]

This question already has answers here:
understand the purpose of jsf ui:composition
(1 answer)
How to include another XHTML in XHTML using JSF 2.0 Facelets?
(2 answers)
Closed 5 years ago.
Been struggling with this for a bit now.
I have an Apache Shiro login.xhtml page that loads at startup.
Once authenticated, the user is redirected to an index.xhtml page in the same folder as the Apache Shiro login.xhtml page (both are in the webapp folder).
I want to prevent non authenticated users from loading the UI by using this in my 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:f="http://xmlns.jcp.org/jsf/core"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
xmlns:jsf="http://xmlns.jcp.org/jsf"
xmlns:p="http://primefaces.org/ui"
xmlns:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions">
<c:if test="${authUserDetail.userAuthenticated}">
<ui:composition template="/WEB-INF/templates/masterTemplate.xhtml">
<ui:define name="title">Title Page</ui:define>
<ui:define name="content">
<ui:include src="/WEB-INF/home/home_page.xhtml"/>
</ui:define>
</ui:composition>
</c:if>
<c:if test="${!authUserDetail.userAuthenticated}">
<h:outputLabel
value="You are not authorized to access this page."/>
</c:if>
</html>
With a backing bean AuthUserDetail.java looking like this:
package com.mycomp.view.shiro;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.omnifaces.cdi.ViewScoped;
import javax.annotation.PostConstruct;
import javax.inject.Named;
import java.io.Serializable;
#Named
#ViewScoped
public class AuthUserDetail implements Serializable {
private boolean userAuthenticated = false;
public AuthUserDetail() {
Subject currentUser = SecurityUtils.getSubject();
this.userAuthenticated = currentUser.isAuthenticated();
}
public boolean isUserAuthenticated() {
return userAuthenticated;
}
public void setUserAuthenticated(boolean userAuthenticated) {
this.userAuthenticated = userAuthenticated;
}
}
If I test without by replacing the <ui:composition... code (only using a single <h:outputLabel> it seems to work. But using it as is, always renders the complete page even if the user is not logged in (using Google Chrome's incognito feature).
What am I missing???

PrimeFaces poll does not work

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.

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

JSF 2.2 multipart form is not getting submitted [duplicate]

This question already has an answer here:
Upgrade JSF / Mojarra in JBoss AS / EAP / WildFly
(1 answer)
Closed 7 years ago.
When I upload a file in jsf 2.2 project with jboss 7.1
its prevent to submit form.
help me out....
here is sample code for upload a file
This is my welcome.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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
</h:head>
<h:body>
<h:form enctype="multipart/form-data" >
<h:outputLabel value="Upload File : " />
<h:inputFile value="#{myController.file}"/>
<h:commandButton value="Submit" action="#{myController.mycontroller}" />
</h:form>
</h:body>
</html>
Here is my backing bean MyController.java
public class MyController {
private Part file;
public Part getFile() {
return file;
}
public void setFile(Part file) {
this.file = file;
}
public String mycontroller() throws IOException {
System.out.println("--call mycontroller method--");
UploadFile uf = new UploadFile();
boolean b = uf.upload(getFile());
System.out.println(b);
return "success.xhtml";
}
}
Edit:1
I have one form with file upload and a <h:commandButton>, where I want to get uploaded file's information. Problem I am facing, the button is not firing when I added enctype="multipart/form-data". So any advice will be helpful to me.
I copy pasted your code and it worked as expected. The only addition I made is to add #ViewScoped and #ManagedBean annotation to MyController.

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.

Resources