JSF - 在ViewScoped bean中使用paginator时传递f:param

时间:2016-11-03 17:37:59

标签: jsf primefaces view-scope paginator viewparams

当这个页面加载时,它需要一个viewparam并使用转换器来获取一个对象。

使用paginator时出现以下错误,因为viewParam未传递给转换器

at java.lang.Long.parseLong(Unknown Source)     在com.civi.HelloWorld.controller.UserConverter.getAsObject(UserConverter.java:36)

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

所以当在数据表中使用pagintor时我需要发送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();
    } 
}

1 个答案:

答案 0 :(得分:1)

你的数据表周围需要一个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,因为它提供了一个转换器属性来保持转换逻辑DRY。