这是我的代码:botoes.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 xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h=" http://java.sun.com/jsf/html"
xmlns:f=" http://java.sun.com/jsf/core">
<h:head>
<title>K19 - Eventos</title>
</h:head>
<h:body>
<h:form>
<h:commandButton id="botao-jonas" value="Jonas" disabled="false" actionListener="#{BotaoBean.sorteiaBotao}" />
<h:commandButton id="botao-marcelo" value="Marcelo" disabled="true" actionListener="#{BotaoBean.sorteiaBotao}" />
<h:commandButton id="botao-rafael" value="Rafael" disabled="true" actionListener="#{BotaoBean.sorteiaBotao}" />
</h:form>
</h:body>
</html>
和 BotaoBean.java:
import javax.faces.bean.ManagedBean;
import javax.faces.component.*;
import javax.faces.event.ActionEvent;
@ManagedBean(name="BotaoBean")
public class BotaoBean {
public void sorteiaBotao(ActionEvent event) {
UIComponent formulario = event.getComponent().getParent();
UIComponent botaoJonas = formulario.findComponent("botao-jonas");
UIComponent botaoMarcelo = formulario.findComponent("botao-marcelo");
UIComponent botaoRafael = formulario.findComponent("botao-rafael");
botaoJonas.getAttributes().put("disabled",true);
botaoMarcelo.getAttributes().put("disabled",true);
botaoRafael.getAttributes().put("disabled",true);
double numero = Math.random();
if (numero<1.0/3.0) {
botaoJonas.getAttributes().put("disabled",false);
} else if (numero<2.0/3.0) {
botaoMarcelo.getAttributes().put("disabled",false);
} else {
botaoRafael.getAttributes().put("disabled",false);
}
}
}
我运行并给出以下异常:
javax.servlet.ServletException:/botoes.xhtml:在类型 BotaoBean javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)上找不到属性“sorteiaBotao”
我不知道怎么解决。我做了与教程示例相同的操作。谁能帮我?
谢谢!