I am trying to add pagination to p:datalist for a primefaces mobile application, sinces mobile version does not support pagination, but I see unwanted calls I have removed all the mobile stuff just to check but still it makes to many calls to the function SongController.getSongs until I get a indexOutOf bounds exception. Here is the code:
Index.xhtml
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:t="http://myfaces.apache.org/tomahawk">
<ui:composition template="/decorator.xhtml">
<ui:define name="title">Gato Musix</ui:define>
<ui:define name="body">
<link href="./css/styles.css" type="text/css" rel="stylesheet"/>
<div style="margin-left:auto;margin-right:auto;width:100%">
<h:form id="results">
<p:dataList value="#{songController.songs}" var="song" id="songs" type="none">
<h:panelGrid columns="3" >
<p:graphicImage value="../#{song.cover}" height="50px" width="50px"/>
<h:outputText value="#{song.name}, #{song.album}" />
</h:panelGrid>
</p:dataList>
</h:form>
</div>
</ui:define>
</ui:composition>
</html>
controller:
Code: Select all
package es.tresw.gatomusic.controller;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.push.PushContext;
import org.primefaces.push.PushContextFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import es.tresw.gatomusic.datamodel.SongDataModel;
import es.tresw.gatomusic.entities.Song;
import es.tresw.gatomusic.service.SongService;
@Component("songController")
@Scope("view")
public class SongController
{
@Autowired
@Qualifier("songService")
public SongService songService;
private Song selectedSong;
private Song song;
private SongDataModel songDataModel;
private String searchTerm;
private List<Song> songs;
private Long minId=new Long(0);
private Long maxId;
/* @PostConstruct
public void myPostConstruct()
{
String renderKitId = FacesContext.getCurrentInstance().getViewRoot().getRenderKitId();
String uri = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRequestURL().toString();
if(renderKitId.equalsIgnoreCase("PRIMEFACES_MOBILE") && uri.indexOf("mobile")==-1)
{
try {
FacesContext.getCurrentInstance().getExternalContext().redirect("./mobile/index.xhtml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*/
public List<Song> searchSongs()
{
return getSongs();
}
public void setSongs(List<Song> songs) {
this.songs = songs;
}
/* public List<Song> getSongs()
{
if(searchTerm==null || searchTerm.equals(""))
{
return songService.getAllSongs();
}
else
{
if(searchTerm.equals("emptyVal"))
{
searchTerm="";
return songService.getAllSongs();
}
else
{
return songService.search(searchTerm);
}
}
}
*/
public List<Song> getSongs()
{
if(searchTerm==null || searchTerm.equals(""))
{
songs=getNextSongsPaginated();
}
else
{
if(searchTerm.equals("emptyVal"))
{
searchTerm="";
songs= getNextSongsPaginated();
}
else
{
if(minId!=null)
songs=songService.search(searchTerm, true, false, 10, minId);
else if(maxId!=null)
songs=songService.search(searchTerm, true, true, 10, minId);
}
}
return songs;
}
public List<Song> getNextSongs()
{
return songService.getNextSongs();
}
public Song getActiveSong()
{
Song song = songService.getActiveSong();
if(song==null)
{
song=new Song();
}
return song;
}
public void vote()
{
songService.addVore(selectedSong.getId());
PushContext pushContext = PushContextFactory.getDefault().getPushContext();
pushContext.push("/active", "songs");
}
public SongService getSongService()
{
return songService;
}
public void update()
{
songService.update(selectedSong);
}
public void delete()
{
songService.delete(selectedSong);
}
public void save()
{
song.setDate(new Date());
songService.update(song);
}
public void setSongService(SongService songService) {
this.songService = songService;
}
public Song getSelectedSong() {
if(selectedSong==null)
selectedSong=new Song();
return selectedSong;
}
public void setSelectedSong(Song selectedSong) {
this.selectedSong = selectedSong;
}
/*public void setSongs(List<Song> songs) {
this.songs = songs;
}*/
public SongDataModel getSongDataModel() {
songDataModel = new SongDataModel(songService.getAllSongs());
return songDataModel;
}
public void setSongDataModel(SongDataModel songDataModel) {
this.songDataModel = songDataModel;
}
public Song getSong() {
if(song==null)
song = new Song();
return song;
}
public void setSong(Song song) {
this.song = song;
}
public boolean getRenderActive() {
Song song = songService.getActiveSong();
if(song==null)
return false;
else
return true;
}
public String getSearchTerm() {
return searchTerm;
}
public void setSearchTerm(String searchTerm) {
this.searchTerm = searchTerm;
}
private List<Song> getNextSongsPaginated()
{
if(minId==null)
minId=new Long(0);
maxId=minId;
List<Song> songsPaginated=songService.getNextSongsPaginated(minId, 10);
minId=songsPaginated.get(songsPaginated.size()-1).getId();
return songsPaginated;
}
private List<Song> getPreviousSongsPaginated()
{
if(maxId==null && minId!=0)
maxId=minId;
minId=maxId;
List<Song> songsPaginated=songService.getPreviousSongsPaginated(minId, 10);
maxId=songsPaginated.get(0).getId();
return songsPaginated;
}
public Long getMaxIdDB()
{
return songService.getMaxId();
}
public Long getMinIdDB()
{
return songService.getMinId();
}
public boolean getEnablePrevious()
{
if(maxId==null)
{
return false;
}
else
{
return maxId>getMinIdDB();
}
}
public boolean getEnableNext()
{
if(minId==null)
{
return (songService.getSongCount()>10);
}
else
{
return minId<getMaxIdDB();
}
}
public void setMinId(Long id)
{
minId=id;
}
public Long getMinId()
{
return minId;
}
public void setMaxId(Long id)
{
maxId=id;
}
public Long getMaxId()
{
return maxId;
}
}