[英]Not able to access method of a managedbean from a xhtml page
这是我的项目结构: Eclipse项目结构
这是我的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的HEllo。 怎么了
以下是将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}"/>
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.1,JDK7和Netbeans 7.3,除了双重</h:body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.