javajsftrinidadselectoneradio

Default value for <tr:selectOneRadio> if the value is from database object


I am struggling with this for a really long time. I am changing code and I need to put default value to selectOneRadio element from Trinidad. I have object from database (for example called result). This object has attribute decision. In the select element value parameter was used this way value="#{result.decision}".

Now I need If result.decision is empty set value to True. If value is False let/set value to False. And if value is True let/set value to True. I tried something like this in template:

 <tr:selectOneRadio id="decision" value="#{viewBean.changeValueToOne(result)}" autoSubmit="true" immediate="true">
        <f:selectItem itemValue="true" itemLabel="Yes"/>
        <f:selectItem itemValue="false" itemLabel="No"/>
 </tr:selectOneRadio>

And this function in viewBean:

public Boolean changeValueToOne(ResultDTO result) {
    if (result.getDecision() == null) {
        result.setDecision(Boolean.TRUE);
    }
    return result.getDecision();
}

But when I did this I got only one String value instead of selectOneRadio element. There was only "Yes" or "No" value.

How can I set default value to selectOneRadio element to the value from the object and get a normal selectOneRadio, not just one value.

EDIT

This is code from template. Decision is the object from the database where I need to change value:

<tr:selectOneRadio id="decisionYes" value="#{viewBean.defaultDecision}" valueChangeListener="#{viewBean.valueChangedOneRadioListener(decision)}"
                                           autoSubmit="true" immediate="true">
     <f:selectItems value="#viewBean.decisionStates}"/>
</tr:selectOneRadio>

This is a listener method from viewBean:

public void valueChangedOneRadioListener(DecisionDTO decision, ValueChangeEvent event)
{
    if(event.getNewValue() != null) {
        if((Boolean)event.getNewValue().equals(Boolean.TRUE))
        {
            decision.setDecision(Boolean.TRUE);
        } else {   
            decision.setDecision(Boolean.FALSE);
        }
        // this is variable used in selectRadio
        this.setDefaultVyjadreni((Boolean)event.getNewValue());
    }
}

When I run this code I got error because method wasn't found.

javax.faces.event.AbortProcessingException: Method valueChangedOneRadioListener not found

Solution

  • I use integer Values for the Trinidads selectOneRadio. You set the default by setting yourValue. You react on a change event with a valueChangeListener.

    <tr:selectOneRadio
        id="maschineId"
        value="#{yourBean.yourIntValue}"
        valueChangeListener="#{yourValueChangeListener}"
        autoSubmit="true">
            <f:selectItem itemValue="1"/>
            <f:selectItem itemValue="0"/>
    </tr:selectOneRadio
    

    Your backingBean looks somehow like this then:

    public void yourValueChangeListener(ValueChangeEvent event)
    {
        if(event.getNewValue() == null)
            return;
    
        if((Integer)event.getNewValue() ==  1)
        {
            //react on a change to 1
        }
        if((Integer)event.getNewValue() ==  0)
        {
            //react on a change to 0
        }
    }
    public int getYourIntValue() {
        return yourIntValue;
    }
    
    public void setYourIntValue(int yourIntValue) {
        this.yourIntValue = yourIntValue;
    }