I'm exploring the Faces Flow feature in JSF 2.2 and I'm getting the following error Target Unreachable, identifier 'flowScope' resolved to null when I run the tutorial in this page: http://www.mastertheboss.com/javaee/jsf/faces-flow-tutorial
The sample seems to be really simple, it only have one flow with 3 facelets, with this structure:
The flow is called signup, so I have a folder called signup in inside my WebContent folder, and 3 facelets, one of them with the same name as the flow as starting node, and a configuration file called signup-flow.xml.
This is the content of the starting node (signiup.xhtml):
<?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://xmlns.jcp.org/jsf/facelets" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Signup account</title>
<meta http-equiv="Content-Type"
content="application/xhtml+xml; charset=UTF-8" />
</h:head>
<h:body>
<h:form id="form1" styleClass="form">
<h1>Signup Account</h1>
<p>Name <h:inputText id="name" value="#{flowScope.name}" /></p>
<p>Surname: <h:inputText id="surname" value="#{flowScope.surname}" /></p>
<p>Email: <h:inputText id="email" value="#{flowScope.email}" /></p>
<p><h:commandButton id="page2" value="next" action="signup2" /></p>
</h:form>
</h:body>
</html>
This is my SignupBean:
package com.jsf.flow;
import java.io.Serializable;
import javax.inject.Named;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.flow.FlowScoped;
#Named
#FlowScoped(value="signup")
public class SignupBean implements Serializable {
private static final long serialVersionUID = 8112971305468080981L;
private boolean licenseAccepted;
public SignupBean() {
}
public String getHomeAction() {
return "/index";
}
public boolean isLicenseAccepted() {
return licenseAccepted;
}
public void setLicenseAccepted(boolean licenseAccepted) {
this.licenseAccepted = licenseAccepted;
}
public String accept() {
if (this.licenseAccepted) {
return "signup3";
} else {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "You have to read and accept the license!", "You have to read and accept the license!"));
return null;
}
}
}
And this is my signup-flow.xml:
<?xml version='1.0' encoding='UTF-8'?>
<faces-config
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"
version="2.2">
<flow-definition id="signup">
<flow-return id="homePage">
<from-outcome>#{signupBean.homeAction}</from-outcome>
</flow-return>
</flow-definition>
</faces-config>
I get the error when I click on the commandButton in the signup.xhtml, the code seems to be exactly the same as the one in the tutorial, I checked several posts with the same error but nothing seems to work for me.
This is the important part in the stack trace:
javax.el.PropertyNotFoundException: /signup/signup.xhtml #12,68 value="#{flowScope.name}": Target Unreachable, identifier 'flowScope' resolved to null
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
Worth to mention that I'm using GlassFish 4.
Found the problem, I was trying to start the flow calling the initial node using a link, like this:
<h:link id="link1" styleClass="link" value="Link" outcome="/signup/signup.xhtml"></h:link>
Instead I replaced the link with a commandButton and in the action parameter I used the flow name, like this:
<h:commandButton id="start" value="Signup User" action="signup"/>
And now it's working.
Related
I'm using Jakarta EE 9, Glassfish 6.0, and JSF 3.0 and getting "Target Unreachable, identifier '' resolved to null..." I know questions like this have been asked before but I tried all proposed solutions. Also, I could find very few discussions about version 3.0 of JSF. I would appreciate it if someone could help me spot what wrong is.
BookController.java
import entities.Book;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Inject;
import jakarta.inject.Named;
#Named
#RequestScoped
public class BookController{
private String test = "dada";
#Inject
private BookEJB bookEJB;
private Book book = new Book();
public String doCreateBook(){
bookEJB.createBook(book);
FacesContext.getCurrentInstance().addMessage(
null, new FacesMessage("Book created"));
return "newBook.xhtml";
}
public void doFindBookById(){
book = bookEJB.findBookById(book.getId());
}
public Book getBook() {
return book;
}
}
BookEJB.java
import entities.Book;
import jakarta.annotation.sql.DataSourceDefinition;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;
#Named
#jakarta.ejb.Stateless(name = "BookEJB")
public class BookEJB {
#PersistenceContext
private EntityManager em;
public BookEJB() {
}
public Book createBook(Book book){
em.persist(book);
return book;
}
public List<Book> findAllBooks() {
return em.createNamedQuery("findAllBooks", Book.class).getResultList();
}
public Book findBookById(Long id){
return em.find(Book.class, id);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>jakarta.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
</web-app>
faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="3.0" xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-facesconfig_3_0.xsd">
</faces-config>
beans.xml (in both WEB-INF and META-INF)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_3_0.xsd"
version="3.0"
bean-discovery-mode="all">
</beans>
layout.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"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title><ui:insert name="title">Default title</ui:insert></title>
<link rel="icon" href="https://i.loli.net/2021/06/12/im236Uy7FLhxCJ1.png"/>
</h:head>
<h:body>
<h:link value="Create a book" outcome="newBook.xhtml"/>
<h1><ui:insert name="title">Default title</ui:insert></h1>
<hr/>
<!-- <h:message for="errors"/>-->
<ui:insert name="content">Default content</ui:insert>
<hr/>
<h:outputText value="APress"></h:outputText>
</h:body>
</html>
newBook.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"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:composition template="layout.xhtml">
<ui:define name="title">Create a new book</ui:define>
<ui:define name="content">
<h:outputText value="#{bookController.test}"/>
<h:form id="bookForm">
<h:outputLabel value="Title: "/>
<h:inputText value="#{bookController.book.title}"/>
<h:outputLabel value="Price : "/>
<h:inputText value="#{bookController.book.price}"/>
<h:commandButton value="Create a book" action="#{bookController.doCreateBook}">
<f:ajax execute="#form" render=":booklist :errors"/>
</h:commandButton>
</h:form>
<hr/>
<h1>List of Books</h1>
<h:dataTable id="booklist" value="#{bookEJB.findAllBooks()}" var="bk">
<h:column>
<f:facet name="header">
<h:outputText value="Title"/>
</f:facet>
<h:link outcome="viewBook.xhtml?id=#{bk}" value="#{bk.title}"/>
</h:column>
</h:dataTable>
</ui:define>
</ui:composition>
</html>
It is because of Application Server that cannot fully support to JSF-3.0. I've got same error when I tested jakartaee-tutorial-examples/web/jsf/guessnumber-jsf project. I've tried guessnumber-jsf.war file on WildFly 24.0.0.Final and wildfly-23.0.2.Final Application servers. I got same error on both servers. But when I tested on Wildfly-preview-23.0.2.Final which supports JakartaEE 9.1 and JDK 11, it is working well.
I wrote a parser, which can parse Hiragana text to Romaji text.
Then I made a facelet which you can see on Picture1. If I enter any Hiragana text and I click the translate button, it shows garbage text in both textareas. The parser library has unit tests, and all tests are passed.
I also made a small JavaFX GUI using the same lib, and the bug is not present there. This bug only presents after I deploy it and run it in a browser. I'm using JSF 2.2 and Glassfish 4.1.0 as container.
Picture1:
When I re-enter the same text after this garbage was shown, it works well.
It works as it supposed to any other time.
You can see it on Picture2. I enter the very same text and it works well after the initial bug.
Picture2:
Here is the code of the 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"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Convert Hiragana, Romaji</title>
</h:head>
<h:body>
<h:outputStylesheet library="css" name="styles.css"/>
<f:view>
<div class="H_OuterDiv">
<h1><h:outputText value="Hiragana to Romaji" /></h1>
<h:form>
<div class="H_HiraganaTextArea">
<h3><h:outputText value="Enter Hiragana" /></h3>
<h:inputTextarea cols="30" rows="20" value="# {hconverter.hiraganaInput}"></h:inputTextarea>
</div>
<div class="H_MiddleDiv">
<div class="H_ButtonsDiv">
<h:commandButton value="Translate" action="# {hconverter.convertHiraganaToRomaji()}"></h:commandButton>
</div>
</div>
</h:form>
<div class="H_RomajiTextArea">
<h3>
Enter Romaji Text
</h3>
<h:inputTextarea value="#{hconverter.romajiOutput}" cols="30" rows="20" />
</div>
</div>
</f:view>
</h:body>
</html>
And here is the code of the managed bean. The HiraganaLettersNew class is a singleton.
#RequestScoped
#Named
public class Hconverter {
private String hiraganaInput = null;
private String romajiOutput = null;
public String getHiraganaInput() {
return hiraganaInput;
}
public void setHiraganaInput(String hiraganaInput) {
this.hiraganaInput = hiraganaInput;
}
public String getRomajiOutput() {
return romajiOutput;
}
public void setRomajiOutput(String romajiOutput) {
this.romajiOutput = romajiOutput;
}
public void convertHiraganaToRomaji() {
HiraganaLettersNew parser = HiraganaLettersNew.getInstance();
romajiOutput = parser.parseHiraganaString(hiraganaInput);
}
}
Could somebody please help me? I'm new to facelets and JavaEE, and I don't have any ideas why is this bug happening. Thank you in advance!
I found a solution.
This solution works with glassfish.
I had to create a file glassfish-web.xml in this structure. Take note it's a maven project.
{projectRoot}/src/main/webapp/WEB-INF/glassfish-web.xml
The file contains this code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD
GlassFish Application Server 3.1 Servlet 3.0//EN"
"http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
<locale-charset-info>
<parameter-encoding default-charset="UTF-8" />
</locale-charset-info>
</glassfish-web-app>
After a clean deploy, the bug is gone.
This question is a duplicate, I didn't know it when I asked.
See also:
It has been asked here before.
BalusC's blog
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.
I am trying to do a simple test before I dive into a large activity. But, here is where I was stuck. The test is to submit the JSF form, but the managed bean action never gets triggered.
<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:ice="http://www.icesoft.com/icefaces/component">
<f:view>
<head>
<title>A B C</title>
</head>
<body>
<h:form id="test">
<h:inputText value="demo"/>
<h:commandButton type="submit" value="A button" action="#{User.better}" immediate="true" />
</h:form>
</body>
</f:view>
</html>
Here is my managed bean
public class User {
public String send() {
System.out.println("Submitting data.....");
return null;
}
public void better() {
System.out.println("In better...");
}
}
I have set all the configurations correctly. I could be able to see the page. But,the control never gets into action method. How come? Any suggestions would be great.
UPDATE:
Here is my faces-conig.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
<application>
<view-handler>com.icesoft.faces.facelets.D2DFaceletViewHandler</view-handler>
</application>
<managed-bean>
<managed-bean-name>User</managed-bean-name>
<managed-bean-class>com.srk.beans.User</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
I have changed the managed-bean-name from User to user(small case) and changed the same in the .xhtml page as well, but seems to be same problem still.
Try user instead of User.Thus, try put user.better instead of User.better. Are you using jsf 2 or no? Also, post your faces-config file
Support for an action method with a void return type did not show up in JSF until v2.x. You must specify a return type of at least Object for better
public Object better() {
System.out.println("In better...");
return null;
}
I am new to JSF, Java EE and am having abit of a problem with a small test case I am trying out with Faces.xml, JSF and ManagedBeans..
I have a managedBean called beanManager.java and in it, I have two fields(name and testname) and a method called testcase1() that just returns a string. Now, in the frontpage.xhtml, I have an input text box that gets a name from a user and once the user clicks the submit button, the testcase1() method is called and all it does is just set the testname field of the BeanManager.java class with the user's input but for some reasons-obviously why I am sending this message- when the user enter's the name and hits the search button the page navigates to the displaypage.xhtml and the page displays nothing. It is supposed to show the name entered by the user but it is blank. I was advised to use #SessionScoped instead of #RequestScoped but the problem now is that when I deploy my Java EE application on glassfish, i get the following error:
Exception Occurred :Error occurred during deployment: Exception while loading the app :
java.lang.IllegalStateException: ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException:
javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException:
Error creating managed object for class: class org.jboss.weld.servlet.WeldListener
Below are my files..
BeanManager.java
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
#ManagedBean(name = "user")
#SessionScoped
public class BeanManager {
private String name = "";
private String testname = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String testcase1(){
setTestname(this.name);
return "test1";
}
public String getTestname() {
return testname;
}
public void setTestname(String testname) {
this.testname = testname;
}
}
frontpage.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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="WEB-INF/templates/origin.xhtml">
<ui:define name="content">
<f:view>
<h:form>
<h:panelGrid>
<h:inputText value="#{user.name}" required = "true"/>
</h:panelGrid>
<h:commandButton
action="#{user.testcase1()}"
value="Search"></h:commandButton>
</h:form>
</f:view>
</ui:define>
</ui:composition>
</html>
displaypage.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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="/WEB-INF/templates/origin.xhtml">
<ui:define name="content">
<h:panelGrid>
<h:outputText value="#{user.testname}"/>
<h:commandButton id="back" value="GoBack" action="frontpage"/>
</h:panelGrid>
</ui:define>
</ui:composition>
</html>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<navigation-rule>
<from-view-id>/frontpage.xhtml</from-view-id>
<navigation-case>
<from-action>#{user.testcase1()}</from-action>
<from-outcome>test1</from-outcome>
<to-view-id>/displaypage.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/displaypage.xhtml</from-view-id>
<navigation-case>
<from-outcome>GoBack</from-outcome>
<to-view-id>/frontpage.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
You can't use the CDI annotation javax.enterprise.context.SessionScoped with the JSF javax.faces.bean.ManagedBean. You either go pure JSF with
javax.faces.bean.ManagedBean(naming) and javax.faces.bean.SessionScoped (scoping)
OR Pure CDI
javax.inject.Named (naming) and javax.enterprise.context.SessionScoped(scoping)
Related:
Java EE 6 #javax.annotation.ManagedBean vs. #javax.inject.Named vs. #javax.faces.ManagedBean