Prime面板selectOneListbox验证错误:值无效

时间:2012-02-12 13:32:16

标签: java jsf primefaces

我一直在努力解决这个问题,我搜索了很多,我找到了答案,但我无法理解为什么这对我不起作用,我尝试了我想到的一切。

我有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>

我有一个转换器:

@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;
    }
}

我有托管bean,我在其中形成了crawledDataList对象。

@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;
    }

我的CrawlerCorpusData对象:

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();
    }
} 

我尝试使用List<SelectItem>,尝试使用selectOneMenu,试图在没有转换器的情况下使用,取得任何成功:(

有人能告诉我在这里缺少什么吗?

1 个答案:

答案 0 :(得分:0)

selectItems提供的值应与您的情况下的操纵值crawlerCorpusTreatmentBean.corpusId相匹配。我会试试

itemValue="#{crawledData.id}"

你不应该在p:selectOneListbox中需要任何转换器,我应该相信默认的数字转换器。如果您想要操作完整的对象值,例如crawlerCorpusTreatmentBean.crawledData,则需要转换器。此类对象无法以明显的方式序列化,您需要提供自定义对象&lt; - &gt;字符串转换。

编辑:如果更正的标记不起作用,则可能意味着请求之间的项目列表丢失。它存储在corpusTreatmentBean中,因此该bean的范围应大于请求,例如View。或者,可以通过将processContinue逻辑移动到corpusTreatmentBean @PostConstruct来在每个请求中重新创建列表。