kooker
kooker

Reputation: 373

Input value not processed in request scoped bean when using conditional rendering

I know this type of question has been asked million times here, but I couldn't find a solution for my problem in relevant posts.

JSF 1.2

I have a request-scoped bean with a method used as valueChangeListener:

class DoStuff{
    ...
    public void step1ChkStuffIncluded_CheckedChanged(ValueChangeEvent event){
        StuffDocument cd = (StuffDocument)getInfo("StuffDocument");
        if(cd == null){
            Logger.Error("DoStuff", "step1ChkStuffIncluded_CheckedChanged", "No stuff document (null)");
            return;
        }

        if (step1ChkStuffIncludedChecked){
            cd.handleChecked();
        }
        else{
            cd.handleUnchecked();
        }
    }
    ...
}

by a selectBooleanCheckbox component as follows (.jspx):

    ...
    </h:panelGroup> 
    <h:panelGroup rendered="#{DoStuff.pnlStep1}">
        <p>
        <label for="step1ChkStuffIncluded">#{DoStuff.step1ChkStuffIncludedText}</label>

        <h:selectBooleanCheckbox
            id="step1ChkStuffIncluded"
            onchange="submit();"
            value="#{DoStuff.step1ChkStuffIncludedChecked}"
            valueChangeListener="#{DoStuff.step1ChkStuffIncluded_CheckedChanged}">
        </h:selectBooleanCheckbox></p>
    </h:panelGroup>
    <div id="someDiv">
    ...

where

xmlns:h="http://java.sun.com/jsf/html"

Whenever the bean's scope is session, both setter and the listener for the checkbox are executed, but not in request scope. Unfortunately I can't find any clues other than that.

Any advise is greatly appreciated. Requests for further clarifications are welcome.

Upvotes: 2

Views: 1613

Answers (1)

BalusC
BalusC

Reputation: 1108722

You've there a rendered="#{DoStuff.pnlStep1}" on a parent component. During processing of the form submit, JSF will as part of attack safeguard determine if the input component (and all of its parents) is rendered according to the server side conditions. If it's not rendered, then it will simply be skipped altogether during the processing.

That it works in a session scoped bean but fails in a request scoped bean indicates that the value behind rendered="#{DoStuff.pnlStep1}" is determined based on some request based variable/condition which was present during the request of displaying the form, but is absent during the request of processing the form submit.

To fix this, you need to make sure that you preserve exactly the same variable/condition for the value behind rendered="#{DoStuff.pnlStep1}" during the request of processing the form submit. There are several ways to achieve this, depending on the nature of the condition and how you're submitting the form. One of the ways is to pass the request based variable/condition back as a request parameter by <f:param> or <h:inputHidden>.

The canonical JSF 2.0 fix would be to put the bean in the view scope which is not available in JSF 1.2, but can be simulated using Tomahawk's <t:saveState> component.

See also:

Upvotes: 2

Related Questions