【问题标题】:Not able to access method of a managedbean from a xhtml page无法从 xhtml 页面访问 managedbean 的方法
【发布时间】:2013-07-26 04:16:26
【问题描述】:

这是我的项目结构: eclipse project structure

这是我的 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!";
        }
    }

这是我的 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>

我输入 localhost:8080/demojsf/ 后得到的输出是 HELLO 形式而不是 来自jsf2的你好。 这里有什么问题?

【问题讨论】:

标签: jsf jsf-2


【解决方案1】:

下面是h:outputLabel使用h:outputLabelfor属性的方式

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

【讨论】:

  • 我使用了 但它仍然无法正常工作。还有什么是 中的 for="msgID"
  • for 是“此元素为其标签的组件的客户端标识符。”如果 不起作用,您可以尝试使用for 吗?我刚刚将名称命名为msgID。您可以为此指定任何其他名称..
  • 您在for 中提供的ID 应与您在h:outputText 中提供的ID 匹配
  • 仍然没有给出输出。
  • 是的,我已经删除了它并在之后尝试了......但它仍然没有给出输出
【解决方案2】:

h:outputLabel 将调用属性的 getter 方法。所以修改如下代码,

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

请在下面找到我的工作代码示例,

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

还有我的豆子

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

【讨论】:

  • 仍然输出页面只显示来自 jsf2 的 hello 而不是来自 jsf2 的 hello。
  • 这是我注意到的奇怪的事情。当我单击项目然后在服务器上运行它时,URL 是:localhost:8080/demojsf/,输出是来自 .但是当我只运行 index.xhtml 时,url 是:localhost:8080/demojsf/faces/index.xhtml 我得到正确的输出
  • 如果您查看 web.xml,您会发现问题所在。原因是您的 faces servlet 只会根据您的 web.xml 为模式调用。在您的第一个 url 中,没有调用 Faces servlet,因此不会发生 JSF 生命周期管理。将 index.jsp 作为欢迎页面并重定向到所需页面将解决您的问题
【解决方案3】:

您的代码在我这边运行良好。我使用了 JSF2.1、JDK7 和 Netbeans 7.3,除了双 &lt;/h:body&gt;

【讨论】:

  • 是的,代码现在可以工作了。但只有当我输入 localhost:8080/demojsf/faces/index.xhtml 时,当我使用 url localhost:8080/demojsf/ 时,它不会显示正确的输出即使我在 web.xml 中添加了 index.xhtml
  • 对我来说一切正常。你的 有这个 /faces/* 吗?如果有,试试这个,faces/index.xhtml
  • 是的,我和你说的一样……现在一切正常。谢谢。
猜你喜欢
  • 2017-10-10
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-23
相关资源
最近更新 更多