【问题标题】:selectItems itemValue not recognized, returns hash code instead无法识别 selectItems itemValue,而是返回哈希码
【发布时间】:2015-10-28 22:21:49
【问题描述】:

我有点卡在selectOneMenu 上。 我有一个带有 Bean 和 DAO 以及 POJO 的 DB 表 foo。感谢BalusC's great tutorial,我已经建立了一个有效的 CRUD 页面。

Foo 具有列 idtexta1a5 作为进一步的值。

菜单只显示哈希码列表,例如“com.example.beans.foo@3a3526c3”,无论我输入什么itemValue。我也可以输入blabla,结果是一样的。

这里是 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>

这里是foo

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

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


}

【问题讨论】:

  • 在带有&lt;f:selectItems var&gt; 的第一个下拉列表中使用“JVM 内存地址”,您实际上是指默认情况下看起来像com.example.Foo@hashcode 的自定义Foo 类的toString() 结果?和你做System.out.println(foo) 时得到的完全一样吗? (哈希码是没有内存地址顺便说一句)。如果是这样,那么这表明您实际上是在运行 JSF 1.x 而不是 JSF 2.x。启动期间记录了哪个 JSF 版本,以及您的 faces-config.xml 声明到哪个 JSF 版本?
  • 我的 faces-config 被声明为 2.2 版,并且在启动时我的服务器日志显示 Mojarra 2.2.1。我第一次意外确实使用了 1.2,但之后在依赖项中将其替换为 Mojarra 2.2.1 和 javax.faces-api 2.2。在阅读完 tuts 之后,我认为对于 String 值,我不必在我的对象中手动覆盖 toString()
  • 发布具有(未)预期行为的 MCVE。 “JVM 内存地址”不是准确的描述。 “com.example.Foo@12345678”是准确的描述。
  • OK 添加了其他模块,以便为这两个问题提供尽可能少的 MCVE 并编辑问题。如果代码太多,那么我将不得不拆分问题。
  • 显示由itemLabel 完成,而不是itemValue。你的Foo 类根本不应该是@ManagedBean,它只会与var="foo" 中的#{foo} 冲突。 FooBean 包含与问题无关的代码,请尝试创建一个真正的 MCVE,而不是编辑内联并取消测试。

标签: jsf jsf-2


【解决方案1】:

问题在于FooManagedBean。我删除了条目,瞧,它起作用了。谢谢@BalusC!

【讨论】:

    猜你喜欢
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多