丰富:选择显示“值无效选项”;选择并将项目转换为对象之后 [英] rich:select shows "Value is not valid option" after selecting and converting the item to an object

查看:87
本文介绍了丰富:选择显示“值无效选项”;选择并将项目转换为对象之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 rich:select 中列出了 Test 类型的元素的集合(来自我的域),位于:以下代码:

I list a collection of elements of type Test (from my domain) in a rich:select using the following code:

test.xtml

<rich:select value="#{testBean.test}" id="cmbTest"
    converter="#{testConverter}" enableManualInput="false">
    <f:selectItems value="#{testBean.all}" var="test" itemLabel="#{test.name}" />
</rich:select>
<rich:message for="cmbTest" />
<h:commandButton id="btnSave" action="#{testBean.save}" value="Save" />

我还有一个自定义jsf转换器,可将选择的字符串值转换为<$ c类型的objets $ c>测试,反之亦然:

I also have a custom jsf-converter to convert the select string values into objets of type Test and vice versa:

TestConverter.java

@Component
@Scope("request")
public class TestConverter implements Converter {
    @Override
    public Object getAsObject(FacesContext facescontext, UIComponent uicomponent, String value) {
        if (value == null) return null;
        return new Test(Integer.parseInt(value), "test" + value);
    }

    @Override
    public String getAsString(FacesContext facescontext, UIComponent uicomponent, Object obj) {
        if (obj == null) return null;
        return ((Test) obj).getId().toString();
    }
}

(您可能会注意到,我正在使用Spring )xhtml文件的backing-bean定义如下:

(As you may notice, I'm using Spring) The backing-bean for the xhtml file is defined as follows:

TestBean.java

@Controller("testBean")
@Scope("session")
public class TestBean {
    private Test test;
    private List<Test> all; 

    public TestBean() {
        all = new ArrayList<Test>();
        for (int i = 0; i < 15; i++) { 
            all.add(new Test(1, String.format("test%d", i)));    
        }
    }

    public Test getTest() {
        return test;
    }

    public void save() {
        System.out.println("Save");
    }

    public List<Test> getAll() {
        return all;
    }
}

当我选择一个有效项目,我收到验证错误:值不是有效选项 ,如下所示:

When I press the "Save" button after selecting a valid item, I get the validation error: "Value is not a valid option", as shown below:

我已经调试了转换器 getAsObject 调用并正常工作,它按预期返回有效的 Test 实例(实际上,此测试项目是我最初发现此问题的工作项目,在该项目中,转换器成功使用注入的服务从数据库中检索对象。

I have debugged the Converter getAsObject call and it works fine, it returns a valid Test instance as expected (in fact, this "test" project is an isolated case for a work project where I first found this problem, and in that project the converter successfully uses an injected service to retrieve the object from database).

很显然,从未调用bean save 方法,因为视图因此错误而陷入jsf验证格式中。

Obviously the bean save method is never called as the view get stuck in the jsf validation fase with this error.

试图将 rich:select 替换为 h:selectOneMenu 但相同。我已经浏览了很多jsf-converter教程/ docs / refs,但是我仍然不知道我做错了什么。

Tried to replace rich:select with h:selectOneMenu but is the same. I have surfed a lot of jsf-converter tutorials/docs/refs but I still don't know what could I be doing wrong.

我正在使用maven和此处指出的Richfaces BOM配置,将版本替换为4.2.2。最终(希望更新可能会解决它)

I'm using maven and the Richfaces BOM configuration as pointed here, replaced the version with 4.2.2.Final though (hoping the update could fix it)

我发布了这里的测试项目

任何帮助都会非常感激,我花了很多时间试图为此找到解决方案,也许是一些简单/愚蠢的东西,但我只是厌倦了调试/搜索

Any help would be really appreciated, I have spent so much time trying to find a solution for this, maybe is something simple/stupid but I'm just tired of debuging/searching

推荐答案

您需要实现 equals () hashCode()在您的 Test 类中,以便JSF可以找到您选择的并在项目列表中转换项目。转换后,JSF会将选定的项目与列表中的项目进行比较,如果找不到,则会引发此错误。

You need to implement equals() and hashCode() inside your Test class so that JSF can find your selected and converted item in the list of the items. After conversion, JSF will compare the selected item against the items in the list and raise this error if it does not find it.

这篇关于丰富:选择显示“值无效选项”;选择并将项目转换为对象之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