在h:selectOneMenu中使用JSF Converter会导致验证错误:值无效 [英] Using JSF Converter in h:selectOneMenu results in Validation Error: Value not valid

查看:129
本文介绍了在h:selectOneMenu中使用JSF Converter会导致验证错误:值无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个SelectOneMenu:

I have this SelectOneMenu:

<h:selectOneMenu value="#{orderController.requestVO.requestSituation}">
    <f:converter converterId="ComboConverter"/>
    <f:selectItems value="#{orderController.requestSituation}" var="requestSituation"
                                                   itemLabel="#{requestSituation.description}" itemValue="#{requestSituation}" />
</h:selectOneMenu>

requestSituation是用RequestSituationVO填充的ArrayList 它已正确填充,并生成以下HTML:

The requestSituation is a ArrayList filled with RequestSituationVO It is populated correctly, generating this HTML:

<select name="j_idt14:j_idt20" size="1">
        <option value="13">Pedido Recusado</option>
    <option value="11">Pedido Validado</option>
    <option value="12" selected="selected">Pedido Confirmado</option>
    <option value="12" selected="selected">Pedido Faturado</option>
</select>

我有这个Converter,这是我感到困惑的地方,我读了很多书,我知道它必须做什么,但不知道它是如何工作的.

I have this Converter and here is where I'm confused, I've read a lot and I know what it has to do but not how it works.

这里是:

@FacesConverter(value = "ComboConverter", forClass = RequestSituationVO.class)
public class ComboConverter implements Converter
{

    private static RequestSituationVO requestSituationVO = new RequestSituationVO();

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {
        requestSituationVO.setId(Integer.valueOf(value));
        requestSituationVO = (RequestSituationVO) new RequestSituationBO().getRequestSituation(requestSituationVO).toArray()[0];
        return (RequestSituationVO) requestSituationVO;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value)
    {
        requestSituationVO = (RequestSituationVO) value;
        String teste = String.valueOf(requestSituationVO.getId());
        return teste;
    }
}

提交页面时,我认为SelectOneMenu将自动为requestVO中的requestSituation方法设置值.但是当我提交时,我收到一条消息Value not valid引用了我的SelectOneMenu.

When I submit my page, I think the SelectOneMenu will automatically set the value for the requestSituation method from requestVO. But when I submit, I get a message Value not valid referencing to my SelectOneMenu.

我需要在RequestSituationVO上设置选定的值,以便将其发送到Business方法.

What I need is to set the selected value on my RequestSituationVO so I can send it to the Business method.

推荐答案

您的转换器很好.否则,您将获得类似

Your converter is fine. You would otherwise have gotten a conversion error something like

空转换器"的转换错误设置值"com.example.RequestSituationVO@hashcode"

Conversion Error setting value 'com.example.RequestSituationVO@hashcode' for 'null Converter'

您遇到验证错误.当 Object#equals() 测试未返回true. JSF正在检查以防止被篡改的请求进行攻击.在您的特定情况下,可能有以下原因:

You've a validation error. This particular one will be thrown when the Object#equals() test of the selected item hasn't returned true for any of the available items in the list. JSF is checking that to prevent attacks by tampered requests. This can in your particular case have the following causes:

  • RequestSituationVO类的equals()方法丢失或损坏.
  • #{orderController.requestSituation}在显示表单的请求和处理表单提交的请求之间发生了不兼容的更改.
  • The equals() method of the RequestSituationVO class is missing or broken.
  • The #{orderController.requestSituation} has incompatibly changed in between the request of displaying the form and the request of processing the form submit.

我认为是前者.鉴于您的RequestSituationVO具有唯一标识对象的Integer id属性,应该这样做:

I think that it's the former. Given the fact that your RequestSituationVO has an Integer id property which uniquely identifies the object, this should do:

@Override
public boolean equals(Object other) {
    return (other instanceof RequestSituationVO) && (id != null)
        ? id.equals(((RequestSituationVO) other).id)
        : (other == this);
}

@Override
public int hashCode() {
    return (id != null)
        ? (this.getClass().hashCode() + id.hashCode())
        : super.hashCode();
}

如果equals()方法不是问题,则是后者的原因.这可以通过确保#{orderController.requestSituation}在显示表单和处理表单提交期间返回完全相同的列表来解决.这可以通过将bean放置在视图范围中而不完全在getter中执行业务逻辑来实现.或者,如果它实际上表示应用程序范围的数据,则可以将其重构为一个单独的应用程序范围的Bean.

If the equals() method is not the problem, then it's the latter cause. This can be solved by making sure that the #{orderController.requestSituation} returns exactly the same list during displaying the form and processing the form submit. This can be achieved by placing the bean in the view scope and not doing the business logic in the getter at all. Or if it actually represents applicationwide data, you could refactor it to a separate application scoped bean.

这篇关于在h:selectOneMenu中使用JSF Converter会导致验证错误:值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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