This question already has answers here:
Validation Error: Value is not valid
                                
                                    (3个答案)
                                
                        
                4年前关闭。
            
        

我整天都在尝试解决此问题,我在Google上搜索了很多东西,找到了答案,但我不明白为什么这对我不起作用,我尝试了所有我认为的方法。

我有素词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,尝试不使用转换器,任何成功:(

有人可以告诉我我在这里想念什么吗?

最佳答案

selectItems提供的值应与操作值匹配,在您的情况下为crawlerCorpusTreatmentBean.corpusId。我会尝试

itemValue="#{crawledData.id}"


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

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

09-30 14:27