I have created entites and facde in netbeans. Now I want to create a native query to get only two feilds from the table. And then Bind them to the
String query = "SELECT DISTINCT(m.idManufacturer),m.hmid "
+ " FROM model m";
@SuppressWarnings("unchecked")
List<model> modelList = (List<model>) g开发者_Go百科etEntityManager().createNativeQuery(query).getResultList();
and then I used this code
<f:selectItems itemLabel="#{myController.modelList.idManufacturer}"/>
to show values in dropdown. But that doesn't seem working. Any ideas?
Try creating list of selectItems from your List<Model>
and give that in value attribute.
e.g.
List<SelectItem> modelList = new ArrayList<SelectItem>();
//implement your conversion label value here.
<f:selectItems value="#{myController.modelList}"/>
If you're on JSF 1.x, you need to provide a SelectItem[]
, List<SelectItem>
or Map<String, Object>
to the value
of f:selectItems
. The most common choice is a List<SelectItem>
. Assuming that you want to use Model#getIdManfacturer()
(which I assume to be a Long
) as both item label and value, here's an example:
public class Bean {
private Long selectedManfacturerId; // +getter +setter
private List<SelectItem> selectManfacturerIds; // +getter
@PostConstruct
public void init() {
selectManfacturerIds = new ArrayList<SelectItem>();
List<Manfacturer> manfacturers = getItFromDatabaseSomehow();
for (Manfacturer manfacturer : manfacturers) {
selectManfacturerIds.add(new SelectItem(manfacturer.getManfacturerId()));
}
}
// ...
}
in combination with
<h:selectOneMenu value="#{bean.selectedManfacturerId}">
<f:selectItems value="#{bean.selectManfacturerIds}" />
</h:selectOneMenu>
However, if you're already on JSF 2.0, then you can also just use a List<SomeBean>
for this. The new var
attribute in a JSF 2.0 <f:selectItems>
allows you to declare the iterated item in the view. Kickoff example:
public class Bean {
private Long selectedManfacturerId; // +getter +setter
private List<Manfacturer> manfacturers; // +getter
@PostConstruct
public void init() {
manfacturers = getItFromDatabaseSomehow();
}
// ...
}
in combination with
<h:selectOneMenu value="#{bean.selectedManfacturerId}">
<f:selectItems value="#{bean.manfacturers}" var="manfacturer" itemValue="#{manfacturer.manfacturerId}" />
</h:selectOneMenu>
精彩评论