[英]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.