I have been trying to solve this problem all day, I googled a lot, I found answers but I can not understand why this is not working for me, I tried everything that I thought.
I have primefaces selectOneListbox:
<p:selectOneListbox id="idCrawledDataSelectMenu"
required="true"
value="#{crawlerCorpusTreatmentBean.corpusId}"
converter="crawledDataConverter"
style="height: 200px; width: 500px;">
<f:selectItems id="idCrawledDataItems"
value="#{crawlerCorpusTreatmentBean.crawledDataList}"
var="crawledData"
itemLabel="#{crawledData.url}"
itemValue="#{crawledData}"/>
</p:selectOneListbox>
I have a converter:
@FacesConverter(value = "crawledDataConverter")
public class CrawledDataConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) {
return s;
}
@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) {
if (o instanceof CrawlerCorpusData) {
CrawlerCorpusData data = (CrawlerCorpusData) o;
return data.getId();
}
return null;
}
}
I there is my managed bean where I form my crawledDataList object.
@ManagedBean(name="crawlerCorpusTreatmentAction")
@RequestScoped
public class CrawlerCorpusTreatmentAction extends BaseAction implements Serializable {
/**
* Logger.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(CrawlerCorpusTreatmentAction.class);
/**
* Processes continue action of crawled corpus treatment request.
*
* @return success if action was success, otherwise - failure
*/
public String processContinue() {
CrawlerCorpusTreatmentBean corpusTreatmentBean = getBean(Beans.CRAWLER_CORPUS_TREATMENT_BEAN);
try {
CrawlerInfoWrapper crawlerInfoWrapper = createCrawlerInfoWrapper();
List<CrawledData> crawledDataList = crawlerInfoWrapper.getCrawledData(corpusTreatmentBean.getCorpusDomain());
List<CrawlerCorpusData> corpusDataList = BeanUtils.convertCrawledDataFromPojo(crawledDataList);
corpusTreatmentBean.setCrawledDataList(corpusDataList);
return ACTION_SUCCESS;
} catch (SystemException e) {
String errorMessage = MessageFactory.getErrorString(MessageFactory.ERROR_SYSTEM_ERROR);
LOGGER.error(errorMessage, e);
addErrorMessage(errorMessage + e.getMessage());
return ACTION_FAILURE;
} catch (CrawlerInfoException e) {
String errorMessage = MessageFactory.getErrorString(MessageFactory.ERROR_CRAWLER_INFO_ERROR);
LOGGER.error(errorMessage, e);
addErrorMessage(errorMessage + e.getMessage());
return ACTION_FAILURE;
}
}
public String processChooseCorpus() {
CrawlerCorpusTreatmentBean corpusTreatmentBean = getBean(Beans.CRAWLER_CORPUS_TREATMENT_BEAN);
corpusTreatmentBean.getCorpusId();
return ACTION_SUCCESS;
}
My CrawlerCorpusData object:
public class CrawlerCorpusData {
private String id;
private String url;
public String getId() {
return id;
}
public CrawlerCorpusData() {
}
public CrawlerCorpusData(String id, String url) {
this.id = id;
this.url = url;
}
public void setId(String id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof CrawlerCorpusData)) {
return false;
}
CrawlerCorpusData data = (CrawlerCorpusData) obj;
return this.id == data.getId();
}
}
I tried using List<SelectItem>, tried to use selectOneMenu, tried to use without converter, any success 🙁
Can someone tell me what am I missing here?
Values provided by selectItems should match the manipulated value,
crawlerCorpusTreatmentBean.corpusIdin your case. I would tryYou should not be needing any converter in
p:selectOneListbox, the default numeric one should do I believe. A converter would be necessary if you wanted to manipulate a full object value, like crawlerCorpusTreatmentBean.crawledData for example. Such object cannot be serialized in an obvious way and you need to provide a custom object<->string conversion.EDIT: If the corrected markup does not work, it may mean the items list is lost between requests. It is stored in corpusTreatmentBean, so this bean should have scope wider than request, for example View. Alternatively the list can be recreated in each request by moving processContinue logic to corpusTreatmentBean
@PostConstructfor example.