3

This is my code: 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>

and 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);
        }
    }    
}

I run and gives the following exception:

javax.servlet.ServletException: /botoes.xhtml: Property 'sorteiaBotao' not found on type BotaoBean javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)

I don't know how to solve. I did the same as tutorial example. Can anyone help me?

Thanks!

Deb
  • 431
  • 4
  • 12
  • 26

1 Answers1

4

The exception indicates that the actionListener="#{BotaoBean.sorteiaBotao}" is been treated as a value expression instead of a method expression (it's looking for a property and thus it's trying to just print the value returned by a getter, which obviously doesn't exist at all; as the exception says).

This in turn indicates that the whole component and the attribute are not been recognized by the JSF renderer.

This in turn indicates that the h: tag library of the component isn't (properly) been declared.

And indeed, you've there a dangling space in the taglib URI:

xmlns:h=" http://java.sun.com/jsf/html"

This space doesn't belong there in the taglib URI. Fix it accordingly:

xmlns:h="http://java.sun.com/jsf/html"

Don't forget to do the same for f: tag library, it has also a misplaced space in its URI.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • The reason for the exception I understand, you explained very well, but it was unclear what to do. How to declare the tag h? The two xmlns you wrote aren't equal? Sorry, but I'm using JSF shortly. Thank you! – Deb Aug 23 '12 at 11:28
  • Look closely at the taglib URI (the value within the quotes). You have placed a space in front of the URI, right before the "http" part. You should remove this space. – BalusC Aug 23 '12 at 11:29
  • After correcting the xmlns moved to the exception: javax.servlet.ServletException: javax.el.MethodNotFoundException: /botoes.xhtml @15,105 action="#{botaoBean.sorteiaBotao}": Method not found: BotaoBean@10d4437.sorteiaBotao() javax.faces.webapp.FacesServlet.service(FacesServlet.java:321) – Deb Aug 23 '12 at 12:25
  • The correct is to call the nameBean.nameMethod, isn't it? I do this: actionListener="#{BotaoBean.sorteiaBotao}" – Deb Aug 23 '12 at 12:27
  • That's better, you made progress! However, you changed `actionListener` to `action` for some reason, so you should remove the `ActionEvent` argument. See also http://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener Note that Java is case sensitive; `#{BotaoBean}` isn't the same as `#{botaoBean}`. The latter is the correct naming convention though and the former isn't. – BalusC Aug 23 '12 at 12:29
  • Ask a new question then. The new problem is not visible in the original code as you posted in the current question and also unrelated to it. – BalusC Aug 23 '12 at 13:01
  • Reading quietly decided to change the action and actionListener and solved!! Thank you so much BalusC! – Deb Aug 23 '12 at 13:11
  • Just to clarify my choice by actionListener. The action attribute must be associated with methods that represent a business rule application. While actionListener attribute must be associated with methods that implement some logic associated with the user interface. – Deb Aug 23 '12 at 13:19