问题描述
我正在尝试填充<h:selectOneMenu>
的列表.但是,我尝试检索的列表是另一个类的一部分,该另一个类是基类中的变量.
I am trying to populate the list of an <h:selectOneMenu>
. However, the list that I am trying to retrieve is part of another class that is a variable in the base class.
这里是我所拥有的,我不确定这是否可行或如何实现.
Here is what I have and I'm not sure if this is even possible or how to do it.
我有一个Citation
类,内容如下:
I have a Citation
class with the following:
public class Citation {
private int id;
private String title;
private Status status;
// getters and setters
}
然后我有一个Status
类,内容如下:
Then I have a Status
class with the following:
public class Status {
private int id;
private String name;
public List<Status> getAll() {
// goes to the database and gets a list of status objects
System.out.println("I was called!");
}
// getters and setters
}
然后在我的xhtml页面上,我具有以下内容:
Then on my xhtml page, I have the following:
<h:selectOneMenu id="citation_status" value="#{citation.status}">
<f:selectItems value="#{citation.status.all}" var="s"
itemLabel="#{s.name}" itemValue="#{s.id}" />
</h:selectOneMenu>
但是,这似乎根本没有调用Status
类中的getAll
方法.页面加载完成后,选择框为空,并且控制台没有getAll()
方法的I was called!
输出.
However, this doesn't seem to be calling the getAll
method in the Status
class at all. When the page finishes loading, the select box is empty and the console does not have the output of I was called!
from the getAll()
method.
我对JSF有点陌生,我已经尝试过尽我所能,但是我不确定在搜索时要使用的术语,因为它在技术上没有被称为子类",但这是我能想到的最好的名字,所以不用说,我搜索时一无所获.
I'm a bit new to JSF and I've tried searching the best that I can, but I'm not really sure the terminology to use when searching for this because it's not technically called a "subclass", but that's the best name I can come up with, so needless to say, I've come up with nothing from searching.
其他信息
如果有帮助:
- 我正在使用Glassfish作为我的应用程序服务器
- 我正在将Hibernate用于我的ORM
- 我正在使用JSF 2.0
推荐答案
显然#{citation.status}
返回了null
.
也就是说,这行不通. h:selectOneMenu value
必须与与f:selectItem itemValue
相同的类型匹配,本例中为int
.您将获得转换器异常.
That said, this isn't going to work. The h:selectOneMenu value
must match the same type as f:selectItem itemValue
which is in this case an int
. You would get a converter exception.
如何最好地解决这个问题取决于功能要求,这个问题尚不清楚.也许您需要f:selectItem itemValue="#{s}"
,或者您需要h:selectOneMenu value="#{citation.id}"
.
How to solve this the best depends on the functional requirement which is yet unclear in the question. Maybe you need f:selectItem itemValue="#{s}"
, or you need h:selectOneMenu value="#{citation.id}"
.
更新:关于转换器,这是一个启动示例:
Update: as to the converter, here's a kickoff example:
@FacesConverter(forClass=Status.class)
public class URLConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
try {
return someStatusDAO.findById(Long.valueOf(value));
} catch (SomeException e) {
throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to Status", value)), e);
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return String.valueOf(((Status) value).getId());
}
}
这篇关于JSF从子类中选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!