Réactualisation d'un Datatable
Bonjour,
Je rencontre un soucis avec la réactualisation de mes Datatable.
En effet, il semblerait que le cache de l'application se perde entre les données intiales du Datatable et les données actuelles.
J'ai donc tenté de changer de "Scope" (passer de SessionScoped à ViewScoped, à RequestScoped), mais aucun ne résout ce problème.
RequestScoped provoque d'ailleurs d'autres problèmes :
Code:
1 2 3 4
|
17 janv. 2013 11:54:50 com.sun.faces.lifecycle.ApplyRequestValuesPhase execute
ATTENTION: / by zero
java.lang.ArithmeticException: / by zero |
Concrètement, est-il possible d'éviter ce type de problème ?
Voici les codes en relation avec ce problème :
page xhtml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
<?xml version='1.0' encoding='UTF-8' ?>
<!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:c="http://java.sun.com/jsp/jstl/core">
<f:event type="javax.faces.event.PreRenderViewEvent" listener="#{sessionManagedBean.preRenderView}"/>
<f:view contentType="text/html">
<h:head>
<f:facet name="first">
<meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
<link rel="stylesheet" href="css/custom.css" type="text/css" />
<title>Administration - Sessions</title>
</f:facet>
</h:head>
<h:body>
<h:form id="form">
<p:layout fullPage="true">
<p:layoutUnit position="center" styleClass="contenu" style="background-color: #FFFFFF">
<p:panel style="bgcolor:'#FFFFFF'">
<h:panelGrid columns="2" cellpadding="2">
<h:graphicImage id="logo" alt="logo" url="/images/logo.png" style="width: 20%;float:left;">
</h:graphicImage>
<h:outputText value="Administration des sessions" style="width: 80%;" />
</h:panelGrid>
</p:panel>
<!-- Growl permet d'afficher le message d'erreur ou d'information si besoin -->
<p:growl id="growl" showDetail="true" autoUpdate="true" redisplay="false" life="4000"/>
<p:tabMenu activeIndex="7">
<p:menuitem value="Accueil" icon="/images/dock/home.png" url="/faces/index.xhtml"/>
<p:menuitem value="Compte" icon="/images/dock/email.png" url="/faces/account.xhtml"/>
<p:menuitem value="Comptes E-mail" icon="/images/dock/history.png" url="/faces/mailbox.xhtml"/>
<p:menuitem value="Modèles" icon="/images/dock/history.png" url="/faces/mailbox_model.xhtml"/>
<p:menuitem value="Liste verte" icon="/images/dock/history.png" url="/faces/green_list.xhtml"/>
<p:menuitem value="Devices" icon="/images/dock/history.png" url="/faces/device.xhtml"/>
<p:menuitem value="Customers" icon="/images/dock/history.png" url="/faces/customer.xhtml"/>
<p:menuitem value="Sessions" icon="/images/dock/history.png" url="/faces/session.xhtml"/>
<p:menuitem value="Statistiques" icon="/images/dock/history.png" url="/faces/statistics.xhtml"/>
<p:menuitem value="Configuration" icon="/images/dock/terminal.png" url="/faces/conf.xhtml">
<p:menuButton value="Configuration">
<p:menuitem value="Noms des répertoires IMAP" ajax="false" url="/faces/imap.xhtml"/>
<p:menuitem value="Paramètres généraux" ajax="false"/>
</p:menuButton>
</p:menuitem>
</p:tabMenu>
<p:dataTable id="sessionDataTable"
var="sessionEntity"
value="#{sessionManagedBean.sessionDatatableList}"
selectionMode="single"
editable="false"
paginator="true"
rows="5"
paginatorPosition="top"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown} Total (sans filtres) : #{sessionManagedBean.getSessionDatatableList().getCountToShow()}"
rowsPerPageTemplate="5,10,15"
lazy="true"
emptyMessage="Aucune session trouvé"
filteredValue="#{sessionManagedBean.filteredValue}"
widgetVar="sessionTable">
<f:facet name="header">
Sessions
</f:facet>
<p:column id="sessionID" filterBy="#{sessionEntity.sessionID}"
headerText="ID de la session">
<h:outputText value="#{sessionEntity.sessionID}" />
</p:column>
<p:column id="account" filterBy="#{sessionEntity.account}"
headerText="ID du compte" style="width:5%">
<h:outputText value="#{sessionEntity.account}" />
</p:column>
<p:column id="isOnline" filterBy="#{sessionEntity.isOnline}"
headerText="En ligne">
<h:outputText value="#{sessionEntity.isOnline}" />
</p:column>
<p:column id="jsonVersion" filterBy="#{sessionEntity.jsonVersion}"
headerText="Version de JSON">
<h:outputText value="#{sessionEntity.jsonVersion}" />
</p:column>
<p:column id="appVersion" filterBy="#{sessionEntity.appVersion}"
headerText="APP Version">
<h:outputText value="#{sessionEntity.appVersion}" />
</p:column>
<p:column headerText="Actions" style="width:40%">
<p:commandButton value="Editer" id="editSessionButton"
update=":form:sessionDataTable, :form:displaySession"
icon="ui-icon-pencil"
actionListener="#{sessionManagedBean.showSessionDatas(sessionEntity)}"/>
<p:commandButton value="Supprimer" id="deleteSessionButton"
update=":form:sessionDataTable"
icon="ui-icon-minus"
actionListener="#{sessionManagedBean.deleteSession(sessionEntity)}"/>
</p:column>
</p:dataTable>
<p:panel header="Session :" id="displaySession" toggleable="true">
<p:panel header="Informations sur la session">
<h:panelGrid columns="2" cellpadding="2">
<h:panelGrid columns="2" cellpadding="2">
<h:outputText value="ID de la session :"/>
<h:outputText value="#{sessionManagedBean.sessionID}" size="35"/>
<h:outputText value="ID du compte :"/>
<h:outputText value="#{sessionManagedBean.account}" size="35"/>
<h:outputText value="ID du device :"/>
<h:outputText value="#{sessionManagedBean.deviceID}" size="35"/>
</h:panelGrid>
<h:panelGrid columns="2" cellpadding="2">
<h:panelGrid columns="2" cellpadding="2">
<h:outputText value="Date de connexion :"/>
<p:calendar pattern="dd/MM/yyyy HH:mm:ss" value="#{sessionManagedBean.sessionClock}" id="popupButtonCal1" showOn="button" />
<h:outputText value="Date de déconnexion :"/>
<p:calendar pattern="dd/MM/yyyy HH:mm:ss" value="#{sessionManagedBean.logoutDate}" id="popupButtonCal2" showOn="button" />
<h:outputText value="Version de JSON :"/>
<h:outputText value="#{sessionManagedBean.jsonVersion}" size="35"/>
<h:outputText value="Version de l'APP :"/>
<h:outputText value="#{sessionManagedBean.appVersion}" size="35"/>
</h:panelGrid>
</h:panelGrid>
</h:panelGrid>
</p:panel>
<p:commandButton value="Valider"
id="validateSessionButton" update=":form:sessionDataTable"
icon="ui-icon-check"
actionListener="#{sessionManagedBean.validateSession}"/>
<p:commandButton value="Réinitialiser"
id="reinitSessionButton" update=":form:displaySession"
icon="ui-icon-cancel"
actionListener="#{sessionManagedBean.preAddSession()}"/>
</p:panel>
</p:layoutUnit>
</p:layout>
</h:form>
</h:body>
</f:view>
</html> |
Le Bean (SessionManagedBean.java) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
@ManagedBean
@RequestScoped
public class SessionManagedBean extends DefaultBean// implements Serializable
{
// private HttpSession session;
public void preRenderView()
{
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
}
/**
* Creates a new instance of MailsManagedBean
*/
public SessionManagedBean()
{
System.out.println("OK init SessionManagedBean");
sessionDatatableList = new SessionDatatableList();
}
private Integer sessionID = -1;
private Date sessionClock = new Date();
private boolean isOnline = true;
public Integer getSessionID()
{
return sessionID;
}
public void setSessionID(Integer sessionID)
{
this.sessionID = sessionID;
}
public Date getSessionClock()
{
return sessionClock;
}
public void setSessionClock(Date sessionClock)
{
this.sessionClock = sessionClock;
}
public boolean isIsOnline()
{
return isOnline;
}
public void setIsOnline(boolean isOnline)
{
this.isOnline = isOnline;
}
public Date getLogoutDate()
{
return logoutDate;
}
public void setLogoutDate(Date logoutDate)
{
this.logoutDate = logoutDate;
}
public Integer getAccount()
{
return account;
}
public void setAccount(Integer account)
{
this.account = account;
}
public String getJsonVersion()
{
return jsonVersion;
}
public void setJsonVersion(String jsonVersion)
{
this.jsonVersion = jsonVersion;
}
public String getAppVersion()
{
return appVersion;
}
public void setAppVersion(String appVersion)
{
this.appVersion = appVersion;
}
private Date logoutDate = null;
public Integer version;
private Integer account = -1;
private Integer deviceID = -1;
private String jsonVersion = "";
private String appVersion = "";
public Integer getVersion()
{
return version;
}
public void setVersion(Integer version)
{
this.version = version;
}
private SessionEntity[] selectedSessionEntity;
public SessionEntity[] getSelectedSessionEntity()
{
return selectedSessionEntity;
}
public void setSelectedSessionEntity(SessionEntity[] selectedSessionEntity)
{
this.selectedSessionEntity = selectedSessionEntity;
}
private SessionDatatableList sessionDatatableList;
public SessionDatatableList getSessionDatatableList()
{
return sessionDatatableList;
}
public void setSessionDatatableList(SessionDatatableList sessionDatatableList)
{
this.sessionDatatableList = sessionDatatableList;
}
private SessionEntity[] filteredValue;
public SessionEntity[] getFilteredValue()
{
return filteredValue;
}
public void setFilteredValue(SessionEntity[] filteredValue)
{
this.filteredValue = filteredValue;
}
/**
* when saving mail, save new values to bdd
*
* @param event
*/
public void onEditSession(RowEditEvent event)
{
System.out.println("OK on Edit SessionEntity");
SessionEntity sessionEntity = (SessionEntity) event.getObject();
SessionHelper.updateSession(sessionEntity);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Modifications enregistrées", ""));
}
public void showSessionDatas(SessionEntity sessionEntity)
{
System.out.println("OK showSessionDatas");
//Données du compte
this.sessionID = sessionEntity.getSessionID();
this.sessionClock = sessionEntity.getSessionClock();
this.isOnline = sessionEntity.isIsOnline();
this.version = sessionEntity.getVersion();
this.logoutDate = sessionEntity.getLogoutDate();
this.account = sessionEntity.getAccount();
this.deviceID = sessionEntity.getDeviceID();
this.jsonVersion = sessionEntity.getJsonVersion();
this.appVersion = sessionEntity.getAppVersion();
System.out.println("OK showSessionDatas - this.sessionID : " + this.sessionID + " isOnline : " + this.isOnline + " deviceID : " + this.deviceID + " this.jsonVersion : " + this.jsonVersion);
// FacesContext.getCurrentInstance().r
}
public void deleteSession(SessionEntity sessionEntity)
{
SessionHelper.deleteSession(sessionEntity);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Suppression effectuée", "Session supprimé avec succès"));
// FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Suppression effectuée", "Suppression pas encore implémentée"));
}
public void validateSession()
{
System.out.println("OK validateSession - this.sessionID : " + this.sessionID + " isOnline : " + this.isOnline + " deviceID : " + this.deviceID + " this.jsonVersion : " + this.jsonVersion);
SessionEntity sessionEntity = null;
boolean add = false;
if (sessionID == -1)
{
sessionEntity = new SessionEntity();
add = true;
} else
{
sessionEntity = SessionHelper.getSessionByID(sessionID);
}
if (sessionEntity == null)
{
Logger.getLogger(SessionManagedBean.class.getName()).log(Level.SEVERE, null, "No session for ID : " + this.sessionID);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Impossible d'effectuer la modification", "Aucune session correspondant à cet ID n'a été trouvé"));
}
System.out.println("OK validateSession - this.sessionID : " + this.sessionID + " isOnline : " + this.isOnline + " deviceID : " + this.deviceID + " this.jsonVersion : " + this.jsonVersion);
sessionEntity.setSessionID(this.sessionID);
sessionEntity.setDeviceID(this.deviceID);
sessionEntity.setSessionClock(this.sessionClock);
sessionEntity.setIsOnline(this.isOnline);
sessionEntity.setVersion(this.version);
sessionEntity.setLogoutDate(this.logoutDate);
sessionEntity.setAccount(this.account);
sessionEntity.setDeviceID(this.deviceID);
sessionEntity.setJsonVersion(this.jsonVersion);
sessionEntity.setAppVersion(this.appVersion);
if (this.version == null)
{
sessionEntity.setVersion(0);
} else
{
sessionEntity.setVersion(this.version + 1);
}
if (add)
{
SessionHelper.addSession(sessionEntity);
} else
{
SessionHelper.updateSession(sessionEntity);
}
preAddSession();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Modification de session", "Modification effectuée avec succès"));
}
public void preAddSession()
{
this.sessionID = -1;
this.sessionClock = null;
this.isOnline = false;
this.logoutDate = null;
this.account = -1;
this.deviceID = -1;
this.jsonVersion = "";
this.appVersion = "";
this.version = 0;
}
public Integer getDeviceID()
{
return deviceID;
}
public void setDeviceID(Integer deviceID)
{
this.deviceID = deviceID;
}
} |
SessionDatatableList.java :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
public class SessionDatatableList extends LazyDataModel<SessionEntity>
{
private List<SessionEntity> sessionList;
private Integer count;
/**
* Creates a new instance of DomainsDatatableList
*/
public SessionDatatableList()
{
System.out.println("OK init SessionDatatableList");
sessionList = new ArrayList<SessionEntity>();
}
@Override
public List<SessionEntity> load(int startPos, int maxResults, String sortField, SortOrder sortOrder, Map<String, String> filters)
{
System.out.println("OK PASSE LOAD");
Session s = HibernateUtil.getSession();
Transaction t = s.getTransaction();
t.begin();
String query = "select session from SessionEntity session";
List<SessionEntity> data = null;
sessionList = new ArrayList<SessionEntity>();
Query q = null;
try
{
q = s.createQuery(query);
sessionList = q.list();
System.out.println("count 2 : " + count);
count = sessionList.size();
} catch (Exception e)
{
}
System.out.println("OK PASSE LOAD -00 " + query);
if (filters != null && filters.size() > 0)
{
data = new ArrayList<SessionEntity>();
System.out.println("OK PASSE LOAD -1 " + sessionList.size());
//filter
for (SessionEntity session : sessionList)
{
System.out.println("OK PASSE LOAD 00");
boolean match = true;
Field[] fields = SessionEntity.class.getFields();
for (int i = 0; i < fields.length; i++)
{
System.out.println("Field : " + fields[i]);
}
for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();)
{
System.out.println("OK PASSE LOAD 01");
try
{
String filterProperty = it.next();
String filterValue = filters.get(filterProperty);
String fieldValue = String.valueOf(SessionEntity.class.getField(filterProperty).get(session));
System.out.println("OK PASSE LOAD 01 - filterProperty : " + filterProperty + " - filterValue : " + filterValue + " - fieldValue : " + fieldValue);
if (filterValue == null || Pattern.compile(Pattern.quote(filterValue), Pattern.CASE_INSENSITIVE).matcher(fieldValue).find())
{
match = true;
} else
{
match = false;
break;
}
// query = query.concat(" where "+filterProperty+" LIKE '%"+filterValue+"%'");
} catch (Exception e)
{
Logger.getLogger(SessionEntity.class.getName()).log(Level.SEVERE, null, e);
// System.out.println("Exception : "+e.printStackTrace());
match = false;
}
}
if (match)
{
data.add(session);
}
}
return data;
}
System.out.println("OK PASSE LOAD -01 " + query);
if (sortField != null)
{
query = query.concat(" order by " + sortField);
if (sortOrder == SortOrder.DESCENDING)
{
query = query.concat(" desc");
}
}
System.out.println("OK PASSE LOAD -02 " + query + " startPos : " + startPos + " max : " + maxResults);
System.out.println("OK PASSE LOAD 1 " + filters.toString());
q = null;
try
{
q = s.createQuery(query);
q.setFirstResult(startPos);
q.setMaxResults(maxResults);
sessionList = q.list();
} catch (Exception e)
{
Logger.getLogger(SessionEntity.class.getName()).log(Level.SEVERE, null, e);
}
t.commit();
if (sortField != null)
{
Collections.sort(sessionList, new LazySorter(sortField, sortOrder, Device.class));
}
if (getRowCount() <= 0)
{
q = s.createQuery("select count(session) from SessionEntity session");
Number num = (Number) q.uniqueResult();
setRowCount(num.intValue());
}
setPageSize(maxResults);
System.out.println("OK PASSE LOAD fin " + sessionList.size());
return sessionList;
}
@Override
public Object getRowKey(SessionEntity session)
{
return session.getSessionID();
}
@Override
public SessionEntity getRowData(String sessionEntityID)
{
Integer id = Integer.valueOf(sessionEntityID);
for (SessionEntity sessionEntity : sessionList)
{
if (id.equals(sessionEntity.getSessionID()))
{
return sessionEntity;
}
}
return null;
}
public Integer getCountToShow()
{
return count;
}
/**
* Initialize sessions parameters from local files
*/
@PostConstruct
public void initSessionList()
{
}
} |
Merci d'avance.
Cordialement.