我遇到了一个问题:当我向具有字段名称别名的数据库发出 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;
}
// 返回空值;}
公共类 ComputerWrapper { 私有字符串 computer_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="";
}