webtier@glassfish.java.net

Validation error while trying to change a value in a request scope bean

From: <webtier_at_javadesktop.org>
Date: Mon, 09 Mar 2009 07:15:43 PDT

- JSF 1.2_09-b01-BETA1 (Mojarra)
- Java 5 Update 17

Hello, everybody!

I'm having the following problem in my JSF web application:

I have a request scope backing bean. The first time this bean is loaded (I check
this with the ResponseStateManager.isPostBack() method) I fill a list of SelectItem
instances that are to be displayed in the JSF page in a <h:selectOneMenu> component.
The list goes, of course, to the <h:selectOneMenu>'s <f:selectItems> facet child
component. In the constructor I also define the value that goes to the value property
of the <h:selectOneMenu> component. This value is a property in the backing bean, as
is the list of SelectItem instances. Until now we have something like this:

The backing bean declaration in faces-config.xml:
<managed-bean>
    <managed-bean-name>solicitacaoGeral</managed-bean-name>
    <managed-bean-class>br.urca.solicitacoes.web.PaginaSolicitacaoGeral</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

The relevant parts of the JSF page:
<h:form id="form">
    <h:selectOneMenu value="#{solicitacaoGeral.setorOrigem}" id="foco">
        <f:selectItems value="#{solicitacaoGeral.setoresOrigem}" />
    </h:selectOneMenu>
</h:form>

The relevant parts of the backing bean class:
public class PaginaSolicitacaoGeral
{
    private final List<SelectItem> fSetoresOrigem = new ArrayList<SelectItem>();
    private Setor fSetorOrigem;

    public PaginaSolicitacaoGeral()
    {
        if (primeiraExibicao()) // First load (!ResponseStateManager.isPostBack())
        {
            // Fill fSetoresOrigem...
            fSetorOrigem = ...
        }
        else // Page submitted
        {
            // Read below...
        }
    }

    public List<SelectItem> getSetoresOrigem()
    {
        return fSetoresOrigem;
    }

    public Setor getSetorOrigem()
    {
        return fSetorOrigem;
    }
        
    public void setSetorOrigem(Setor setorOrigem)
    {
        fSetorOrigem = setorOrigem;
    }

    private boolean primeiraExibicao()
    {
        String idFerramentaExibicao =
            FacesContext.getCurrentInstance().getViewRoot().getRenderKitId();
        ResponseStateManager gerenciadorEstadoResposta =
            RenderKitUtils.getResponseStateManager(
            FacesContext.getCurrentInstance(), idFerramentaExibicao);
        return !gerenciadorEstadoResposta.isPostback(
            FacesContext.getCurrentInstance());
    }
}

But when the user submits the form and the bean constructor is called again
(this time the method ResponseStateManager.isPostBack() returns true),
in the else block in the constructor above, I need to fill fSetoresOrigem with
other values and also the fSetorOrigem field because, of course, the fSetorOrigem
field has to be a valid value that exists in the new fSetoresOrigem list.
JSF is not complaining about the change to the list items, but it is complaining
to the change to the fSetorOrigem field (the list value), even though it is a
valid value present in the list. So I'm getting this error message:

08:23:54,312 INFO [lifecycle] WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=form:foco[severity=(ERROR 2), summary=(form:foco: Validation Error: Value is not valid), detail=(form:foco: Validation Error: Value is not valid)]

I suppose that JSF is comparing the new value of the field fSetorOrigem with the value
it has in the view state. As the value is different it is raising the error. That's
what I suppose. But am I not able the change the value in the postback? I've already
checked and the value is valid. It corresponds to a value that exists in the list.
I really need a solution to this problem as I'm stuck with this and can't proceed until
I find a solution to this. What I am doing wrong and how can I solve this?

Thank you very much.

Marcos
[Message sent by forum member 'marcos_aps' (marcos_aps)]

http://forums.java.net/jive/thread.jspa?messageID=335850