Unwanted calls to backend

UI Components for JSF
Post Reply
bradox
Posts: 19
Joined: 28 Nov 2012, 01:13

03 Apr 2013, 00:39

Hello,
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;
	}
	
}
From the controller I commented everything I thought that could be causing the problem. The thing is that when the page is loaded there are two calls to getSongs until I get the index out of bounds exception because I do not check if the list is empty before doing: minId=songsPaginated.get(songsPaginated.size()-1).getId(); The thing is why is it calling three times getSongs when the page loads?
primefaces 3.5, spring 3.1.0, hibernate 4.0.1, jsf 2.1
tomcat 7, mysql

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

14 Apr 2013, 15:14

First and very important piece of advice. Search instead of ask. You will find and 'get' an answer much much quicker than asking a question! Speaking from experience. :)

Moving on... I searched google for the following:

balusc getter methods

stackoverflow balusc getter methods (this is usually how i search for JSF questions)

and the following was in the list of search results. Read it.

Why JSF calls getters multiple times
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

14 Apr 2013, 15:18

If you need some help with your implementation, click URL below,

viewtopic.php?f=8&t=28068&start=10#p94624

and mention the following:

feature request: PF Mobile datalist with pagination
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 95 guests