Target Unreachable, identifier 'user' resolved to null - jsf

So I have this very weird issue... I have tried searching all over StackOverflow and tried everything I could find, but I simply cannot fix this error from happening. The error is:
/index.xhtml #15,60 value="#{user.name}": Target Unreachable, identifier 'user' resolved to null
It's a school project and the project works for some of my classmates, but it also won't work for some. Could it be NetBeans screwing up when we open them? So far at least 2 people got it working, but at least 2 people also got this error.
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://java.sun.com/jsf/html">
<h:head>
<title>Welcome</title>
</h:head>
<h:body>
<h:form>
<h3>Please enter your name and password.</h3>
<table>
<tr>
<td>Name:</td>
<td><h:inputText value="#{user.name}"/></td>
<td>.</td>
<td><h:inputText value="#{user.userId}"/></td>
</tr>
<tr>
<td>Password:</td>
<td><h:inputSecret value="#{user.password}"/></td>
</tr>
</table>
<p><h:commandButton value="Login" action="welcome"/></p>
</h:form>
</h:body>
</html>
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:h="http://java.sun.com/jsf/html">
<h:head>
<title>Welcome</title>
</h:head>
<h:body>
<h3>Welcome to JavaServer Faces, #{user.name}.#{user.userId}!</h3>
<h3>your password is #{user.password}</h3>
</h:body>
<h:form>
<h:commandButton value="Logout" action="index"/>
</h:form>
</html>
UserBean.java
package com.corejsf;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
#Named("user")
#SessionScoped
public class UserBean implements Serializable {
private String name;
private String password;
private Integer userId;
public String getName() {
return name;
}
public void setName(String newValue) {
name = newValue;
}
public String getPassword() {
return password;
}
public void setPassword(String newValue) {
password = newValue;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer newValue) {
userId = newValue;
}
public String logout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "index";
}
}
What it basically does, is take any username, id, and password on index.xhtml. Then you press login, which sends you over to welcome.xhtml (because of action="welcome"). welcome.xhtml should then post your username, id, and password on the page. However, when I press the login button, I get this error:
/index.xhtml #15,60 value="#{user.name}": Target Unreachable, identifier 'user' resolved to null
I did try switching to ManagedBean (#ManagedBean(name="user")) but that resulted in the same thing.

I found a fix for this. Apparently, for some of us.., the imported project name was not matching the <context-root> element's name. All we have to do, is go into WEB-INF and find glassfish-web.xml. In that file, check what is inside <context-root> and either replace that with your project name or simply rename your project to whatever is inside that element.
Took a few hours to figure that out, but hopefully others will find this and appreciate my answer.

Related

Apache MyFaces 2.3-next-M6 | change of language don't work | it shows not the selected language

How can I fix the following error? The start language is German. If I select english it stays with the german text. However, if I then select German, I get English and if I then select English, I get German language. However, when the English text appears instead of the German text, "de" is displayed correctly next to language (Language de:) in the index.xhtml. To show which language is selected I use #{language.localeCode} in the index.xhtml. I use Apache MyFaces 2.3-next-M6 with Quarkus. What am I doing wrong?
index.xhtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.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" xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html" encoding="UTF-8" locale="#{language.localeCode}">
<f:loadBundle basename="messages" var="msg" />
<h:head>
<h:outputStylesheet name="primeflex/primeflex.css" />
</h:head>
<h:body>
<p:growl>
<p:autoUpdate />
</p:growl>
<h:form id="formlanguagechanger">
<h:panelGrid columns="2">
Language #{language.localeCode}:
<h:selectOneMenu value="#{language.localeCode}" onchange="submit()"
valueChangeListener="#{language.countryLocaleCodeChanged}">
<f:selectItems value="#{language.countriesInMap}" />
</h:selectOneMenu>
</h:panelGrid>
</h:form>
<h:outputText value="#{msg.title}" />
</h:body>
</f:view>
</html>
LanguageBean.java
#Named("language")
#SessionScoped
public class LanguageBean implements Serializable {
private static final long serialVersionUID = 1L;
private String localeCode;
#PostConstruct
public void init() {
localeCode = "de";
}
private static Map<String, Locale> countries;
static {
countries = new LinkedHashMap<String, Locale>();
countries.put("Deutsch", new Locale("de"));
countries.put("English", new Locale("en"));
}
public Map<String, Locale> getCountriesInMap() {
return countries;
}
public String getLocaleCode() {
return localeCode;
}
public void setLocaleCode(String localeCode) {
this.localeCode = localeCode;
}
// value change event listener
public void countryLocaleCodeChanged(ValueChangeEvent e) {
String newLocaleValue = e.getNewValue().toString();
// loop country map to compare the locale code
for (Map.Entry<String, Locale> entry : countries.entrySet()) {
if (entry.getValue().toString().equals(newLocaleValue)) {
FacesContext.getCurrentInstance().getViewRoot().setLocale((Locale) entry.getValue());
}
}
}
}
This code works fine for me in Mojarra 2.3.17.
In Myfaces 2.3.9, however, I could reproduce the described issue.
The root cause is that the <f:loadBundle> is not (re)initialized during view render time but during view build time only. This might be a bug in MyFaces.
I could work around the issue by replacing the <f:loadBundle> tag by this entry in faces-config.xml.
<application>
...
<resource-bundle>
<base-name>messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
It's at least not a Quarkus related problem. So I've fixed your question.
See also:
Localization in JSF, how to remember selected locale per session instead of per request/view

OutputLink not working in Flow Scope

Confirmation.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>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2" columnClasses="rightalign,leftalign">
<h:outputText value="Salutation: "></h:outputText>
#{registrationBean.salutation}
<h:outputText value="First Name: "></h:outputText>
#{registrationBean.firstname}
<h:outputText value="Age: "></h:outputText>
#{registrationBean.age}
<h:outputText value="Email: "></h:outputText>
#{registrationBean.email}
<!--
<h:panelGroup/>
<h:commandButton value="continue" action="register-return"></h:commandButton>
<h:commandButton value="back" action="register"></h:commandButton>-->
<h:outputLink value="register.xhtml">
<h:outputText value="back"></h:outputText>
</h:outputLink>
</h:panelGrid>
</h:form>
</h:body>
</html>
RegistrationBean.java
import java.io.Serializable;
import javax.faces.flow.FlowScoped;
import javax.inject.Named;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author rshingha
*/
#Named
#FlowScoped("register")
public class RegistrationBean implements Serializable {
private int age;
private String firstname;
private String salutation;
private String email;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getSalutation() {
return salutation;
}
public void setSalutation(String salutation) {
this.salutation = salutation;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
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">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:commandLink action="register">
<h:outputText value="Click here to register"></h:outputText>
</h:commandLink>
</h:form>
</h:body>
</html>
Functionality:
Here we are navigating from index.xhtml to "register" flow scope, containing register.xhtml, register-flow.xml, confirmation.xhtml
From register.xhtml we navigate to confirmation.xhtml.
Issues:
1) I want to make a "back" output link in confirmation.xhtml to navigate back to register.xhtml
<h:outputLink value="register.xhtml">
<h:outputText value="back"></h:outputText>
</h:outputLink>
But this is not working, its giving following error when I click on "back" on UI
"WELD-001303: No active contexts for scope type javax.faces.flow.FlowScoped"
Interesting thing is <h:commandButton> is working instead.
2) Why can't we directly run a file in directory ("register").
Its giving following error:
WELD-001303: No active contexts for scope type javax.faces.flow.FlowScoped
I am confused. Please help me?

Importing IceFaces crashes the webapplication

I would like to work with icefaces, but as soon as I import the libraries of icefaces, my webapplication crashes after clicking a button on the form which worked properly before the import.
What could go wrong?
The source Java:
package test,
import javax.faces.bean.ManagedBean;
#ManagedBean
public class Student {
private String firstName;
// create no-arg constructor
public Student() {
}
// define getter/setter methods
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
index.xhtml:
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Student Registration Form</title>
</h:head>
<h:body>
<h:form>
First name: <h:inputText value="#{student.firstName}" />
<br/><br/>
<h:commandButton value="Submit" action="student_response" />
</h:form>
</h:body>
student_response.xhtml
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Student Confirmation</title>
</h:head>
<h:body>
The student is confirmed: #{student.firstName}
</h:body>
</html>
The error I get:
you have 2 javax.faces. jars in classpath according to your first screenshot.
Please remove ONE of the faces jars in your classpath.

Passing parameter between pages

I'm unable to pass a simple parameter from one Facelet to another, and set a bean property...here's my code:
The calling page, main.xhtml (only relevant code):
<h:link outcome="index" value="disconnect" >
<f:param name="logout" value="true" />
</h:link>
The final page, 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">
<f:metadata>
<f:viewParam name="logout" value="#{indexBean.logout}"/>
<f:event type="preRenderView" listener="#{indexBean.redirect}" />
</f:metadata>
</html>
And the IndexBean (only relevant code):
#Named
#RequestScoped
public class IndexBean {
#Inject
private Logger logger;
private boolean logout;
public IndexBean() {this.logout = false;}
public void setLogout(boolean logout) {
logger.log(Level.DEBUG, "logout changed");
this.logout = logout;
}
public boolean isLogout() {return logout;}
public void redirect() throws IOException {
if(logout) {
//Never get in here
} else {
//Always here
}
}
}
I'm getting the URL parameter right on the link (http://localhost/index.xhtml?logout=true), but the setLogout method is never called.
I've even tried to change the logout type to String, change getter and setter properly and see what happens, but the setter is never called...
Any idea?
Thanks in advance!!

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

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

Resources