gpt4 book ai didi

中继器的 JSF2 寻呼/寻呼机

转载 作者:行者123 更新时间:2023-12-03 19:05:05 27 4
gpt4 key购买 nike

当您编写的每个代码都立即运行并且您未按时完成计划时,您是否知道这种感觉:-P 就像'哦,是的,现在我有时间让它变得完美'。这就是我现在的位置^^

所以我用 JSF (ui:repeat) 实现了一个转发器,我考虑了对所有实体的分页。有没有一种简单的方法可以做到这一点?我必须考虑哪些要点?

如果有人给我一些帮助,那就太好了。到目前为止,我的 googleskills 还没有帮助我:-P

干杯...

最佳答案

这是一个简单的示例,它应该可以让您了解如何实现这一点。

重复分页器:

public class RepeatPaginator {

private static final int DEFAULT_RECORDS_NUMBER = 2;
private static final int DEFAULT_PAGE_INDEX = 1;

private int records;
private int recordsTotal;
private int pageIndex;
private int pages;
private List<?> origModel;
private List<?> model;

public RepeatPaginator(List<?> model) {
this.origModel = model;
this.records = DEFAULT_RECORDS_NUMBER;
this.pageIndex = DEFAULT_PAGE_INDEX;
this.recordsTotal = model.size();

if (records > 0) {
pages = records <= 0 ? 1 : recordsTotal / records;

if (recordsTotal % records > 0) {
pages++;
}

if (pages == 0) {
pages = 1;
}
} else {
records = 1;
pages = 1;
}

updateModel();
}

public void updateModel() {
int fromIndex = getFirst();
int toIndex = getFirst() + records;

if(toIndex > this.recordsTotal) {
toIndex = this.recordsTotal;
}

this.model = origModel.subList(fromIndex, toIndex);
}

public void next() {
if(this.pageIndex < pages) {
this.pageIndex++;
}

updateModel();
}

public void prev() {
if(this.pageIndex > 1) {
this.pageIndex--;
}

updateModel();
}

public int getRecords() {
return records;
}

public int getRecordsTotal() {
return recordsTotal;
}

public int getPageIndex() {
return pageIndex;
}

public int getPages() {
return pages;
}

public int getFirst() {
return (pageIndex * records) - records;
}

public List<?> getModel() {
return model;
}

public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}

}

bean 角,扁 bean :
public class TestBean {

private List<String> list;
private RepeatPaginator paginator;

@PostConstruct
public void init() {
this.list = new ArrayList<String>();
this.list.add("Item 1");
this.list.add("Item 2");
this.list.add("Item 3");
this.list.add("Item 4");
this.list.add("Item 5");
this.list.add("Item 6");
this.list.add("Item 7");
this.list.add("Item 8");
this.list.add("Item 9");
this.list.add("Item 10");
this.list.add("Item 11");
paginator = new RepeatPaginator(this.list);
}

public RepeatPaginator getPaginator() {
return paginator;
}

}

XHTML:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
template="/WEB-INF/template/default.xhtml">

<ui:define name="content">
<h:form>
<ui:repeat value="#{testBean.paginator.model}" var="listItem">
<div>
<h:outputText value="#{listItem}"/>
</div>
</ui:repeat>
<h:commandButton value="&lt; prev" action="#{testBean.paginator.prev}"/>
<h:outputText value="#{testBean.paginator.pageIndex} / #{testBean.paginator.pages}"/>
<h:commandButton value="next &gt;" action="#{testBean.paginator.next}"/>
<h:inputHidden value="#{testBean.paginator.pageIndex}"/>
</h:form>
</ui:define>
</ui:composition>

关于中继器的 JSF2 寻呼/寻呼机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3519569/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com