简体   繁体   中英

Not able to access method of a managedbean from a xhtml page

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?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM