简体   繁体   English

无法从XHTML页面访问ManagedBean的方法

[英]Not able to access method of a managedbean from a xhtml page

Here is my project structure: eclipse project structure 这是我的项目结构: Eclipse项目结构

this is my HelloWorld.java 这是我的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 这是我的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. 输入localhost:8080 / demojsf /后得到的输出是HEllo形式,而不是jsf2的HEllo。 What is wrong here? 怎么了

Below is the way of using h:outputLabel using for attribute of h:outputLabel 以下是将h:outputLabel for 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. h:outputLabel将调用属性的getter方法。 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> 我使用了JSF2.1,JDK7和Netbeans 7.3,除了双重</h:body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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