[英]How to display data table in JSF page with database data
我在數據庫中有一個表,用於存儲應用程序配置數據。
這是表結構-這是非常簡單的示例:
SessionTTL MaxActiveUsers
---------------------- ----------------------
30 787
我想以這種方式顯示表數據:
<table border="1">
<tr>
<td>SessionTTL</td>
<td>30</td>
</tr>
<tr>
<td>MaxActiveUsers</td>
<td>787</td>
</tr>
<tr>
<td>option</td>
<td>value</td>
</tr>
<tr>
<td>option</td>
<td>value</td>
</tr>
</table>
我嘗試使用此JSF代碼和此Java代碼顯示數據,但結果不是我想要的:
<h:dataTable id="books"
columnClasses="list-column-center,
list-column-right, list-column-center,
list-column-right" headerClass="list-header"
rowClasses="list-row" styleClass="list-
background" value="#{DashboardController.getDashboardList()}" var="store">
<h:column>
<h:outputText value="Session Timeout"/>
<h:outputText value="Maximum Logged Users"/>
</h:column>
<h:column>
<h:outputText value="#{store.sessionTTL} minutes"/>
<h:outputText value="#{store.maxActiveUsers}"/>
</h:column>
</h:dataTable>
public List<Dashboard> getDashboardList()throws SQLException{
List<Dashboard> list = new ArrayList<Dashboard>();
if(ds == null) {
throw new SQLException("Can't get data source");
}
Connection conn = ds.getConnection();
if(conn == null) {
throw new SQLException("Can't get database connection");
}
PreparedStatement ps = conn.prepareStatement("SELECT * from GLOBALSETTINGS");
try{
//get data from database
ResultSet result = ps.executeQuery();
while (result.next()){
Dashboard cust = new Dashboard();
cust.setSessionTTL(result.getString("SessionTTL"));
cust.setMaxActiveUsers(result.getString("MaxActiveUsers"));
list.add(cust);
}
}
catch(Exception e1){
// Log the exception.
}
finally{
try{
ps.close();
conn.close();
}
catch(Exception e2){
// Log the exception.
}
}
return list;
}
如何以所需方式顯示數據?
最好的祝願
您不能為get方法分配括號。 您必須使用托管bean中的List屬性。
value="#{DashboardController.getDashboardList()}" //WRONG!
您的托管bean應該如下所示:
public class DashboardController {
private List<Dashboard> lstDashboard;
public DashboardController() {
try {
lstDashboard = getDashboardList();
} catch (Exception e) {
//log the exception or something else...
}
}
//getter and setter...
public List<Dashboard> getLstDashboard() {
return this.lstDashboard;
}
public void setLstDashboard(List<Dashboard> lstDashboard) {
this.lstDashboard = lstDashboard;
}
//your other methods here...
}
其次,設置表中每一列的設計,而不是行的設計。 您將為1列設置2個值,為另一列設置實際輸出。
修復您的數據表代碼:
<h:dataTable id="books"
columnClasses="list-column-center,
list-column-right, list-column-center,
list-column-right" headerClass="list-header"
rowClasses="list-row"
styleClass="list-background"
value="#{DashboardController.lstDashboard}"
var="store">
<h:column>
<f:facet name="header">
<h:outputText value="Session Timeout" />
</f:facet>
<h:outputText value="#{store.sessionTTL} minutes"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="MaxActiveUsers" />
</f:facet>
<h:outputText value="#{store.maxActiveUsers}"/>
</h:column>
</h:dataTable>
@BalusC是StackOverflow JSF專家。 在他的博客文章中,他有一個很好的有關使用JSF DataTable的示例。
除了我在前面的問題中已經提到的一些設計缺陷外,至少您還必須為dataTable使用正確的value
屬性。
更換:
value="#{DashboardController.getDashboardList()}"
有:
value="#{DashboardController.dashboardList}"
“ get”前綴將自動添加。 括號可以省略。
getDataList()您可以編寫一些常規的getter方法。 您編寫一些方法dataList()並在該方法中實現您的業務代碼。
jsf的dataTable中的xhtml或jsp文件中的方法聲明。
<h:dataTable id="books" type="submit" value="#{DashboardController.dataList}" var="dashbord">
<h: column name="ID">
<f:facet name="header">
<h:outputText value="#{dashbord.id}"/>
</f:facet>
</h:column>
...your another columns...
</h:dataTable>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.