Prime 面临 selectOneListbox 验证错误:值无效
我一整天都在试图解决这个问题,我用谷歌搜索了很多,我找到了答案,但我不明白为什么这对我不起作用,我尝试了我想的一切。
我有 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
,尝试使用 selectOneMenu,尝试在没有转换器的情况下使用,任何成功:(
有人可以告诉我我在这里缺少什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
selectItems 提供的值应与您的情况下的操作值
crawlerCorpusTreatmentBean.corpusId
相匹配。我会尝试您不应该在
p:selectOneListbox
中需要任何转换器,我相信默认的数字转换器应该可以。如果您想操作完整的对象值(例如crawlerCorpusTreatmentBean.crawledData),则需要转换器。此类对象无法以明显的方式序列化,您需要提供自定义对象<->字符串转换。编辑:如果更正的标记不起作用,则可能意味着项目列表在请求之间丢失。它存储在 corpusTreatmentBean 中,因此该 bean 的范围应该比请求更宽,例如 View。或者,可以通过将 processContinue 逻辑移动到 corpusTreatmentBean
@PostConstruct
例如,在每个请求中重新创建列表。Values provided by selectItems should match the manipulated value,
crawlerCorpusTreatmentBean.corpusId
in 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
@PostConstruct
for example.