This question already has an answer here:
JSF returns blank/unparsed page with plain/raw XHTML/XML/EL source instead of rendered HTML output
(1 answer)
Closed 7 years ago.
I'm trying to follow this tutorial on jsf: https://netbeans.org/kb/docs/web/jsf20-intro.html
In the "Wiring Managed Beans to Pages" chapter you have to switch the HTML form element for an equivalent JSF HTML form component. But after I do so (simply commenting/uncommenting already present code) it isn't visible.
I've added xmlns:h="http://xmlns.jcp.org/jsf/html" to the html tag; I get no errors from the IDE; the form shows up in inspect element; I've also tried xmlns:h="http://java.sun.com/jsf/html" instead, but still nothing.
Here's my page:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
<!--<h:outputStylesheet name="css/stylesheet.css" />-->
<title>Greeting</title>
</head>
<body>
<div id="mainContainer">
<div id="left" class="subContainer greyBox">
<h4>Hi, my name is Duke!</h4>
<h5>I'm thinking of a number
<br/>
between
<span class="highlight">0</span> and
<span class="highlight">10</span>.</h5>
<h5>Can you guess it?</h5>
<!-- <form action="response.xhtml">
<input type="text" size="2" maxlength="2" />
<input type="submit" value="submit" />
</form>-->
<h:form>
<h:inputText id="userNumber"
size="2"
maxlength="2"
value="#{UserNumberBean.userNumber}"/>
<h:commandButton id="submit" value="submit" action="response"/>
</h:form>
</div>
<div id="right" class="subContainer">
<img src="duke.png" alt="Duke waving" />
<!--<h:graphicImage url="/duke.png" alt="Duke waving" />-->
</div>
</div>
</body>
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>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
JSF-Tags like <h:form> and <h:inputText> being returned to the client (your browser) instead of being rendered as proper HTML are a sure sign that your whole setup is wrong. (A browser will duly ignore any tags that it doesn't understand, that's why you don't get to see any of the contents of the form.)
If you are mapping your URL pattern like so:
<url-pattern>/faces/*</url-pattern>
you should then remove the second welcome file entry
<welcome-file>index.xhtml</welcome-file>
because this can never work, because it does NOT match against the pattern, so the whole request is NOT processed by FacesServlet in which case the plain .xhtml file is served as-is!
(It even says so explicitly in the tutorial you are referring to, at the end of the "Adding JSF 2.x Support to a Web Application" section.)
Related
I currently use mojarra javax.faces-2.2.8.jar and Apache Tomcat 6.0.26 Server.
I'm trying to add ajax behaviour to an input field(it's just for simplification).
Here is the simple facelets page(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:al = "http://xmlns.jcp.org/jsf/composite/AppearanceLibrary"
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/>
<h:body>
<h:form>
<h:outputLabel for = "login"
value = "login" />
<h:inputText id = "login" value = "#{authentication.submittedLogin}">
<f:ajax execute = "login" render = "out" />
</h:inputText>
<h:outputText id = "out" value = "#{authentication.submittedLogin}" />
</h:form>
</h:body>
</html>
Here is the HTML output:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head id="j_idt2"><script type="text/javascript" src="/JSFCars/javax.faces.resource/jsf.js.xhtml?ln=javax.faces&stage=Development"></script></head><body>
<form id="j_idt4" name="j_idt4" method="post" action="/JSFCars/welcome.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt4" value="j_idt4" />
<label for="j_idt4:login">login</label><input id="j_idt4:login" type="text" name="j_idt4:login" onchange="mojarra.ab(this,event,'valueChange','j_idt4:login','j_idt4:out')" /><span id="j_idt4:out"></span><input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="o5U6o+AhXUBnlwlaf5BbfJsXNxEe0OmBn+vyQUuLH1If1Hk802YaE1PT+ACraIlE+2rJMXEMJcDEgtaxMDURnPR0EFkR0Pdln4WoUMimfPxwbiCQCQcPfL+JDsPkyx+S6SvffiRyQFmz40B/lF5qtnt+AzzGhkYyuiuokiZVnJYv4hZBVhImLwKLm28dGvVh9qfnpnjngXm8cmAdIVULAY1sn1koFC2Bv702uBK0z9I6jHzMQFdPPCpBd5b+rri7R952i9cfi/CYGdcFSCq1yZGYL0s7E5Dshgaaikr5MVdJ4pRRQbiF60hypro+ZLavymy7G2pMukJNRaMhr7SxZffK13rGuCfpBXzXwdSeXZp2tMpWBhXJZ8gk/7rYzizmMgOTssZtj1inrPAgJdMSu2CJM2Q1xMV5wQSmSpU3bSHWdGknSMH4EGZsipKf8YCr3EsOSPD9khK8hlT5AmLm/iQ0j1xRHJny/6mIPJjVPPN/nD6AXBY+9bdt+SUPEKpT4u8in5ZulcG57t/9NUq/VA==" autocomplete="off" />
</form></body>
</html>
This is my deployment descriptor:
<?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"
id = "WebApp_ID"
version = "3.1">
<context-param>
<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>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<listener>
<listener-class>
com.sun.faces.config.ConfigureListener
</listener-class>
</listener>
<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>/welcome.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>welcome.xhtml</welcome-file>
</welcome-file-list>
</web-app>
Inside the browser's JavaScript console I got the message that this request http://localhost:8086/JSFCars/javax.faces.resource/jsf.js.xhtml?ln=javax.faces&stage=Development failed with 404(Not found). And as the result when I try to invoke the input field's ajax behaviour I get the following message: Uncaught ReferenceError: mojarra is not defined.
I'm aware that I should use <h:head> and <h:body>, otherwise the jsf.js wasn't included in first place. I've tried several browsers and the results were the same. Also I've tried the myfaces-core-2.0.2-bin jsf implementation.
I has found very similar question here. The problem was connected with the user defined filter which rejected the js request. But I
don't define any filters in the web deployment descriptor. Might there be any implicitly defined filter that blocks that request? Or are there any other ideas why something blocks that request?
Since the time I asked this question I installed "GlassFish Server Open Source Edition 4.1.2"
server and deployed the application on it(I included "javax.faces-2.2.8.jar" into project libraries,
but I think the server uses the internal "Mojarra 2.2.14" JSF implementation). As the way of testing
of the JSF implementation I tried using a template, a composite componet. <ui:composition>, <ui:define>
tags and custom library inclusion work as needed. authentication bean is created and perfectly initialized using faces-congig.xml. Then after it I removed everything not related to
the question and left only welcome.xhtml page. Again I got "404 Not found".
The solution is to remove everything connected to faces servlet from the web.xml.
The view of the rendered welcome.xhtml page after applying the solution
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/welcome.xhtml</url-pattern>
</servlet-mapping>
is very weird.... This would mean nothing but your welcome page is loaded via the Faces servlet. So the jsf.js which has an .xhtml extension in the name as well as you posted yourself isn't either. Fix the url pattern (or remove all the faces servlet references since the defaults are most often fine) and everything will work
I don't know how to continue, but I always get the "java.lang.RuntimeException: Cannot find FacesContext" for my new JSF 1.2 web application. I'm sure it's just some configuration I can't find.
The exception occurs with the first f: or h: tag. Already with the important <f:view> at the beginning.
My index.jsp
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<f:view>
<html>
<head>
<title>MyWebsite</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<div>MyContent</div>
</body>
</html>
</f:view>
My web.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.jsp</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>720</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
And then I also have a faces-config.xml that should reference myBean I want to use afterward in the body of the page:
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="1.2"
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">
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
</application>
<managed-bean>
<managed-bean-name>myClassName</managed-bean-name>
<managed-bean-class>
com.company.className
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
What am I missing here?
java.lang.RuntimeException: Cannot find FacesContext
Thus, the JSF <f:xxx> and <h:xxx> tags are complaining that FacesContext cannot be found. The FacesServlet is the one responsible for creating the faces context. The faces servlet is invoked when the request URL matches its URL pattern, which is in your particular case *.jsf. So, when you open the index.jsp as http://localhost:8080/context/index.jsp, or are relying on the <welcome-file> setting, then you are not invoking the faces servlet and you would indeed get this exception.
You need to open the index.jsp as http://localhost:8080/context/index.jsf, or to set the welcome file entry to index.jsf in order to properly invoke the faces servlet, so that it can create the faces context which is required by the JSF components declared in the JSP page.
Note however that only fixing the welcome file isn't sufficient in this JSF 1.x + Tomcat environment. You also need to supply a physically existing, but completely empty index.jsf file next to the index.jsp file in the webcontent in order to fool Tomcat that index.jsf really exists as welcome file. It would otherwise show a 404 error because it checks the physical presence of the welcome file beforehand.
See also:
javax.faces.FacesException: java.lang.RuntimeException: Cannot find FacesContext
Unrelated to the concrete problem, I'm wondering why you're using JSP if you've apparently installed Facelets 1.x and registered its view handler. Facelets is far superior to JSP.
I have a jsf project
and my pages are all referencing a template.xhtml
currently i have the following in the web.xml
<welcome-file-list>
<welcome-file>/pages/home/index.xhtml</welcome-file>
</welcome-file-list>
when i try to access the root directory now it shows me some content from that page as jsf code (not rendered) but everything from within the template vanished as well. It just shows the title and 2 lines of code.
All help appreciated
Check the url mapping for the Faces Servlet in your web.xml file, it's probably wrong.
A good one should be like the one at the JSF tag info page:
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
solved it
added the following in the web.xml
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
and created the following index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="refresh" content="0; faces/pages/home/index.xhtml">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
I am currently learning jsf. I have written A simple jsf application where my target is to navigate from one page to another page. However, I am unable to navigate at all.
In my code there are two jsp files; one is createcustomer.jsp and the other is viewCustomerDetails. After the user enters data in createcustomer.jsp and clicks on the button it should move to next page (where it will display entered data in previous page in this page).
Below is my code. Can anyone find where I'm going wrong?<
createCustomer.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>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>
<h1 style="background-color:blue;color:white;">Create a new Customer</h1>
</td>
</tr>
</table>
<f:view>
<table>
<tr><td>Name:</td><td><h:inputText value="#{customer.name}"></h:inputText></td></tr>
<tr><td>Address:</td><td><h:inputText value="#{customer.address}"></h:inputText></td></tr>
<tr><td>Phone Number:</td><td><h:inputText value="#{customer.phNumber}"></h:inputText></td></tr>
<tr><td>Email Address:</td><td><h:inputText value="#{customer.email_id}"></h:inputText></td></tr>
</table>
<table>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td><h:commandButton value="Create Customer" action="viewCustomerDetails" /></td></tr>
</table>
</f:view>
</body>
</html>
face-config.xml
<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>customer</managed-bean-name>
<managed-bean-class>com.app.view.Customer</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/createcustomer.jsp</from-view-id>
<navigation-case>
<!-- <from-action>#{customer.validate}</from-action> -->
<from-outcome>viewCustomerDetails</from-outcome>
<to-view-id>/viewCustomerDetails.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_3_0.xsd" version="3.0">
<display-name>Customer</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>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
</web-app>
Apparently, based on your code snipplet you have no <h:form> tag defined. Components such as <h:commandButton> need to be defined inside of one to function properly. Encase the content of your <f:view> inside an <h:form> and try again.
When running my JSF "Hello World" in server I get a File download message box which says "do you want to save this file, or find a program online to open it" which has three options find, save and cancel. When I click on 'find' following message is shown
"Windows has the following information about this MIME type. This page will help you find software needed to open your file.
MIME Type: application/xhtml xml
Description: UnKnown
Windows does not recognize this MIME type."
my xhtml is
<?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 lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Insert title here</title>
</h:head>
<h:body>
<f:view>
<h:outputText value="Hello World"></h:outputText>
</f:view>
</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" 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"
id="WebApp_ID" version="3.0">
<display-name>hospital</display-name>
<welcome-file-list>
<welcome-file>login.xhtml</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>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
If I run that particular xhtml file then its shown in IE. But any update to xhtml file will will be shown after refreshing the IE.
I am using Eclipse-helios IDE, Glassfish webserver. Thanks in advance
MSIE does indeed not support the content type of application/xhtml+xml. The page should be served with a content type of text/html (you can verify that with HTTP/web developer tools). But that should already be done by default JSF/Facelets configuration. You don't seem to have overridden the content type anywhere in the given code. One of the ways is defining it using the contentType attribute of the <f:view> tag. Another way is calling HttpServletResponse#setContentType() in some web filter. Or, completely different, perhaps it's been done afterwards by the webserver itself or by some proxy in the line.