0

I am new to JSF. I am using jsf on tomcat6 platform. I created my first test program with following file contents:

hoem.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">
<head>
   <title>JSF Tutorial!</title>
</head>
<body>
   #{userData.msg}
</body>
</html>

UserData.java

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "userData", eager = true)
@RequestScoped

public class UserData {
    private String msg = "Hello";

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

faces-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_1_2.xsd"
    version="1.2">

  <managed-bean>
<managed-bean-name>userData</managed-bean-name>
<managed-bean-class>UserData</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
  </managed-bean>
</faces-config>

and finally part of web.xml file:

<display-name>Z</display-name>
  <welcome-file-list>
    <welcome-file>home.xhtml</welcome-file> //Warning:file name references to "home.xhtml" that is not a page 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>
  <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>

I am getting following output:

#{userData.msg} 

Expected Output is:

Hello

Please help me to solve this problem. thanks in advance.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3189037
  • 51
  • 2
  • 7
  • What url do you use to access the application? – Seitaridis Sep 17 '14 at 10:52
  • Right Click on project and run on server(Tomcat) – user3189037 Sep 17 '14 at 10:55
  • How do you access the application in the browser? The problem appears because you don't access the application with the URL pattern(*faces* in your case) for the Faces Servlet. – Seitaridis Sep 17 '14 at 10:58
  • Your URL should be something like: http://localhost:8080/application_name/faces/home.xhtml. – Seitaridis Sep 17 '14 at 11:07
  • A good starting point for learning JSF is http://stackoverflow.com/tags/jsf/info. – Seitaridis Sep 17 '14 at 11:09
  • man this is one confusing example. I see JSF 2.x annotations on the backing bean yet the application is configured as JSF 1.2, with duplicate mappings in the faces-config for that backing bean. Copy/paste/everyone can do programming from different sources on the net? – Gimby Sep 17 '14 at 11:12
  • @Seitaridis after running using url: http://localhost:8080/Z/faces/home.xhtml. I am getting error saying:"java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config Caused by: java.lang.ClassNotFoundException - javax.servlet.jsp.jstl.core.Config" – user3189037 Sep 17 '14 at 11:31

1 Answers1

-2

As far as I know you can not use only #{userData.msg}. I think you got following exception.

{...} not allowed in a template text body

so you have to use <h:outputText value="#{userData.msg}"/> instead of only #{userData.msg} in your .xhtml page.

Updated:

Yes I was confused like BalusC said in comment JSP with Facelets. Above answer is correct if this was in JSP. Now I got solution by reading many examples. I tasted and succeed. Following is the full details.

  • Netbeans IDE
  • Tomcat 6
  • Jar files
    • jsf-api-2.1.7.jar
    • jsf-api-2.0.jar
    • jsf-impl-2.1.7.jar

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.tutorialspoint.test</groupId>
<artifactId>helloworld</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>helloworld 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.7</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.1.7</version>
    </dependency>
    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
<build>
    <finalName>helloworld</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>        
</project>

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">
<welcome-file-list>
    <welcome-file>faces/home.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>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>

UserData.java Managed Bean

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class UserData {

private String msg = "Hello";

public UserData() {
}

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}
}

home.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://java.sun.com/jsf/html">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    #{userData.msg}
</h:body>
</html>

Reference

JSF Quick Guide

I did not explain here because it is completely explained on above reference link. And I highly recommend you to see here for JSF which is best blog.

Yubaraj
  • 3,800
  • 7
  • 39
  • 57
  • No. It's not the problem. If I run only home.xhtml, then it is giving me proper output. – user3189037 Sep 17 '14 at 11:17
  • You're confusing JSP with Facelets. OP is using Facelets. JSP has been succeeded by Facelets almost 5 years ago. The same applies to whoever upvoted this wrong answer. – BalusC Sep 17 '14 at 11:37
  • @BalusC yes sir I was confused. Thank you for your remind. I found solution and updated answer. Finally I learned this but exactly what was the problem I could not find. Just solved. :) – Yubaraj Sep 18 '14 at 12:47