When this page loads it takes a viewparam and uses a converter to get an object.
Im getting the following error when the paginator is used because the viewParam is not being passed to the converter
at java.lang.Long.parseLong(Unknown Source) at com.civi.HelloWorld.controller.UserConverter.getAsObject(UserConverter.java:36)
Every time the page refreshes it needs a param passed to it.
So when using the pagintor in the datatable i need to send the f:param inorder for the viewParam to use the converter to load the object
<f:metadata>
<f:viewParam
name="idUser"
value="#{userBean.tipTourUser}"
converter="#{userConverter}"
required="true"
requiredMessage="Bad request. Please use a link from within the system." />
</f:metadata>
<h:form id="abc" styleClass="simpleformstyle">
<p:dataTable id="transactionsTable"
var="transaction"
resizableColumns="true"
value="#{userBean.tipTourUser.tips}"
rendered="#{not empty userBean.tipTourUser.tips}"
rows="2"
paginator="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="1,2,3"
paginatorPosition="bottom" >
//SOMETHING LIKE THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<f:param name="idUser" value="#{userBean.tipTourUser.idUser}" />
//SOMETHING LIKE THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<p:column>
<f:facet name="header">
<h:outputText style="float: left;" value="Date received" />
</f:facet>
<h:outputText value="#{transaction.received}" >
<f:convertDateTime pattern="d-M-yyyy" />
</h:outputText>
</p:column>
</p:dataTable>
</h:form>
UserBean
@ViewScoped
@Named
public class UserBean implements Serializable {
private static final long serialVersionUID = 1L;
@EJB
private UserService userService;
private User tipTourUser;
private boolean editmode;
//sets the edit mode and stores the original values
public void edit() {
editmode = true;
}
//resets the original values and leaves the edit mode
public void cancel() {
editmode = false;
}
//saves updated values and leavers the edit mode
public void save(User user) {
tipTourUser = userService.updateUser(user);
editmode = false;
}
public boolean isEditmode() {
return editmode;
}
public User getTipTourUser() {
return tipTourUser;
}
public void setTipTourUser(User tipTourUser) {
this.tipTourUser = tipTourUser;
}
}
Converter
@Named
@RequestScoped
public class UserConverter implements Converter
{
@EJB
private UserService userService;
public UserConverter() {
}
//get User object from idUser, key = idUser
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String key) throws ConverterException {
return userService.getUser(Long.parseLong(key));
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException {
return arg2.toString();
}
}
You need a h:form
around your datatable, then it is an AJAX component and not reloading the page on pagination.
For basic GET page linkgs you must pass the view param to every redirect. Else it is lost. Something like this:
<h:link>
<o:param name="idUser" value="#{userBean.tipTourUser}" converter="#{userConverter}" />
<f:param name="page" value="#{userBean.page}" />
</h:link>
Hint:
Use omnifaces o:param, since it provides a converter attribute to keep your conversion logic DRY.