我正在使用datatable从数据库中检索记录。 Datatable按预期显示行上的所有项目。我想要的是如何只选择其中一个或单个,可以通过在datatable中指定项目的索引吗?还是通过SQL查询?
bean:
public JcalendarController getSelectedUser() {
return selectedday;
}
public void setSelectedUser(JcalendarController selectedday) {
this.selectedday = selectedday;
}
List<String> user_spinner_list = new ArrayList<String>();
List<JcalendarController> calendarlist = new ArrayList<JcalendarController>();
public void delete() {
System.out.println("JadminBeans >> delete() ---------- id= ");
JcalendarDAO.deleteDay(selectedday);
}
public List<JcalendarController> getMessages() {
System.out.println("List<JcalendarController> getMessages()");
calendarlist = JcalendarDAO.getdays();
return calendarlist;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
//--------------------------------------------------------------------------
//---------------------------- user_spinner() ----------------------------//
public List<String> user_spinner() {
System.out.println("List<JcalendarBeans> user_spinner()");
user_spinner_list = JcalendarDAO.AllUsarname_spinner();
return user_spinner_list;
}
//---------------------------- ImageUpload() ----------------------------//
public void ImageUpload() {
JcalendarController CC = new JcalendarController(this.username, this.day, this.file);
System.out.println(this.username + " " + this.day + " " + this.file);
calendarlist.add(CC);
JcalendarDAO.add_image_DAO(CC);
}
//---------------------------- TextUpload() ----------------------------//
public void TextUpload() {
JcalendarController CC = new JcalendarController(this.username, this.day, this.text);
System.out.println(this.username + " " + this.day + " " + this.text);
calendarlist.add(CC);
JcalendarDAO.add_text_DAO(CC);
}
DAO:
public static List<JcalendarController> getdays() {
List<JcalendarController> ccs = new ArrayList<JcalendarController>();
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from calendar");
while (rs.next()) {
JcalendarController cc = new JcalendarController();
cc.setId(rs.getLong("id"));
cc.setDay(rs.getInt("day"));
cc.setText(rs.getString("text"));
cc.setUsername(rs.getString("username"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return ccs;
}
XHTML
<p:dataTable value="#{Jcalendar.messages}" var="o" paginator="true" selection="#{Jcalendar.selectedUser}"
rowKey="#{o.id}" style="margin-bottom:20px"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="24,48,144">
<f:facet name="header">
<h:outputText value="Data showing from database" />
</f:facet>
<p:column selectionMode="single" />
<p:column>
<f:facet name="header">
<h:outputText value=" Id" />
</f:facet>
<h:outputText value="#{o.id}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Day" />
</f:facet>
<h:outputText value="#{o.day}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Text" />
</f:facet>
<h:outputText value="#{o.text}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Username" />
</f:facet>
<h:outputText value="#{o.username}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Image" />
</f:facet>
<h:outputLink value="DisplayImage?id=#{o.id}" target="_blank">
<h:graphicImage value="DisplayImage?id=#{o.id}" width="50" height="50"></h:graphicImage>
</h:outputLink>
</p:column>
<f:facet name="footer">
<p:commandButton value="Delete" action="#{Jcalendar.delete()}" ajax="false" update=":form:msgs"/>
</f:facet>
</p:dataTable>
答案 0 :(得分:2)
数据表将显示消息属性的值,因此 getMessages()返回的列表中的任何内容都将在表内呈现。
对于您提出的问题,最好制作一个DAO方法并从数据库返回所需的记录,用你的话说,使用SQL。
一个优化提示,您应该避免在 getter 方法中使用任何逻辑,就像在单个请求的生命周期中一样,可以多次调用它。在您的情况下,它会在每次通话时联系DB。您可以查看此post了解详情,并找到更好的初始化列表的方法。