JSF - 在 ViewScoped bean 中使用分页器时传递 f:param

JSF - Passing an f:param when the paginator is used in a ViewScoped bean

当此页面加载时,它接受一个视图参数并使用一个转换器来获取一个对象。

我在使用分页器时收到以下错误,因为 viewParam 没有传递给转换器

在 java.lang.Long.parseLong(来源不明) 在 com.civi.HelloWorld.controller.UserConverter.getAsObject(UserConverter.java:36)

每次页面刷新时都需要传递一个参数给它。

因此,当在数据表中使用分页器时,我需要发送 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>

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;
    }

}

转换器

@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();
    } 
}

你的数据表周围需要一个 h:form,然后它是一个 AJAX 组件,而不是在分页时重新加载页面。

对于基本的 GET 页面链接,您必须将视图参数传递给每个重定向。否则它会丢失。像这样:

<h:link>
    <o:param name="idUser" value="#{userBean.tipTourUser}" converter="#{userConverter}" />
    <f:param name="page" value="#{userBean.page}" />
</h:link>

提示:

使用 omnifaces o:param,因为它提供了一个转换器属性来保持你的转换逻辑干燥。