0

Here is my project structure: eclipse project structure

this is my HelloWorld.java

 package com.tutorialspoint.test;
 import javax.faces.bean.ManagedBean;
 import javax.faces.bean.RequestScoped;
 @ManagedBean(name = "helloWorld")
 @RequestScoped
 public class HelloWorld
    {
       public HelloWorld()
        {
           System.out.println("HelloWorld started!");
        }
      public String getMessage() 
        {
          return "JSF2!";
        }
    }

and this is my index.xhtml

            <?xml version="1.0" encoding="ISO-8859-1" ?>
            <!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:ui="http://java.sun.com/jsf/facelets"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html">
            <h:head>
            <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
            <title>Insert title here</title>
            </h:head>
            <h:body>
            HEllo form  <h:outputLabel value="#{helloWorld.getMessage()}"  />
            </h:body>

            </h:body>
            </html>

the output that i get after entering localhost:8080/demojsf/ is HEllo form and not HEllo from jsf2. What is wrong here?

user1788048
  • 265
  • 1
  • 8
  • 18
  • 1
    Related: http://stackoverflow.com/questions/17544141/purpose-of-the-for-attribute-of-houtputlabel/ – BalusC Jul 26 '13 at 12:09

3 Answers3

2

Below is the way of using h:outputLabel using for attribute of h:outputLabel

public class HelloWorld
    {
       public String message= "JSF2!";
       public HelloWorld()
        {
           System.out.println("HelloWorld started!");
        }
      public String getMessage() 
        {
          return message;
        }
   }

<h:outputLabel for="msgID" value="HEllo form " />
<h:outputText id="msgID" value="#{helloWorld.message}"/>
Vikas V
  • 3,176
  • 2
  • 37
  • 60
1

The h:outputLabel will be calling the getter method of the attribute. So change the code as below,

<h:outputLabel value="#{helloWorld.message}"  />

Please find below a working code sample for me,

<!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"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich">
<head>
<title>JSF Hello World</title>
</head>
<body>
    <f:view>
        <h:form>
            <h2>
                <h:outputLabel value="#{jsfHelloWorldBean.message}" />
            </h2>           
        </h:form>
    </f:view>
</body>
</html>

And my bean

 package devmanuals;

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

    @ManagedBean(name="jsfHelloWorldBean")
    @RequestScoped
    public class JsfHelloWorldBean {
        //String message;
        public String getMessage(){
            return "JSF!2";
        }
    }
sk85
  • 407
  • 4
  • 17
  • Still the output page shows only hello from and not hello from jsf2. – user1788048 Jul 26 '13 at 07:17
  • Here is the weird thing i noticed. when i click on project and then run it on server the url is: localhost:8080/demojsf/ and the output is HEllo from . But when i run only the index.xhtml then the url is: localhost:8080/demojsf/faces/index.xhtml and i get correct output – user1788048 Jul 26 '13 at 07:44
  • 1
    if you look into the web.xml you will figure out the issue. The reason is your faces servlet will get invoked only for the pattern as per your web.xml. In your first url, the Faces servlet is not getting invoked and hence the JSF lifecycle management will not happen. Giving an index.jsp as your welcome page and redirecting to the desired page will solve your issue – sk85 Jul 26 '13 at 08:16
1

your code works fine on my side. I used JSF2.1, JDK7, and Netbeans 7.3, well except for the double </h:body>

van
  • 139
  • 2
  • yes the code works now.. but only when i enter localhost:8080/demojsf/faces/index.xhtml , it does not show the correct output when i use the url localhost:8080/demojsf/ even though i have added index.xhtml in the web.xml – user1788048 Jul 26 '13 at 08:02
  • 1
    it all works fine on me. does your has this /faces/*? and if it had, just try this, faces/index.xhtml – van Jul 26 '13 at 08:10
  • yes i had the same thing as you said... and everything works fine now.Thanks. – user1788048 Jul 26 '13 at 08:16