当此页面加载时,它需要一个视图参数并使用转换器来获取一个对象。
使用分页器时出现以下错误,因为 viewParam 未传递给转换器
在 com.civi.HelloWorld.controller.UserConverter.getAsObject(UserConverter.java:36) 的 java.lang.Long.parseLong(Unknown Source)
每次页面刷新时,它都需要一个参数传递给它。
因此,当在数据表中使用分页器时,我需要发送 f:param 以便 viewParam 使用转换器加载对象
<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>
用户Bean
@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;
}
}
转换器
@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();
}
}