0

I'm trying to using h:selectOneMenu but it doesn't send the values to the backbean. I tried sending just a simple value, a whole bean.

Here's the code:

<h:selectOneMenu value="#{campanha.campanha}" onchange="submit();">
  <f:selectItems value="#{campanha.campanhas}" var="c" itemLabel="#{c.nome}" itemValue="# c.codigo}"  />
</h:selectOneMenu>

`

bean:

@ManagedBean(name = "campanha")
@RequestScoped
public class CCampanha {

private List<Campanha> campanhas;
private Campanha campanha;

public void inicializar(ComponentSystemEvent event) {
    campanhas = new ArrayList<Campanha>();
    campanhas.add(new Campanha(1, "campanha 1"));
    campanhas.add(new Campanha(2, "campanha 2"));
    campanhas.add(new Campanha(3, "campanha 3"));
    campanhas.add(new Campanha(4, "campanha 4"));
    campanhas.add(new Campanha(5, "campanha 5"));
    campanhas.add(new Campanha(6, "campanha 6"));
    campanhas.add(new Campanha(7, "campanha 7"));

}

public String selecionarCampanha() {
    System.out.println(campanha.getCodigo());

    return "busca.jsf?faces-redirect=true";
}

public List<Campanha> getCampanhas() {
    return campanhas;
}

public void setCampanhas(List<Campanha> campanhas) {
    this.campanhas = campanhas;
}

public Campanha getCampanha() {
    return campanha;
}

public void setCampanha(Campanha campanha) {
    this.campanha = campanha;
}

}

When the form is submitted, this msg is shown in the console:

03/02/2014 14:37:12 com.sun.faces.renderkit.RenderKitUtils renderUnhandledMessages
INFO: AVISO: FacesMessage(s) foram enfileirados, mas podem não ter sido exibidos.
sourceId=j_idt12:j_idt16[severity=(ERROR 2), summary=(Erro de conversão ao definir o     valor '5' para 'null Converter'. ), detail=(Erro de conversão ao definir o valor '5' para 'null Converter'. )]

it means something like: couldn't convert '5' for 'null converter'. Conversion error.

I'm using Java, jsf2.2 and tomcat 6.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Victor Bello
  • 493
  • 1
  • 8
  • 23

2 Answers2

1

in your case try this, changes are here, by default jsf supports Integer(and more see this) convertors, you are submiting a object value="#{campanha.campanha}" so you need to create custom convertor,

@ManagedBean(name="campanha")
//someScoped     
public class Campanha {

}



    <h:selectOneMenu value="#{cCampanha.campanha.codigo}" onchange="submit();">
          <f:selectItems value="#{cCampanha.campanhas}" var="c" itemLabel="#{c.nome}" itemValue="# c.codigo}"  />
        </h:selectOneMenu>




        @RequestScoped
        public class CCampanha {

        private List<Campanha> campanhas;

        @ManagedProperty(value = "#{campanha}")
        private Campanha campanha;

        public void inicializar(ComponentSystemEvent event) {
            campanhas = new ArrayList<Campanha>();
            campanhas.add(new Campanha(1, "campanha 1"));
            campanhas.add(new Campanha(2, "campanha 2"));
            campanhas.add(new Campanha(3, "campanha 3"));
            campanhas.add(new Campanha(4, "campanha 4"));
            campanhas.add(new Campanha(5, "campanha 5"));
            campanhas.add(new Campanha(6, "campanha 6"));
            campanhas.add(new Campanha(7, "campanha 7"));

        }

        public String selecionarCampanha() {
            System.out.println(campanha.getCodigo());

            return "busca.jsf?faces-redirect=true";
        }

        public List<Campanha> getCampanhas() {
            return campanhas;
        }

        public void setCampanhas(List<Campanha> campanhas) {
            this.campanhas = campanhas;
        }

        public Campanha getCampanha() {
            return campanha;
        }

        public void setCampanha(Campanha campanha) {
            this.campanha = campanha;
        }
        }

by @ManagedProperty we inject managed bean into the property of another managed bean, a example bymkyong

Mohsin AR
  • 2,998
  • 2
  • 24
  • 36
  • 2
    Thanks for the help, but this doesn't make sense to me... Why should I use `@manageProperty`? I updated my question. – Victor Bello Feb 03 '14 at 16:39
  • i will clear it for you but could you post code of `Campanha` class – Mohsin AR Feb 03 '14 at 20:13
  • Please test answers on a real environment as long as you can't post correct answers from top of head. It's also very much appreciated if you elaborate the changes made and the reasoning behind those changes elaborated in easy to understand technical terms instead of merely cluelessly stating "try this" and then making it an annoying search&find game. – BalusC Feb 04 '14 at 07:51
  • @BalusC i have changed my answer – Mohsin AR Feb 04 '14 at 16:52
0

You could try to use the same var type in:

h:selectOneMenu -> value

and in

f:selectItems > itemValue

You are using an object in h:selectOneMenu -> value="#{campanha.campanha}"

And a primirive type in f:selectItems > itemValue="# c.codigo}"

Just replace this:

<h:selectOneMenu value="#{campanha.campanha}" onchange="submit();">

by this one:

<h:selectOneMenu value="#{campanha.campanha.codigo}" onchange="submit();">

Of course this solution works if campanha.campanha is never null, otherwise you get an error

Or you could try to declara an integer variable in CCampanha, and set this variable in h:selectOneMenu -> value

Led Machine
  • 7,122
  • 3
  • 47
  • 49