无法识别 selectItems itemValue,改为 returns 哈希码
selectItems itemValue not recognized, returns hash code instead
我有点卡住了 selectOneMenu
。
我有一个带有 Bean 和 DAO 以及 POJO 的 DB table foo
。感谢 BalusC's great tutorial,我已经建立了一个工作的 CRUD 页面。
Foo
具有列 id
、text
和 a1
到 a5
作为进一步的值。
菜单只显示哈希码列表,如"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;
}
}
问题是 Foo
是 ManagedBean
。我删除了条目,瞧,它起作用了。谢谢@BalusC!
我有点卡住了 selectOneMenu
。
我有一个带有 Bean 和 DAO 以及 POJO 的 DB table foo
。感谢 BalusC's great tutorial,我已经建立了一个工作的 CRUD 页面。
Foo
具有列 id
、text
和 a1
到 a5
作为进一步的值。
菜单只显示哈希码列表,如"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;
}
}
问题是 Foo
是 ManagedBean
。我删除了条目,瞧,它起作用了。谢谢@BalusC!