我有一个Bean,它为不同的实体返回"Notes"。例如,我可以有报价、客户、发票等的票据。
我不想为不同的实体创建多个Bean,而是希望有一个Bean为每个实体返回正确的Notes。我已经在JSF页面中向bean发送了两个信息"entityType"one_answers"objectId"。有了这两个参数,我可以在bean 中过滤我的数据表
目前我有以下代码,在这个例子中是"OFFER":
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:p="http://primefaces.org/ui"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:pe="http://primefaces.org/ui/extensions"
template="/WEB-INF/layout/portal/template.xhtml"
xmlns:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions">
<ui:define name="head" />
<ui:define name="title">Page</ui:define>
<ui:define name="content">
<p:growl id="growl" showDetail="false" sticky="false" life="3000" />
<div class="ui-g">
<div class="ui-g-12">
<div class="card card-w-title">
<p:tabView id="offerTab" dynamic="true" cache="false"
widgetVar="tabPanel">
<p:tab id="note"
disabled="#{offerEditController.modeOffer eq 'ADD'}">
<f:facet name="title">
<i class="fa fa-sticky-note Fs12 MarRight40"></i>
<h:outputText value=" Notizen" />
</f:facet>
<div class="EmptyBox10"></div>
<p:outputPanel id="notePanel">
<div class="ui-g ui-fluid">
<div class="ui-g-12 ui-lg-5">
<h:form id="noteForm"
rendered="#{noteRequestByObjectController.init('OFFER', offerEditController.offer.id, offerEditController.offer.idHash)}">
<p:dataTable id="noteListDatatable"
value="#{noteRequestByObjectController.lazyModel}"
var="note" widgetVar="noteTable" resizableColumns="false"
multiViewState="false" selectionMode="single"
selection="#{noteRequestByObjectController.selectedNote}"
filteredValue="#{noteRequestByObjectController.filteredNote}"
rows="50" pageLinks="10" paginatorPosition="bottom"
lazy="true" paginator="true" reflow="true"
currentPageReportTemplate="(Eintrag: {startRecord}-{endRecord} von {totalRecords}, Seite: {currentPage} von {totalPages})"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="50,100,200"
paginatorAlwaysVisible="false"
emptyMessage="Kein Eintrag gefunden">
<f:facet name="header">
<p:outputPanel id="functionalityPanel"
style="float:left; margin-top: -3px">
<p:commandButton value="Neu hinzufügen"
styleClass="green-btn White" icon="fa fa-plus-circle"
actionListener="#{noteRequestByObjectController.doCreateNew}"
update=":offerTab:noteForm:noteListDatatable, :offerTab:addNoteForm"
resetValues="true" ajax="true">
</p:commandButton>
</p:outputPanel>
<h:outputText id="overviewText"
value="Übersicht (#{noteRequestByObjectController.numberTotal})" />
<p:tooltip for="overviewText" position="top"
value="Die Gesamtzahl der Filterung" />
</f:facet>
<p:ajax event="rowSelect" ajax="true" immediate="true"
resetValues="true"
listener="#{noteRequestByObjectController.onSelectNote}"
update=":offerTab:noteForm, :offerTab:addNoteForm" />
<p:column sortBy="#{note.createDate}" visible="true">
<f:facet name="header">
<h:outputText value="Create" />
</f:facet>
<h:outputText id="createDateInfo"
style="margin-left: 5px;" value="#{note.createDate}"
styleClass="Fright">
<f:converter converterId="prettyTimeCustomConverter" />
</h:outputText>
</p:column>
</p:dataTable>
</h:form>
</div>
</div>
</p:outputPanel>
<div class="EmptyBox20"></div>
</p:tab>
</p:tabView>
</div>
</div>
</div>
</ui:define>
我的憨豆:-包含init函数和从数据库加载Notes的函数:
@ViewScoped
@Named
public class NoteRequestByObjectController extends LazyDataModel<Note>
implements SelectableDataModel<Note>, Serializable {
private static final long serialVersionUID = 2815796004558360299L;
private final Logger LOGGER = LoggerFactory.getLogger(NoteRequestByObjectController.class);
@EJB
private NoteService noteService;
@Inject
private LoginBean loginBean;
private List<Note> filteredNote;
private List<Note> selectedNoteList = new ArrayList<Note>();
private LazyDataModel<Note> lazyModel;
private List<Note> list;
private Note selectedNote;
private List<Boolean> toogleList;
private List<ObjectForSearchList> searchList = new ArrayList<ObjectForSearchList>();
private int numberTotal = 0;
public enum ModeNote {
EDIT, ADD
};
private ModeNote mode;
private String entityType;
private String visibleType;
private Long objectId;
private String objectIdHash;
private boolean startLoad = false;
/**
* Init
*/
public boolean init(String currentEntityType, Long currentObjectId, String currentObjectIdHash) {
try {
entityType = currentEntityType;
objectId = currentObjectId;
objectIdHash = currentObjectIdHash;
if (objectId == null || objectIdHash == null)
return true;
lazyModel = null;
// SearchList
initSearchList();
findNote();
selectedNote = new Note();
mode = ModeNote.ADD;
startLoad = true;
LOGGER.info("END init");
return true;
}
catch (Exception e) {
LOGGER.error(ExceptionUtils.getFullStackTrace(e));
}
return false;
}
/**
* Die eigentliche Suche
*/
public void findNote() {
LOGGER.info("START findNote");
lazyModel = null;
if (lazyModel == null) {
lazyModel = new LazyDataModel<Note>() {
@Override
public List<Note> load(int startingAt, int maxPerPage, String sortField, SortOrder sortOrder,
Map<String, Object> filters) {
try {
list = noteService.findAllNoteLazyLoading(searchList, startingAt, maxPerPage, sortField,
sortOrder, filters);
numberTotal = noteService.countNoteRowsLazyLoading(searchList);
lazyModel.setRowCount(numberTotal);
} catch (NoteNotFoundException e) {
lazyModel.setRowCount(0);
numberTotal = 0;
} catch (Exception e) {
lazyModel.setRowCount(0);
numberTotal = 0;
}
return list;
}
@Override
public Note getRowData(String rowKey) {
if (list != null && !list.isEmpty())
for (Note note : list) {
String s = "Note [id=" + note.getId() + "]";
if (s.equals(rowKey))
return note;
}
return null;
}
@Override
public Object getRowKey(Note note) {
return note;
}
};
}
}
代码正在工作,我为每个不同的实体和他的ID获得了该对象的正确Notes。
但我的问题是,如果我在日志中查看的话。init((函数正在调用多次。对于这种情况,是否有任何可能的解决方案来避免每个实体(客户、报价、发票等(都有许多Bean
非常感谢
表单的rendered
属性上有init方法,因此每次JSF评估表单是否需要渲染时,都会调用init方法。
您可以将init方法放在viewAction
中,类似于以下内容:
<f:view ....>
<f:metadata>
<f:viewAction action="#{noteRequestByObjectController.init('OFFER', offerEditController.offer.id, offerEditController.offer.idHash)}"/>
</f:metadata>