0

I'm a little stuck with a selectOneMenu. I have a DB table foo with a Bean and DAO as well as a POJO. I have already established a working CRUD page thanks to BalusC's great tutorial.

Foo has the columns id, text, and a1 to a5 as further values.

The menu only displays a list of hash codes, like "com.example.beans.foo@3a3526c3", no matter what I put as itemValue. I can put in blabla as well, the result is the same.

Here's the JSF.

    <?xml version="1.0" encoding="UTF-8"?>
<!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://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view><h:form>
    <h:selectOneMenu value=#{fooBean.id}>

      <f:selectItems value="#{fooBean.list}" var="foo" itemValue="#{foo.id}" itemLabel="#{foo.text}" />

    </h:selectOneMenu>

  </h:column>

</h:form>
</f:view>
</html>

And here's foo:

@ManagedBean (name = "foo")
@ViewScoped
public class Foo implements Serializable {
    private String text;
    private String id;
//getters and setters
}

and fooBean:

@ManagedBean (name="fooBean")
@ViewScoped
public class FooBean implements Serializable {
    private List<Foo> list;
    private Foo foo = new Foo();
    private boolean edited;
    private String id;
    private String text;
    @EJB
    private FooDAO fooDAO;

    @PostConstruct
    public void init() {
        list = fooDAO.getAll();
    }

    public void add() {
        String id = fooDAO.insert(foo);
        foo.setId(id);
        list.add(foo);
        foo = new Foo();
    }

    public void edit(Foo Foo) {
        this.foo = Foo;
        edited = true;
    }

    public void save() {
        fooDAO.update(foo);
        foo = new Foo();
        edited = false;
    }

    public void delete(Foo Foo) {
        fooDAO.delete(Foo);
        list.remove(Foo);
    }

    public List<Foo> getList() {
        return list;
    }

    public Foo getFoo() {
        return foo;
    }

    public boolean isEdited() {
        return edited;
    }    
  public String getId() {
    return id;
}

public String getText() {
    return text;
}


}
cirko
  • 211
  • 2
  • 13
  • With "JVM memory addresses" in the first dropdown with ``, you actually mean the `toString()` result of the custom `Foo` class which look like `com.example.Foo@hashcode` by default? Exactly as you'd get when you do `System.out.println(foo)`? (hash code is no memory address btw). If so, then this suggests that you're actually running JSF 1.x not JSF 2.x. Which JSF version is logged during startup and which JSF version is your `faces-config.xml` declared to? – BalusC Oct 29 '15 at 08:57
  • my `faces-config` is declared to version 2.2, and on startup my server log says Mojarra 2.2.1. I first accidentaly indeed used 1.2, but replaced it in the dependencies with Mojarra 2.2.1 and javax.faces-api 2.2 after that. And after reading the tuts, I thought that for `String` values I didn't have to manually override `toString()` in my objects – cirko Oct 29 '15 at 14:05
  • Post a MCVE with (un)expected behavior exactly described. "JVM memory addresses" is not an exact description. "com.example.Foo@12345678" is an exact description. – BalusC Oct 29 '15 at 14:16
  • OK added the other modules to provide a MCVE as minimal as possible for the two issues and edited the question. If it's too much code, then I'll have to split up the question. – cirko Oct 29 '15 at 16:19
  • MCVE is explained here: http://stackoverflow.com/tags/jsf/info The table and other dropdowns are irrelevant to the problem. You can just reduce this from the question to make it better focused. – BalusC Oct 29 '15 at 16:23
  • ok I trimmed it down and will post the other problem as another question – cirko Oct 29 '15 at 16:58
  • Does this answer your question? http://stackoverflow.com/questions/6848970/how-to-prepopulate-hselectonemenu-with-entities-from-database – BalusC Oct 30 '15 at 09:31
  • I modified it (and this question too) according to the answer in that question; still, I get the same result, and no error messages. I know I shouldn't consider this too much, but even my IDE gives me the right values on autocompletion (.name, .id), so I assume that it's constructed right. I just can't find the error, and it's pretty frustrating. – cirko Oct 30 '15 at 10:16
  • 1
    The display is done by `itemLabel`, not `itemValue`. Your `Foo` class should not have been a `@ManagedBean` at all, it would only conflict with `#{foo}` from `var="foo"`. The `FooBean` contains code irrelevant to the problem, try creating a real MCVE instead of editing inline and untesting it. – BalusC Oct 30 '15 at 10:21

1 Answers1

0

The problem was that Foo was a ManagedBean. I removed the entry and voilà, it worked. Thanks @BalusC!

cirko
  • 211
  • 2
  • 13