java.lang.RuntimeException: Cannot find FacesContext [duplicate] - jsf

This question already has an answer here:
java.lang.RuntimeException Cannot find FacesContext
(1 answer)
Closed 7 years ago.
I'm new to JSF as i have started creating JSF app one week before i'm getting this exception
java.lang.RuntimeException: Cannot find FacesContext
i'm using Eclipse INDIGO
I'have tried with url pattern /faces/*, faces/HelloWorld.jsp, jsf/HelloWorld.jsp
can anyone tell me that which url we have to use when...??
my
web.xml
<display-name>JSFTutorial</display-name>
<welcome-file-list>
<welcome-file>HelloWorld.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>/jsf/*</url-pattern>
</servlet-mapping>
my faces.config.xml
<?xml version="1.0" encoding="UTF-8"?>
<managed-bean>
<managed-bean-name>helloWorldBean</managed-bean-name>
<managed-bean-class>com.myhomepageindia.jsftutorial.web.bean.HelloWorldBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>HelloWorld</display-name>
<from-view-id>/HelloWorld.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/HelloWorldResult.jsp</to-view-id>
</navigation-case>
</navigation-rule>
my Managed bean
package com.myhomepageindia.jsftutorial.web.bean;
public class HelloWorldBean {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCompleteName() {
return this.firstName + " " + this.lastName;
}
public String sayHelloWorld() {
return "success";
}
}
HelloWorld.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%#taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%#taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<%
try {
%>
<f:view>
<p>
<h:message id="errors" for="firstName" style="color:red" />
<h:message id="errors1" for="lastName" style="color:red" />
</p>
<h:form>
<h:outputText value="First Name"></h:outputText>
<h:inputText id="firstName" value="#{helloWorldBean.firstName}"
required="true"></h:inputText>
<h:outputText value="Last Name"></h:outputText>
<h:inputText id="lastName" value="#{helloWorldBean.lastName}"
required="true"></h:inputText>
<h:commandButton action="#{helloWorldBean.sayHelloWorld}"
value="Get Complete Name"></h:commandButton>
</h:form>
</f:view>
<%
} catch (Exception e) {
out.println(e);
}
%>
</body>

Based in your web.xml configuration, you should call the pages under "/jsf/" to made them work with the Faces Servlet. There are two possible solutions for this:
Make sure you're accesing your pages like this http://your.domain.com/YourProject/jsf/anyPAge.jsp
Try changing the configuration in your web.xml to something like
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
Then access your page like http://your.domain.com/YourProject/jsf/anyPAge.jsf
Note for the last option: if you're using JSF 1.x, you must not use *.jsp as the url-pattern, it will give you a huge error explained here: Help with JSF 1.2 + Jboss 5.1.0 (it doesn't mind if you'reusing Tomcat, you will have the same error). If you're using JSF 2.x, then there will be no problem, and you should be using Facelets (those pages with xhtml extension) as explained here: What is the difference between JSF and Facelets?
A very simple way to test if JSF is working in your project is to make this simple page:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%#taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%#taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<f:view>
<h:outputText value="Hello world!" />
</f:view>
</body>
</html>
If this page gives you errors, then there must be something else in your project or in your application server that is blocking the Faces Servlet.

Make sure you have all the correct Jars on your classpath.
Change the following things in your code:
Change your url-pattern in web.xml to *.jsf.
Wrap all your html code inside <f:view> tags
<%# page pageEncoding="UTF-8" %>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!doctype ... >
<f:view>
<html>
...
</html>
</f:view>
Now your URL should be:
localhost:8080/JSFTutorial/HelloWorld.jsf
Hope this helps!

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

What works for me is to just omit using the Taglib in my jsp file and move them into the html tag such as
<%# taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%# taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Simple jsp page</title></head>
<body>
<f:view>
<h:outputLabel value="Hello, world"/>
</f:view>
</body>
</html>
Becomes
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<head><title>Simple jsp page</title></head>
<body>
<f:view>
<h:outputLabel value="Hello, world"/>
</f:view>
</body>
</html>

Related

/enternameform.xhtml #17,59 value="#{register.firstName}": Target Unreachable, identifier 'register' resolved to null [duplicate]

This question already has answers here:
Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable
(18 answers)
Closed 6 years ago.
I am new to jsf and am getting a parse error when running a simple form. The error says "/enternameform.xhtml #17,59 value="#{register.firstName}": Target Unreachable, identifier 'register' resolved to null"
My code is below
enternameform.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">
<h:head>
<title>registration form</title>
<link href="./css/styles.css" rel="stylesheet" type="text/css"/>
</h:head>
<h:body>
<div align="center">
<h1 class="title">Enter Name and Register</h1>
<br/>
<fieldset>
<legend>Please Enter Name here</legend>
<h:form>
<!-- Your form elements here -->
First Name: <h:inputText value="#{register.firstName}"/> <br/>
Last Name: <h:inputText value="#{register.lastName}"/><br/>
<h:commandButton value="go ahead" action="#{register.fullName}"/>
</h:form>
</fieldset>
</div>
</h:body></html>
Register.java
package registrationform;
import javax.faces.bean.ManagedBean;
#ManagedBean
public class Register {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String fullName (){
if(isMissing(firstName)|| isMissing(lastName)){
return("missing");
}else{
return("registrationsuccess");
}
}
private boolean isMissing(String name){
return (name.trim().isEmpty());
}
}
missing.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">
<h:head>
<title>missing</title>
<link href="./css/styles.css" rel="stylesheet" type="text/css"/>
</h:head>
<h:body>
<h1 class="title">Please enter both First and Last Name to Register</h1>
Try again here
</h:body></html>
registrationsuccess.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">
<h:head>
<title>registrationsuccess</title>
<link href="./css/styles.css" rel="stylesheet" type="text/css"/>
</h:head>
<h:body>
<h1 class="title">You have completed registration</h1>
First Name: #{register.firstName} <br/>
Last Name:#{register.lastName}
</h:body></html>
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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- The bare minimum needed for JSF 2.2 is a servlet 2.5 or later
declaration (this uses 3.0) and the mapping for the FacesServlet.
Setting PROJECT_STAGE to Development is highly recommended
during initial development so that you get more helpful
error messages. Whether you want server-side state saving
(default) or client-side is a more complicated question:
client-side uses more bandwidth but fewer server resources.
Client-side also helps to avoid the dreaded view expired exceptions.
From JSF 2 and PrimeFaces tutorial
at http://www.coreservlets.com/JSF-Tutorial/jsf2/
-->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<description>State saving method: 'client' or 'server' (default). See JSF Specification section 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<!-- If you go to http://host/project/ (with no file name), it will
try index.jsf first, welcome.jsf next, and so forth.
-->
<welcome-file-list>
<welcome-file>enternameform.jsf</welcome-file>
<welcome-file>welcome.jsf</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Please let me know what I may be doing wrong here.
It has been fixed after restarting server n some edits

not able to move forward from one jsf page to another

I am trying to build a simple jsf application in which i take user id and name as parameter and trying to display on another page with the help of ManagedBean but so far i haven't succeeded and also the console is showing no error/exception, below are the list of files i'am using.
AddUser.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%# taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add New User Form</title>
</head>
<body>
<f:view>
<h:form>
<h:panelGrid border="1" columns="2">
<h:outputText value="ID"></h:outputText>
<h:inputText value="#{userBean.id}" required="true">
<f:validateLongRange minimum="1" maximum="500"/>
</h:inputText>
<h:outputText value="Name"></h:outputText>
<h:inputText value="#{userBean.name}"></h:inputText>
<h:commandButton action="#{userBean.addUser}"
value="Add Customer"></h:commandButton>
</h:panelGrid>
</h:form>
</f:view>
</body>
</html>
ManagedBean (UserBean)
package com.sapient.bean;
import java.io.Serializable;
public class UserBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String name;
//Action method to add user
public String addUser() {
return "success";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ListUser
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%# taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>List of Users</title>
</head>
<body>
<f:view>
<h:form>
<h:outputText value="User #{userBean.name} is added successfully.">
</h:outputText>
</h:form>
</f:view>
</body>
</html>
FacesConfig.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_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>userBean</managed-bean-name>
<managed-bean-class>com.sapient.bean.UserBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>AddUser</display-name>
<from-view-id>/AddUser.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/ListUser.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Archetype Created Web Application</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>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<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>
<description>
This parameter tells MyFaces if javascript code should be allowed in
the rendered HTML output.
If javascript is allowed, command_link anchors will have javascript code
that submits the corresponding form.
If javascript is not allowed, the state saving info and nested parameters
will be added as url parameters.
Default is 'true'</description>
<param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>
If true, rendered HTML code will be formatted, so that it is 'human-readable'
i.e. additional line separators and whitespace will be written, that do not
influence the HTML code.
Default is 'true'</description>
<param-name>org.apache.myfaces.PRETTY_HTML</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<description>
If true, a javascript function will be rendered that is able to restore the
former vertical scroll on every request. Convenient feature if you have pages
with long lists and you do not want the browser page to always jump to the top
if you trigger a link or button action that stays on the same page.
Default is 'false'
</description>
<param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
<param-value>true</param-value>
</context-param>
</web-app>
Please help, any word of advice will be appreciated.
Thanks
This is the problematic part of your code present in AddUser.jsp
<h:inputText value="#{userBean.id}" required="true">
<f:validateLongRange minimum="1" maximum="500"/>
</h:inputText>
As here you placed a required="true" in <h:inputText../> and I am damn sure that you are not giving the value in id field and you were just submitting the form by giving only name.So thats why your form is not submitting
Solution:
Now you have two solutions
1) Don't change even a single word in code and just give value in id textfield.(Don't think that 0) which you are seeing in id field as a default is a value which will pass the required field scenario
2) Remove tha required="true" part form here <h:inputText value="#{userBean.id}" required="true"> and you will see it running

java.lang.NullPointerException at javax.faces.webapp.UIComponentClassicTagBase.setJspId [duplicate]

This question already has answers here:
java.lang.NullPointerException at javax.faces.webapp.UIComponentClassicTagBase.setJspId
(2 answers)
Closed 7 years ago.
using jsf causes a NullpointerException:
WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at javax.faces.webapp.UIComponentClassicTagBase.setJspId(UIComponentClassicTagBase.java:1858)
at org.apache.jsp.jsf.list_jsp._jspx_meth_h_form_0(list_jsp.java:112)
at org.apache.jsp.jsf.list_jsp._jspService(list_jsp.java:86)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
In my lib folder of my glassfish-server and of my dynamic web-project are the following
jars:
standard-1.1.2.jar
jstl-jstl-1.2.jar
jsp-api-2.1
jsf-impl-2.1.7.jar
jsf-api-2.2.0-m03.jar
My Bean:
#Stateless
#Named
public class StatelessBean {
public int getNumberOfMovies() {
return 42;
}
public int getNumberOfPersons() {
return 42;
}
}
My jsf:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%# taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="../css/basic.css" type="text/css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Liste</title>
</head>
<body>
<h:form>
<h1>Stateless</h1>
<h:outputText value="Anzahl der Filme: #{StatelessBean.getNumberOfMovies()}" />
<h:outputText value="Anzahl der Personen: #{StatelessBean.getNumberOfPersons()}" />
</h:form>
</body>
</html>
The awnsers provided here: stackoverflow didnt help.
Dont know how to resolv this.
You should change your view code like this :
<!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">
<h:head>
<link href="css/basic.css" type="text/css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Liste</title>
</h:head>
<h:body>
<h:form>
<h1>Stateless</h1>
<h:outputText value="Anzahl der Filme: #{statelessBean.getNumberOfMovies()}" />
<h:outputText value="Anzahl der Personen: #{statelessBean.getNumberOfPersons()}" />
</h:form>
</h:body>
</html>
This is the way in JSF do declare taglibs.
You will also need to change some configurations in your web.xml and rename your file to yourfile.xhtml :
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<!-- Use prefix mapping for Facelets pages, e.g. http://localhost:8080/webapp/yourfile.jspx -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
To use your bean in EL expressions, you need to change it like this :
#ManagedBean
#RequestScoped
public class StatelessBean {
public int getNumberOfMovies() {
return 42;
}
public int getNumberOfPersons() {
return 42;
}
}
More info :
Custom tags in JSF 2.0
Try changing the call to getNumberOfMovies. JSF silently adds the word 'get' and the brackets '()' to the value in your html. The idea is that you write getters and setters and then just name the property and JSF calls the appropriate one.
<h:outputText value="Anzahl der Filme: #{statelessBean.numberOfMovies}" />
<h:outputText value="Anzahl der Personen: #{statelessBean.numberOfPersons}" />
However, this invokes a propertyNotFoundException. It may also be the cause of your null pointer but I'm not sure.

JSF redirect from page to page

I've a problem with JSF 1.1 (dinosaur, I know)
So there are two pages - index.jsp and test.jsp
I want to be redirected from index.jsp to test.jsp after clicking "Create" button
But nothing happens:/
p.s. and, in general, how do I log the things there, right know there is even no log of the clicking event! it's horrible
index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%-- jsf:pagecode language="java" location="/src/pagecode/Index.java" --%><%-- /jsf:pagecode --%>
<%#page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>index</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="GENERATOR" content="IBM WebSphere Integration Developer 6.2">
<link rel="stylesheet" type="text/css" title="Style"
href="theme/stylesheet.css">
</head>
<f:view>
<body>
<h:panelGrid columns="1" width="12%" cellpadding="10" rendered="true">
<h:commandButton value="Create" action="#{Controller.create}" />
</h:panelGrid>
</body>
</f:view>
</html>
Controller.java
public class Controller extends PageCodeBase {
public String create() {
return "success";
}
}
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<managed-bean>
<managed-bean-name>Controller</managed-bean-name>
<managed-bean-class>pagecode.Controller</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>index</display-name>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/test.jsp</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
</faces-config>
web.xml
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>test.jsp</welcome-file>
</welcome-file-list>
The JSF <h:commandButton> generates a HTML <input type="submit"> element which works only when nested in a HTML <form> element whose JSF equivalent is <h:form>.
However, you don't have a <h:form> anywhere in your view. Add it accordingly:
<h:form>
<h:commandButton value="Create" action="#{Controller.create}" />
</h:form>
By the way, if all your create method does is returning a fixed outcome, then you can also just specify the outcome directly in the action attribute:
<h:form>
<h:commandButton value="Create" action="success" />
</h:form>
By the way #2, using POST for page-to-page navigation is a bad practice, you should prefer output links for this.
See also:
commandButton/commandLink/ajax action/listener method not invoked or input value not updated - point 1
The create method should return a string. This string is the path to the page where you want to redirect.
Replace:
return "success";
For:
return "yourpage"

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