arjacsoh
arjacsoh

Reputation: 9232

Cannot get Jsf tag outputText appear

I am new in JSF and EJB applications, hence I encounter problems even in the simple JavaEE applications. I am creating a simple JavaEE application in eclipse with JBoss with goal just to try some tags of JSF and the binding of them to Java Beans. I cannot find out why the following JSf code does not get nothing appear to the output page:

<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">

    <f:loadBundle basename="resources" var="msg" />
    <head>
    <title><ui:insert name="pageTitle">Page Title</ui:insert></title>
    <style type="text/css">

    </style>
    </head>

<body bgcolor="#ffffff">
<h:body>
<h:outputText value="#{hello.world}" />
<h:outputText value="TTT" />
</h:body>   
</body>

</html>  

Not only the value hello.world coming form Bean, but a simple text "TTT" does not appear as well. The code of the Bean is:

@ManagedBean
public class Hello  {

final String world = "World";

/**
 * Default constructor. 
 */
public Hello() {
}

public String getWorld(){
    return "Hello" + world;
}

} 

The facelets-config.xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.1" 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_2_1.xsd">
 <managed-bean>
  <managed-bean-name>hello</managed-bean-name>
  <managed-bean-class>com.al.jsftest.Hello</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
 </managed-bean>

</faces-config>

When I try the tag with a plain text, it comes out on the screen, but it does not work again with #{hello.world}, namely the binding to the Bean fails. As soon as I have a have I hint to how get the JSF tag give successfully output, I would appreciate hint regarding what I should take care of, in order to get the Bean bind to JSF?

UPDATE: It seems to be the same problem with similar question, but my application is in Eclipse with JBoss, not in Netbeans with GlassFish. I add therefore my web.xml file, probably a modification in that is required, but I still cannot figure out it.

<?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>JsfTest</display-name>
  <welcome-file-list>
   <welcome-file>index.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>/faces/*</url-pattern>
 </servlet-mapping>

Upvotes: 0

Views: 3059

Answers (2)

arjacsoh
arjacsoh

Reputation: 9232

I created a new Dynamic Web Project and I added this time index.xhtml file using a "Blank Facelet Page" as template instead of "Common Facelet Page". I changed the url-pattern in my web.xml file as *.xhtml and it finally worked either by the full path name on the browser (\index.xhtml) or not. Following the same process with a "Common Facelet Page", I didn't manage to do it work. Strange.

I have also noticed, that after selecting "Common Facelet Page" and clicking next, a list of libraries appears and the user has the option to check some of them. I didn't check anything, maybe I should have.

Upvotes: 0

SimonSez
SimonSez

Reputation: 7769

You'll have to tell the server that your page should be loaded by the JSF-Servlet defined in your web.xml-file.
If I understand your sample right, you have a welcome file called index.xhtml which should be presented to the user with JSF by calling your website.

There is the problem:
All your pages won't be rendered by JSF unless you 'put them through' your defined JSF-Servlet.

Your servlet definition should look like this:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern> /* !important */
</servlet-mapping>

And therefore you have two (there are more for sure :) ) ways to tell the server to load your page with the JSF-Servlet:

  1. Tell your welcome-file to be loaded with the JSF-Servlet

    <welcome-file-list>
      <welcome-file>faces/index.xhtml</welcome-file> /* note the /faces/ prefix */
    </welcome-file-list>
    
  2. Extend your <url-pattern> inside the servlet-mapping

    <servlet-mapping>  
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
        <url-pattern>*.xhtml</url-pattern>  /*tell JSF to render all pages with xhtml-extension*/
    </servlet-mapping>
    

Ther shouldn't be any difference between Netbeans+Glassfish and JBoss+eclipse but you should check if the JSF libraries are loaded by the server.

One last note:
You're configurating your Beans via faces-config.xml, that'll do the job but since JSF 2.x you're able to do the same inside your class via annotations, e.g.

@ManagedBean
@SessionScoped  
public class TestBean {
  // your stuff here
}

For me this is much easier and more readable.

Hope this helped, have Fun!

Edit:
After your comments I've tested your set-up: nothing wrong!
At first I've got an error because of the <f:loadBundle basename="resources" var="msg" /> which was not defined in my set-up but after deleting this line everything worked fine.

Nevertheless, I've made a small typo in my first list item so please check this again.
Your xhtml-skeleton looked fine for me and works in my test, so there shouldn't be anything wrong either.

Please check the following:

  1. Are the JSF-libraries loaded either by the server or your application
  2. Are there anymore Servlets defined inside your web.xml-file
  3. What's the detailed error message

Cheers!

Upvotes: 1

Related Questions