Target Unreachable, identifier '<backingbean>' resolved to null in JSF 3.0 - jsf

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.

Related

Bean validation doesn't work

I'm trying to use Bean validation instead of JSF validation since it is a more DRY aproach, i'm using the annotations on my model but when i insert null data on jsf fields it does nothing...
None of the annotations that i'm using is having any effect, its like they are not there....
I'm using Tomcat 8
My 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 xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<h:body>
<h1>Cadastro de funcionario</h1>
<h:form>
<h:messages/>
<h:panelGrid columns="3">
<h:outputLabel value="Salário: R$ " for="campo-salario"/>
<h:inputText id="campo-salario" value="#{funcionarioBean.funcionario.salario}">
<f:convertNumber locale="pt_BR"/>
</h:inputText>
<h:message for="campo-salario" />
<h:outputLabel value="Código: " for="campo-codigo"/>
<h:inputText id="campo-codigo" value="#{funcionarioBean.funcionario.codigo}"/>
<h:message for="campo-codigo"/>
<h:outputLabel value="Data: " for="campo-aniversario"/>
<h:inputText id="campo-aniversario" value="#{funcionarioBean.funcionario.aniversario}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:inputText>
<h:message for="campo-aniversario"/>
<h:commandButton value="Cadastrar" action="#{funcionarioBean.mensagem}"/>
</h:panelGrid>
</h:form>
</h:body>
</html>
My Bean
#ManagedBean
public class FuncionarioBean {
private Funcionario funcionario = new Funcionario();
public void mensagem(){
System.out.println("Pressionado");
}
public Funcionario getFuncionario() {
return funcionario;
}
public void setFuncionario(Funcionario funcionario) {
this.funcionario = funcionario;
}
}
My Funcionario class
import java.util.Date;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
public class Funcionario {
#NotNull(message="{br.com.k19.Funcionario.nome")
#Min(value = 0)
private Double salario;
#NotNull
#Min(value = 5)
#Max(value = 19)
private Integer codigo;
#NotNull
private Date aniversario;
public Double getSalario() {
return salario;
}
public void setSalario(Double salario) {
this.salario = salario;
}
public Integer getCodigo() {
return codigo;
}
public void setCodigo(Integer codigo) {
this.codigo = codigo;
}
public Date getAniversario() {
return aniversario;
}
public void setAniversario(Date aniversario) {
this.aniversario = aniversario;
}
}
My lib
Can't post pictures yet.
Lib
>
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.6.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
hibernate-validator-5.0.0.final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
jsf-api-2.2.8.jar
jsf-impl-2.2.8.jar
mysql-connector-java-5.1.23.bin.jar
omnifaces-1.8.1.jar
primefaces-5.0.jar
validation-api-1.0.0.GA.jar
My web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>K19-Conversao-e-Validacao</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>com.sun.faces.writeStateAtFormEnd</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
</web-app>
Downloaded hibernate jars again to match the versions
Download validator: http://hibernate.org/validator/
Download ORM: http://hibernate.org/orm/
My lib
Working as intend now
ref: Bean Validation #NotNull, #NotBlank and #NotEmpty does not work in JSF+Tomcat

JSF Backing Bean (EJB) not called from JSF (using glassfish)

