我遇到了一个问题:当我向带有字段名别名的数据库发出SQL请求时,我无法用h:dataTable(我使用JSF)显示它们。
这里的例子。请求:
SELECT
computers.name AS computers_name,
FROM
computers
WHERE
computers.id IS NOT NULL AND (computers.group_id=1);
和XHTML代码:
<h:dataTable value="#{advancedSearchComp.computers}"
var="computer" rowClasses="oddTableRow, evenTableRow"
headerClass="tableHeader" styleClass="table">
<h:column>
<f:facet name="header">name</f:facet>
#{computer.computers_name}
</h:column>
</h:dataTable>
并且存在一个问题,因为h:column不能通过别名获取字段值,只能通过实名获取,并且它返回null。
最佳解决方案是什么?
我会感激任何提示。
更新。我所知道的所有手册只考虑了一些简单的案例“SELECT * FROM table”。
是否有一种在没有棘手方法的情况下在JSF中显示ResultSet的常用方法?
更新。我做到了:
<h:dataTable value="#{advancedSearchComp.selectedComputers}" var="computer" rowClasses="oddTableRow, evenTableRow"
headerClass="tableHeader" styleClass="table">
<h:column>
<f:facet name="header">id</f:facet>
#{computer.computers_id}
</h:column>
<h:column>
<f:facet name="name">Name</f:facet>
#{computer.computers_name}
</h:column>
<h:column>
<f:facet name="header">Location</f:facet>
#{computer.locations_name}
</h:column>
<h:column>
<f:facet name="header">Rack</f:facet>
#{computer.racks_name}
</h:column>
<h:column>
<f:facet name="header">Rack unit</f:facet>
#{computer.computers_rack_unit}
</h:column>
<h:column>
<f:facet name="header">Serial</f:facet>
#{computer.computers_serial}
</h:column>
<h:column>
<f:facet name="header">Inv number</f:facet>
#{computer.computers_inv_number}
</h:column>
<h:column>
<f:facet name="header">Status</f:facet>
#{computer.computers_status_name}
</h:column>
</h:dataTable>
public List<ComputerWrapper> getSelectedComputers() throws SQLException{
ComputerWrapper computer;
selectedComputers.clear();
if (query==null) {
return null;
}
else {
Statement sth=dbhIrmdb.createStatement();
ResultSet rowSet=sth.executeQuery(query);
while (rowSet.next()) {
computer=new ComputerWrapper();
computer.setComputers_id(rowSet.getString("computers_id"));
computer.setComputers_name(rowSet.getString("computers_name"));
computer.setLocations_name(rowSet.getString("locations_name"));
computer.setRacks_name(rowSet.getString("racks_name"));
computer.setComputers_rack_unit(rowSet.getString("computers_rack_unit"));
computer.setComputers_serial(rowSet.getString("computers_serial"));
computer.setComputers_inv_number(rowSet.getString("computers_inv_number"));
computer.setComputers_status_name(rowSet.getString("computers_status_name"));
selectedComputers.add(computer);
}
sth.close();
return selectedComputers;
}
//返回null; }
公共类ComputerWrapper { private String computers_id,computers_name,locations_name,racks_name,computers_rack_unit, computers_serial,computers_inv_number,computers_status_name;
public String getComputers_id() {
return computers_id;
}
public void setComputers_id(String computers_id) {
this.computers_id=computers_id;
}
public String getComputers_name() {
return computers_name;
}
public void setComputers_name(String computers_name) {
this.computers_name=computers_name;
}
public String getLocations_name() {
return locations_name;
}
public void setLocations_name(String locations_name) {
this.locations_name=locations_name;
}
public String getRacks_name() {
return racks_name;
}
public void setRacks_name(String racks_name) {
this.racks_name=racks_name;
}
public String getComputers_rack_unit() {
return computers_rack_unit;
}
public void setComputers_rack_unit(String computers_rack_unit) {
this.computers_rack_unit=computers_rack_unit;
}
public String getComputers_serial() {
return computers_serial;
}
public void setComputers_serial(String computers_serial) {
this.computers_serial=computers_serial;
}
public String getComputers_inv_number() {
return computers_inv_number;
}
public void setComputers_inv_number(String computers_inv_number) {
this.computers_inv_number=computers_inv_number;
}
public String getComputers_status_name() {
return computers_status_name;
}
public void setComputers_status_name(String computers_status_name) {
this.computers_status_name=computers_status_name;
}
public ComputerWrapper() {
computers_id=""; computers_name=""; locations_name=""; racks_name=""; computers_rack_unit="";
computers_serial=""; computers_inv_number=""; computers_status_name="";
}
答案 0 :(得分:0)
您需要创建一个班级Computer
public class Computer{
private String computerName;
// getter-setter
}
advancedSearchComp
托管bean
@ManagedBean (name = "advancedSearchComp")
public class AdvancedSearchComp{
private List<Computer> computers;
// getter-setter
// code to populate computers and map result set to the Computer bean
}
要将结果集映射到java bean,您需要迭代结果集并准备一个列表。
然后在XHTML中
<h:dataTable value="#{advancedSearchComp.computers}"
var="computer" rowClasses="oddTableRow, evenTableRow"
headerClass="tableHeader" styleClass="table">
<h:column>
<f:facet name="header">name</f:facet>
<h:outputText value = "#{computer.computerName}"/>
</h:column>
</h:dataTable>
<强>更新强> 如果要显示16个或大量字段,则有以下两个选项:
Option1 添加bean中的所有字段,然后按上面显示它们
Option2 在bean中添加地图,然后在XHTML中使用此地图。
public class Computer{
private Map<String, Object> dataMap;
// getter-setter
}
在XHTML上
<h:dataTable value="#{advancedSearchComp.computers}"
var="computer" rowClasses="oddTableRow, evenTableRow"
headerClass="tableHeader" styleClass="table">
<h:column>
<f:facet name="header">name</f:facet>
<h:outputText value = "#{computer.dataMap['computerName']}"/>
</h:column>
<h:column>
<f:facet name="header">Brand</f:facet>
<h:outputText value = "#{computer.dataMap['computerBrand']}"/>
</h:column>
</h:dataTable>
迭代结果集以填充dataMap
ResultSet rs= statement.executeQuery();
if(rs!= null){
dataMap = new HashMap<String, Object>();
ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next()){
for(int _iterator = 0; _iterator < rsmd.getColumnCount(); _iterator++){
String columnName = rsmd.getColumnName(_iterator + 1);
Object columnValue = rs.getObject(_iterator + 1);
dataMap.put(columnName , columnValue );
}
}
}
答案 1 :(得分:0)
无法通过别名获取它。你必须将它分配给bean中某个地方的实际对象。如果尚未创建专门用于存储ResultSet结果的对象,请执行此操作,将其传递到页面并显示。
将名称包装在outputText标记周围也很有帮助,比如
<h:outputText value="#{computer.computersName}" />
答案 2 :(得分:0)
如果computers
中的advancedSearchComp
属性属于ResultSet
类型,那么您应该能够编写如下内容:
<h:dataTable value="#{advancedSearchComp.computers}" var="computer">
<h:column>
<f:facet name="header">name</f:facet>
<h:outputText value="#{computer.getString('computers_name')}" />
</h:column>
</h:dataTable>