1

I am a newbie in JAVA EE.My issue is that the tag h:outputText not rendering in the browser. Iam using

  1. Eclipse JAVA EE IDE (Luna Service Release 1 (4.4.1))
  2. apache-tomcat-7.0.57
  3. Maven 3.2.5
  4. Java version 1.7

JAR files included in library: junit3.8.1, jsf-api-2.1.13,jsf-impl-2.1.12,javax.servlet-api-3.0.1

Following is my 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>jsfexample</display-name>
    <servlet>
        <servlet-name>faces</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>faces</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
   </servlet-mapping>
   <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list> 
</web-app>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.javacodegeeks.snippets.enterprise</groupId>
  <artifactId>jsfexample</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>jsfexample Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.13</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.13</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>jsfexample</finalName>
  </build>
</project>

index.xhtml

<!DOCTYPE html>
<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>JavaCodeGeeks</title>
</h:head>
<h:body>
    <b>Check B tag</b>
    <h:outputText value="#{helloWorldBean.msg}" />
</h:body>
</html>

JAVABEAN-helloWorld.java

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name="helloWorldBean")
@RequestScoped
public class HelloWorldBean {
    public String msg="test";
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
       this.msg = msg;
    }
    @PostConstruct
    private void init() {
        msg = "Hello World!! JFS example.. ";
   }
}

Please Let me know why the Tag is not rendering.Thanks in advance.

Leoman
  • 25
  • 5
  • What do you see on the browser, when the index.xhtml page is rendered? Is it left blank (white) in its entirely? Do you see some exceptions and/or some lines indicating `SEVERE`/`WARN` on the server log? Check to see if the `init()` method is properly invoked or not and the response status on the HTTP monitor - whether it is 200ok or not etc. – Tiny Jan 17 '15 at 14:17
  • In browser it shows the title bar and data enclosed within the tag(Check B tag). Only the h:outputext is not being passed to the browser, the http status says 200 OK. And there are no errors or warnings in the log – Leoman Jan 17 '15 at 18:39
  • I created project from your sources and it is working perfectly well, i.e. `h:outputText` is rendered, see [the result](https://github.com/destin/SO-answers/tree/master/SO-houtputtext-of-index-xhtml-not-rendering-in-the-browser). If it does not work for you there are two things that might go wrong: page is not rendered as JSF or value expression `#{helloWorldBean.msg}` is not evaluated properly. To check latter possibility change value to literal value, e.g. `value="Should be rendered"`. Use also [debug component](http://www.javabeat.net/jsf-2-ui-debug/) to help you find the problem. – Dawid Pytel Jan 17 '15 at 21:26
  • i chaged the value of outputText and it displayed the value in the browser. In the Debus component the value of Bean is not shown – Leoman Jan 18 '15 at 04:27
  • It seems like #{helloWorldBean.msg} is not reached the xhtml only. How can i fix this? – Leoman Jan 18 '15 at 04:28
  • If you replace `getMsg()` with one that returns hard-coded value: `public String getMsg() { return "Hello!"; }` - does it work then? If it works then you might have a problem with invoking method annotated with `@PostConstruct` (for possible causes see http://stackoverflow.com/questions/7234141/postconstruct-didnt-get-called-by-jsf-if-managedbean-is-inside-jar-library) – Dawid Pytel Jan 18 '15 at 13:23
  • Got the issue. When i created Maven project the JavaServerFaces of ProjectFacets in eclipse was not checked. Once i checked it and rebuild the project, it's working fine. My hearty Thanks to you **Dawid Pytel** , for your timely support. – Leoman Jan 18 '15 at 20:01

1 Answers1

-2

In fact it's nicer to put the servlet mapping in web.xml as follows

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

So you tell your applicication that any xhtml file called wirh prefix faces/ should be rendered by the faces Servlet.

  • Using virtual URLs is absolutely not nicer. It only unnecessarily adds an extra layer of confusion and maintenance. See also a.o. http://stackoverflow.com/questions/3008395/jsf-facelets-sometimes-i-see-the-url-is-jsf-and-sometimes-xhtml-why Moreover, you're not answering OP's concrete problem. – BalusC May 26 '15 at 12:50