I'm trying to create a questionnaire website using GlassFish, JSF/Primefaces and for database, I'm using the embedded derby DB of Glassfish.
I followed this video tutorial (http://www.youtube.com/watch?v=aBjlR9HoR50) which is really good but I haven't managed to get my project to work properly.
My project deploys successfully on Glassfish but when I see the webpage on the browser, it is obvious that the backing bean is not called from the xhtml page.
Here is the backing bean code:
package ac.hw.questionnaire;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import ac.hw.questionnaire.model.Question;
/**
* Session Bean implementation class QuestionsEJB
*/
#Stateless
#LocalBean
#Named
public class QuestionsEJB {
private Logger logging = Logger.getLogger(this.getClass().getName());
#PersistenceContext (unitName="QuestionnaireEmbeddedDB")
EntityManager em;
private List<Question> questions;
private String name;
/**
* Default constructor.
*/
private EntityManager getEntityManager() {
if (em==null){
logging.info("EntityManager is null!");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("QuestionnaireEmbeddedDB");
em = emf.createEntityManager();
if (em==null){
logging.info("EntityManager is STILL null!");
}
}
return em;
}
public QuestionsEJB() {
// TODO Auto-generated constructor stub
logging.info("Logger: Constructor of QuestionsEJB called");
if (getEntityManager()==null){
return;
}
List resultList = em.createQuery("select q from Question q").getResultList();
if (resultList==null){
logging.info("ResultList is null");
}else{
logging.info("Constructor/Retrieved "+resultList.size()+" questions");
}
}
public List<Question> getQuestions() {
System.out.println("getQuestions method called");
if (questions==null){
this.questions = em.createQuery("select q from questions q").getResultList();
System.out.println("Retrieved "+questions.size()+" questions");
}
return questions;
}
public String getMyText(){
return "myTextFromEJB";
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
public String getName() {
this.logging.info("Get name!");
return name;
}
public void setName(String name) {
this.logging.info("Set name!");
this.name = name;
}
}
Here is my xhtml page:
<!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">
<ui:composition template="WEB-INF/template.xhtml">
<ui:define name="content">
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="myText" value="MyText is:"></h:outputLabel>
<h:outputText id="myText" value="#{QuestionsEJB.myText}">
</h:outputText>
</h:panelGrid>
<h:panelGrid columns="4" cellpadding="5">
<h:outputLabel for="name" value="Name:" style="font-weight:bold" />
<p:inputText id="name" value="#{QuestionsEJB.name}" />
<p:commandButton value="Submit" update="display" />
<h:outputText value="#{QuestionsEJB.name}" id="display" />
</h:panelGrid>
<br />
<p:dataTable var="qVar" value="#{QuestionsEJB.questions}">
<h:column>#{qVar.questionText}</h:column>
</p:dataTable>
</ui:define>
</ui:composition>
</html>
When I visit the xhtml page on the browser, none of the values are displayed and the Submit button doesn't do anything which tells me that the facelet doesn't bind to the EJB.
My web.xml (inside WebContent/WEB-INF/) has the following content:
<web-app version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >
<display-name>Questionnaire</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
My beans.xml file (also inside WebContent/WEB-INF) is empty (nothing at all).
My glassfish-web.xml file (also inside WebContent/WEB-INF) has the following content:
<!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>
<context-root>/QuestionnaireEmbeddedDB</context-root>
<parameter-encoding default-charset="UTF-8"/>
</glassfish-web-app>
I have no exceptions on the server.log and the only unorthodox thing is that the EntityManager is null until I explicitly call the getEntityManager() method on the QuestionEJB.
Why can't I get the xhtml to connect to the backing bean?
Also, the template.xhtml has the following 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:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title><ui:insert name="title">Questionnaire</ui:insert></title>
</h:head>
<body>
<ui:debug hotkey="x"
rendered="#{initParam['javax.faces.FACELETS_DEVELOPMENT']}" />
<div id="header">
<ui:insert name="header">
<h1>Welcome to the privacy questionnaire... (header section)</h1>
<!-- include your header file or uncomment the include below and create header.xhtml in this directory -->
<!-- <ui:include src="header.xhtml"/> -->
</ui:insert>
</div>
<h:body>
<h:form id="mainForm" enctype="multipart/form-data">
<div id="content">
<ui:insert name="content">
Content area. See comments below this line in the source.
<!-- include your content file or uncomment the include below and create content.xhtml in this directory -->
<!-- <div> -->
<!-- <ui:include src="content.xhtml"/> -->
<!-- </div> -->
</ui:insert>
</div>
</h:form>
</h:body>
<div id="footer">
<ui:insert name="footer">
<br />
<br />Powered by Glassfish & Eclipse.
<br />
<!-- include your header file or uncomment the include below and create footer.xhtml in this directory -->
<!--<ui:include src="footer.xhtml"/> -->
</ui:insert>
</div>
</body>
</html>

f:viewParam is not working

I am using JSF 2.2.4 on GlassFish 3.1.2.2.
I have this backing bean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.io.Serializable;
#ManagedBean(name="testMB")
#RequestScoped
public class TestMB implements Serializable {
public long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
And this view test.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:metadata>
<f:viewParam name="id" value="#{testMB.id}"/>
</f:metadata>
<h:body>
<h:inputText value="#{testMB.id}" />
</h:body>
</html>
When I open /test.html?id=123, then the ID shows up as 0 instead of 123. Why didn't <f:viewParam> do its job?
Updated
I installed GlassFish 4.0.
Maven dependencies for JSF:
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.2</version>
</dependency>
faces-config.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">
<!-- JSF and Spring are integrated -->
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
And test.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<f:metadata>
<f:viewParam name="id" value="#{testMB.id}"/>
</f:metadata>
<h:body>
<h:inputText value="#{testMB.id}" />
</h:body>
</html>
But the ID shows up as 0.
Maven project for test:
https://www.dropbox.com/s/qbc05vysspvt46l/jsf-spring-mybatis-master.zip
Problem solved
Pages named as *.xhtml.
In web.xml was:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Right:
<url-pattern>*.xhtml</url-pattern>

Kept getting Target Unreachable, identifier resolved to null in JSF web app

i have a simple problem here
i am making a simple jsf web app that can upload file
but i kept getting
Target Unreachable, identifier 'upload' resolved to null
here's my upload class
package SimpleLogin;
import java.io.IOException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.apache.commons.io.FilenameUtils;
import org.apache.myfaces.custom.fileupload.UploadedFile;
#ManagedBean
#RequestScoped
public class upload {
private UploadedFile uploadedFile;
public void submit() throws IOException {
String fileName = FilenameUtils.getName(uploadedFile.getName());
String contentType = uploadedFile.getContentType();
byte[] bytes = uploadedFile.getBytes();
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", fileName, contentType)));
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
}
and my index page
<!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:t="http://myfaces.apache.org/tomahawk"
>
<h:head>
<title>Facelet Title</title>
<h:outputStylesheet name="css/jsfcrud.css"/>
</h:head>
<h:body>
<ui:composition template="./template.xhtml">
<ui:define name="body">
<h:form>
<h:commandLink action="/contacts/List" value="Show All Contacts Items"/>
</h:form>
<h:form enctype="multipart/form-data">
<t:inputFileUpload value="#{upload.uploadedFile}" />
<h:commandButton value="submit" action="#{upload.submit}" />
<h:messages />
</h:form>
</ui:define>
</ui:composition>
oh yeah, here's my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/login.xhtml</welcome-file>
</welcome-file-list>
Sorry for the long delay
here my face config.xml
<faces-config version="2.0"
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">
<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>SimpleLogin.simpleLogin</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>bean</managed-bean-name>
<managed-bean-class>SimpleLogin.Upload</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope></managed-bean>
<navigation-rule>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-action>
#{simpleLogin.CheckValidUser}
</from-action>
<from-outcome>fail</from-outcome>
<to-view-id>/resultforfail.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-action>
#{simpleLogin.CheckValidUser}
</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/index.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<application>
<resource-bundle>
<base-name>/Bundle</base-name>
<var>bundle</var>
</resource-bundle>
</application>
and my template
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><ui:insert name="title">Default Title</ui:insert></title>
<h:outputStylesheet name="css/jsfcrud.css"/>
<h:outputStylesheet name="css/cssLayout.css"/>
<h:outputStylesheet name="css/default.css"/>
</h:head>
<h:body>
<div id="top">
<ui:insert name="top">Ini Header</ui:insert>
</div>
<h1>
<ui:insert name="title"></ui:insert>
</h1>
<p>
<ui:insert name="body"></ui:insert>
</p>
<div id="bottom">
<ui:insert name="bottom">Ini Footer</ui:insert>
</div>
</h:body>
what is the problem ? do i not clearly state the upload class ?
Any help is good :)
You have overridden the #ManagedBean by a <managed-bean> declaration on the faces-config.xml, however that <managed-bean> declaration has a different managed bean name, namely bean instead of upload.
You have basically 3 options:
Use #{bean} instead of #{upload} in your view.
Rename the <managed-bean-name> in faces-config.xml from bean to upload so that you can use #{upload} in your view.
Get rid of <managed-bean> altogether so that the #ManagedBean will be used. The managed bean name defaults to the bean class name with 1st char lowercased, thus #{upload} should work.
For the remaining, you are not consistent with Java naming conventions. Please work on that as well. Package names should be all lowercase and class names should be CamelCase and start with uppercase.

JSF error in the page - not able get or set values to the bean

I am to do a web application with JSF, just to get and put a value from the JSF to bean and vice versa. I think I have done everything properly but when I start the server and try to access my first page I get the following error
SEVERE: Servlet.service() for servlet [jsp] in context with path [/SimpleJSF] threw exception [/greeting.jsp (line: 20, column: 85) #{...} is not allowed in template text] with root cause
org.apache.jasper.JasperException: /greeting.jsp (line: 20, column: 85) #{...} is not allowed in template text
I am using Eclipse Helios with JDK 1.6 , apache Tomcat 7 and JSF 2.0 framework
Here is my code snippet
greeting.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Guess Number Facelets Application</title>
</h:head>
<h:body>
<h:form>
<h2>
Hi, my name is Duke. I am thinking of a number from
0 to 10.
Can you guess it?
</h2>
<p><h:inputText id="userNo" title="Type a number from 0 to 10:" value="#{resultNumber.userNumber}">
<f:validateLongRange minimum="#{resultNumber.minimum}" maximum="#{resultNumber.maximum}"/>
</h:inputText>
<h:commandButton id="submit" value="Submit" action="response.jsp"/>
</p>
<h:message showSummary="true" showDetail="false" style="color: #d20005; font-family: 'New Century Schoolbook', serif; font-style: oblique; text-decoration: overline"
id="errors1"
for="userNo"/>
</h:form>
</h:body>
</html>
response.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
<h:head>
<title>Guess Number Facelets Application</title>
</h:head>
<h:body>
<h:form>
<h2>
<h:outputText id="result" value="#{resultNumber.response}"/>
</h2>
<h:commandButton id="back" value="Back" action="greeting.xhtml"/>
</h:form>
</h:body>
</html>
Java bean, ResultNumber.java
package guessNumber;
import java.util.Random;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
/**
* Session Bean implementation class ResultNumber
*/
#Stateless(mappedName = "resultNumber")
#LocalBean
public class ResultNumber {
Integer randomInt = null;
Integer userNumber = null;
String response = null;
private long maximum=10;
private long minimum=0;
public ResultNumber() {
Random randomGR = new Random();
randomInt = new Integer(randomGR.nextInt(10));
System.out.println("Duke's number: " + randomInt);
}
public void setUserNumber(Integer user_number) {
userNumber = user_number;
}
public Integer getUserNumber() {
return userNumber;
}
public String getResponse() {
if ((userNumber != null) && (userNumber.compareTo(randomInt) == 0)) {
return "Yay! You got it!";
} else {
return "Sorry, " + userNumber + " is incorrect.";
}
}
public long getMaximum() {
return (this.maximum);
}
public void setMaximum(long maximum) {
this.maximum = maximum;
}
public long getMinimum() {
return (this.minimum);
}
public void setMinimum(long minimum) {
this.minimum = minimum;
}
}
Now for the configuration files
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>SimpleJSF</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>faces/greeting.jsp</welcome-file>
</welcome-file-list>
</web-app>
facet-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">
<managed-bean>
<managed-bean-name>resultNumber</managed-bean-name>
<managed-bean-class>guessNumber.ResultNumber</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
In JSF 2.0, JSP is been deprecated as view technology and succeeded by Facelets. You need to rename your .jsp files to .xhtml files. In your case you also need to remove the entire <%# page %> line in both JSPs. Then you need to invoke them with the .xhtml extension in the URL.
Further, you also need to remove the ConfigureListener from web.xml and you need to rename the JSP welcome file to XHTML. I'd also suggest to use *.xhtml instead of /faces/* as FacesServlet URL pattern. This way you don't need to put a /faces/* in URLs everytime. Finally you need to remove the bean from faces-config.xml and annotate the bean as follows instead of those javax.ejb annotations:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean
#RequestScoped
public class ResultNumber {
After all, it seems that you were reading JSF 1.x tutorials and mixing it up with JSF 2.x. You should be extremely careful what JSF version the JSF book/tutorial you're reading target. Since JSF 2.0 a lot of things are done differently.
You are using .jsp files,so you should import the taglibs manually.Add these two lines right above your opening html tag.
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

Resources