1

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?

Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143

1 Answers1

0

Values provided by selectItems should match the manipulated value, crawlerCorpusTreatmentBean.corpusId in your case. I would try

itemValue="#{crawledData.id}"

You 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 @PostConstruct for example.

mrembisz
  • 12,722
  • 7
  • 36
  • 32
  • Thanks, but this does not works either. When I tried itemValue="#{crawledData.id}"/> does not worked for me (the same error), so I tried to implement converter, but no success. – Paulius Matulionis Feb 12 '12 at 13:55
  • @PauliusMatulionis What do you get as an error then? – mrembisz Feb 12 '12 at 14:20
  • I get this error: Validation Error: Value is not valid – Paulius Matulionis Feb 12 '12 at 14:22
  • @PauliusMatulionis The original value of crawlerCorpusTreatmentBean.corpusId must be present in your items list , ie. there must be a crawledData with id equal to this value on the list. If it's null and there is no crawledData with null id you should add maybe something like `` under `p:selectOneListbox`. – mrembisz Feb 12 '12 at 14:28
  • I add selectItem: But still getting the same error :( I do not know what to try anymore. I looks like I tried everything and it still does not work. – Paulius Matulionis Feb 12 '12 at 14:36
  • What is type of crawlerCorpusTreatmentBean.corpusId? Also can you try to remove `required` from p:selectOneListbox? – mrembisz Feb 12 '12 at 14:43
  • crawlerCorpusTreatmentBean.corpusId is string defined in managed bean CrawlerCorpusTreatmentBean private String corpusId. I tried to remove required attribute but it did not help. Still the same error. I tried to manualy add value and when I select this item it works, but it does not works on any other items from – Paulius Matulionis Feb 12 '12 at 14:52
  • Can you post html generated by listbox in your question with some option elements at least? – mrembisz Feb 12 '12 at 14:56
  • Here it is:
    • Example
    • http://recipe.com/recipes/occasions/
    • http://recipe.com/recipes/chili/
    – Paulius Matulionis Feb 12 '12 at 15:02
  • 1
    Also are you sure crawledDataList contains the same values in subsequent requests? I get an impression some values disappear. – mrembisz Feb 12 '12 at 15:03
  • I think this will be a problem of subsequent requests. Because I have many of selectOneMenu and they are working properly form me. But this is different. All other selectOneMenus is formed by one click of the button. This: Click one -> Formed One selectOneMenu, then I choose value, and click the button, this click calls processContinue method of CrawlerCorpusTreatmentAction managed bean and forms crawledDataList. Then it is setted in CrawlerCorpusTreatmentBean and from this bean is displayed in the selectOneListBox. But I do not understand why this could be a problem. – Paulius Matulionis Feb 12 '12 at 15:10
  • If the scope od treatmentBean is request then this list is immediately lost and unavailable in the subsequent request. – mrembisz Feb 12 '12 at 15:21
  • So in what scope I should put this bean? I do not need session scope to cary objects in all session lifecycle or I should bind it somehow using bindings? – Paulius Matulionis Feb 12 '12 at 15:30
  • Thanks. Finally my issue is solved. – Paulius Matulionis Feb 12 '12 at 15:37