-1

我遇到了一个问题:当我向具有字段名称别名的数据库发出 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="";
}
4

3 回答 3

0

如果 in 的computers属性advancedSearchComp是 type 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>
于 2013-07-09T09:13:32.527 回答
0

没有办法通过别名获得它。您必须将其分配给 bean 中某处的实际对象。如果您尚未创建定制的对象来存储来自 ResultSet 的结果,请执行此操作,将其传递到您的页面并显示。

它还有助于将您的姓名包裹在 outputText 标记周围,例如

<h:outputText value="#{computer.computersName}" />
于 2013-07-09T08:59:16.530 回答
0

你需要创建一个类Computer

public class Computer{
    private String computerName;
    // getter-setter
}

advancedSearchComp托管豆

@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 );
    }
}
}
于 2013-07-09T08:52:24.137 回答