日期:2014-05-16  浏览次数:20539 次

JSF2 Primeface2 LazyDataModel 使用

第一步 新建Web项目,搭建好项目环境,将相关依赖的包加入到项目里,使用的服务器为JBoss7

?

第二步 页面代码

?

? ? ? ? ? ?<p:outputPanel id="updteTabelUser">

		<p:dataTable var="user" value="#{userBean.lazyModel}" id="userTable"
			paginator="true" rows="10" lazy="true" paginatorPosition="bottom"
			paginatorTemplate=" {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {CurrentPageReport}"
			currentPageReportTemplate="{currentPage}/{totalPages}"
			emptyMessage="#{msgs.empty_data_msg}" selectionMode="single">
			<f:facet name="header">
                                                         用户列表
                </f:facet>
            <p:column headerText="用户名">
				<h:outputText value="#{user.name}" />
			</p:column>
			<p:column headerText="姓名">
				<h:outputText value="#{user.userName}" />
			</p:column>
			<p:column headerText="手机">
				<h:outputText value="#{user.cellphoneNo}" />
			</p:column>
			<p:column headerText="收购点">
				<h:outputText value="#{user.purchaseLocName}" />
			</p:column>
			
			<p:column headerText="操作" styleClass="operating_90">
				<p:commandButton rendered="#{loginBean.contains('02')}" update="carDetail"
					image="ui-icon ui-icon-zoomin" oncomplete="singleCarDialog.show()"
					title="查看">
					<f:setPropertyActionListener value="#{user}"
						target="#{userBean.currentUser}" />
				</p:commandButton>
				
				<p:commandButton  ajax="false"
					image="ui-icon ui-icon-person" title="角色设置" rendered="#{loginBean.contains('03')}"
					action="#{userBean.roleSet}">
					<f:setPropertyActionListener value="#{user}"
						target="#{userBean.currentUser}" />
				</p:commandButton>
			
				<p:commandButton image="ui-icon ui-icon-org" title="组织分配" rendered="#{loginBean.contains('04')}"
					action="#{userOrgBean.input}" ajax="false" >
					
					<f:setPropertyActionListener value="#{user}"
						target="#{userBean.currentUser}" />
				</p:commandButton>
			</p:column>
		</p:dataTable>
	</p:outputPanel>

? ?p:dataTable 标签相关属性请参照Primeface文档,若没有文档可以发邮件给我

?

?

第三步 后台Bean代码

?

?

?

? ?@Named

@ConversationScoped
public class UserBean{
    /** 数据模型 */
    private LazyDataModel<User> lazyModel;
    @Inject
    private EntityService entityService;

    @PostConstruct
    private void initLazyModel(){
       String sql = "select u form User u' ;
       lazyModel = this.entityService.findModel(sql.toString());
    }





    /**
     * @return the lazyModel
     */
    public LazyDataModel<User> getLazyModel() {

        return lazyModel;
    }

    /**
     * @param lazyModel
     *            the lazyModel to set
     */
    public void setLazyModel(LazyDataModel<User> lazyModel) {
        this.lazyModel = lazyModel;
    }


}
?

? 主要逻辑代码EntityService对LaydataModel的封装具体实现

??public <T> LazyDataModel<T> findModel(final String jpql, final Object... values) {?

        LazyDataModel<T> lazyModel = new LazyDataModel<T>() {
            @Override
            public List<T> load(int first, int pageSize, String sortField, boolean sortOrder, Map<String, String> mapFilters) {
                // 得到总记录数
                Integer count = Long.valueOf(countHqlResult(jpql, values)).intValue();
                this.setRowCount(count);
                //  得到查询结果
                Query q = createQuery(jpql, values);
                setPageParameterToQuery(q, first, pageSize);
                List result = q.getResultList();
                
                return result;
            }
        };

        
        lazyModel.setRowCount(1);
        return lazyModel;
    }