我希望在表单验证后保留选定的值。我的代码在这里:
<h:form id="advanceSearchForm">
<p:dataGrid value="#{advancedSearchBean.selectedCriteriasList}" id="advancedSearchGrid" var="criteria" rowIndexVar="rowIndex"
columns="2" layout="grid" paginator="false" >
<h:panelGrid columns="1" id="criteriaPanel">
<p:panel id="criteria">
<div>
<!-- select criteria -->
<p:selectOneMenu value="#{criteria}" label="#{criteria.name}"
valueChangeListener="#{advancedSearchBean.addNewCriteria}"
converter="#{advancedSearchBean.criteriaConverter}">
<f:selectItems value="#{advancedSearchBean.criterias}"
var="crt"
itemLabel="#{crt.name}"
itemValue="#{crt}"
itemDisabled="#{advancedSearchBean.disableItem(crt.id)}" />
<f:ajax execute="@this"/>
</p:selectOneMenu>
<!-- launch date -->
<p:outputPanel rendered="#{criteria.id != null and (criteria.id == BusinessConstants.ADVANCED_SEARCH_FILTER_LAUNCH_DATE_ID)}">
<p:outputPanel>
<h:outputText value="From" styleClass="passportLabel" />
<p:inputMask required="true" id="launchStartDate" value="#{advancedSearchBean.launchStartDate}" mask="99/9999">
<f:convertDateTime pattern="MM/yyyy" timeZone="#{sessionScope.identity.timezone}" />
<p:ajax event="change" listener="#{advancedSearchBean.changeLaunchStartDate()}" />
</p:inputMask>
</p:outputPanel>
<p:outputPanel>
<h:outputText value="To" styleClass="passportLabel" />
<p:inputMask required="true" id="launchEndDate" value="#{advancedSearchBean.launchEndDate}" mask="99/9999">
<f:convertDateTime pattern="MM/yyyy" timeZone="#{sessionScope.identity.timezone}" />
<p:ajax event="change" listener="#{advancedSearchBean.changeLaunchEndDate()}" />
</p:inputMask>
</p:outputPanel>
</p:outputPanel>
</div>
</p:panel>
</h:panelGrid>
</p:dataGrid>
<!-- run search button -->
<div>
<p:commandButton id="runAdvancedSearch" value="Run search" action="#{advancedSearchBean.doAdvancedSearch}"
update="advanceSearchForm:advancedSearchGrid"/>
</div>
</h:form>
当我输入错误的日期并点击“搜索”按钮时,我收到了验证错误消息,而且我也在selectOneMenu中丢失了所选的值。
奇怪的是,如果重定向到另一个页面,之后我又回来了,它可以...我的豆子是:
@ManagedBean(name = "advancedSearchBean")
@ViewScoped
public class AdvancedSearchBean {
private Map<Long, String> criteriaMap;
private List<AutocompleteCriteria> criterias;
private List<AutocompleteCriteria> selectedCriteriasList;
@PostConstruct
public void init() {
criteriaMap = BusinessConstants.ADVANCED_SEARCH_CRITERIA.entrySet().stream().sorted(Entry.comparingByValue()).
collect(Collectors.toMap(Entry::getKey, Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
criterias = new ArrayList<AutocompleteCriteria>();
for(Iterator<Map.Entry<Long, String>>it = criteriaMap.entrySet().iterator();it.hasNext();){
Map.Entry<Long, String> entry = it.next();
criterias.add(new AutocompleteCriteria(entry.getKey(), entry.getValue()));
}
criteriaConverter = new AutocompleteConverter(criterias);
selectedCriteriasList = new ArrayList<AutocompleteCriteria>();
selectedCriteriasList.add(new AutocompleteCriteria(BusinessConstants.ADVANCED_SEARCH_FILTER_NO_CRITERIA, criteriaMap.get(BusinessConstants.ADVANCED_SEARCH_FILTER_NO_CRITERIA)));
}
答案 0 :(得分:0)
在p:selectOneMenu
value="#{criteria}"
而不是value="#{advancedSearchBean.criteria}"
。