It works perfectly outside the dataList
Code: Select all
<div id="searchBar"><h:inputText
value="#{searchBean.searchText}" style="width:220px"></h:inputText><p:commandButton
id="searchBtn" value="Search" update="panel,display"
actionListener="#{searchBean.searchFriends}"></p:commandButton></div>
<div id="keywords"><h:outputText value="#{searchBean.keywords}"></h:outputText></div>
<div class="datalist-noborder"><p:panel id="display">
<div id="keywords"><h:outputText value="#{searchBean.keywords}"></h:outputText></div>
<p:dataList value="#{searchBean.searchResult}" var="searchResult"
itemType="none">
<table style="margin-top: 30px">
<tbody>
<tr>
<td><p:column>
<p:commandLink action="#{searchBean.getFriend}">
<h:graphicImage value="#{searchResult.resultPhotoPath}"
style="width: 100px;"></h:graphicImage>
<f:setPropertyActionListener value="#{searchResult}"
target="#{searchBean.selectedUser}" />
</p:commandLink>
</p:column></td>
<td style="width: 400px; text-align: left;"><h:outputText
value="#{searchResult.resultFullName}" style="font-weight: bold" /></td>
</tr>
</tbody>
</table>
</p:dataList>
</p:panel></div>
Code: Select all
package com.tomo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
public class SearchBean {
public String keywords;
public String searchText;
public String searchSql;
public String fullName;
public SearchResult selectedUser;
public List<SearchResult> searchResult;
public int rows;
public String getFriend() {
return "Stranger";
}
public void NumberOfRows() throws Exception {
PreparedStatement getRows = null;
searchSql = "%" + searchText + "%";
Class.forName("oracle.jdbc.driver.OracleDriver");
HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(true);
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "tomo", "tomo123");
// @machineName:port:SID, userid, password
String sql = "Select Count(USERID) AS" + "ROWS"
+ " FROM USERINFO Where FIRSTNAME LIKE ? AND USERID <> ?";
getRows = conn.prepareStatement(sql);
getRows.setString(1, searchSql.toLowerCase());
getRows.setString(2, (String) session.getAttribute("userId"));
try {
ResultSet rs = getRows.executeQuery();
if (rs.next()) {
this.rows = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void searchFriends() throws Exception {
PreparedStatement getFriends = null;
searchResult = new ArrayList<SearchResult>();
this.NumberOfRows();
this.keywords = "Results for keywords: " + searchText;
searchSql = "%" + searchText + "%";
Class.forName("oracle.jdbc.driver.OracleDriver");
HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(true);
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "tomo", "tomo123");
// @machineName:port:SID, userid, password
String sql = "SELECT USERINFO.USERID, USERINFO.FIRSTNAME, USERINFO.LASTNAME, USERINFO.GENDER, PHOTO.PHOTOPATH FROM USERINFO INNER JOIN PHOTO ON USERINFO.PHOTOID = PHOTO.PHOTOID WHERE USERINFO.FIRSTNAME LIKE ? AND USERINFO.USERID <> ? ORDER BY USERINFO.FIRSTNAME";
getFriends = conn.prepareStatement(sql);
getFriends.setString(1, searchSql.toLowerCase());
getFriends.setString(2, (String) session.getAttribute("userId"));
try {
ResultSet rs = getFriends.executeQuery();
for (int i = 0; i < rows; i++)
while (rs.next()) {
fullName = rs.getString("firstName") + " "
+ rs.getString("lastname");
this.searchResult.add(new SearchResult(fullName, rs
.getString("photoPath")));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public SearchResult getSelectedUser() {
return selectedUser;
}
public void setSelectedUser(SearchResult selectedUser) {
this.selectedUser = selectedUser;
}
public String getSearchText() {
return searchText;
}
public void setSearchText(String searchText) {
this.searchText = searchText;
}
public List<SearchResult> getSearchResult() {
return searchResult;
}
public void setSearchResult(List<SearchResult> searchResult) {
this.searchResult = searchResult;
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
}
Primefaces 2.2 RC1