Anand Builders
Anand Builders

Reputation: 141

Update Action in Bean for JSF

Although it is a simple question . Need an answer for this. I have written a jsp page as :

<body>
<f:view>
    <f:loadBundle basename="message" var="msg"/>
<h:form id="edit">

    <h:panelGroup>      
        <h:selectOneRadio id="CompletePublication" layout="pageDirection">
                <f:selectItem itemLabel="SUCCESS" itemValue="true"/>
                <f:selectItem itemLabel="FAILED" itemValue="false"/>
        </h:selectOneRadio>                         
     </h:panelGroup>
        <h:commandButton id="setAction" immediate="true" action="#{user.completePublication}" value="#{msg.Button_OK}"/>
        <h:commandButton id="cancel" immediate="true" action="#{user.cancelCompletePublish}" value="#{msg.Button_CANCEL}"/>
</h:form>
</f:view>               
</body>

It needs to be handled under theBean:

public class User implements Serializable {

    private String name;
    private boolean action;

    public boolean getAction()
    {
        System.out.println("Get");
        return action;
    }
    public void setAction(boolean action)
    {
        this.action=action;
        System.out.println("Set");
    }
    public String completePublication(){
        if (action==true){
            System.out.println("Value of True Action - " + action);
            return "updated";
        }
        if (action==false){
            System.out.println("Value of False Action - " + action);
            return "notupdated";
        }
        return null;
    }
    public String cancelCompletePublish()
    {
        System.out.println("Hi");
        return null;
    }

}

So can any one help on this. At the output evertime i see the "Value of False Action - False"

Upvotes: 0

Views: 119

Answers (1)

Andy
Andy

Reputation: 6568

In your <h:selectOneRadio> you are not passing the value of the selected choice back to your bean when the form is submitted. Therefore, the action variable will never get updated. Assuming that you refer to your managed bean as user in the page, you will need to modify that component by adding the value attribute. Therefore, change

<h:selectOneRadio id="CompletePublication" layout="pageDirection">

to

<h:selectOneRadio id="CompletePublication" value="#{user.action}" layout="pageDirection">

For more information on <h:selectOneRadio> please refer to this link. Also, as mentioned by @Xtreme Biker, you need to understand what immediate="true" does when it is applied to a particular component. For your case, when a user hits any of the <h:commandButton>, the Update Model Phase (as well as other phases) will be skipped. In other words, action will never be set to true when the user selects SUCCESS and then clicks the OK (first) button. That's why you always see Value of False Action - False in your output. To fix this just remove the immediate attribute in the first <h:commandButton> like so

<h:commandButton id="setAction" action="#{user.completePublication}" value="#{msg.Button_OK}"/>

After removing that attribute, make the following change in your completePublication method

public String completePublication(){
    if (action){
        System.out.println("Value of True Action - " + action);
        return "updated";
    }
    else {
        System.out.println("Value of False Action - " + action);
        return "notupdated";
    }
}

There is no need to return null since action will either be true or false.

NOTE I intentionally did not go into detail on the behavior of the immediate attribute. If you want to understand it, you will need to spend some time to try and understand the JSF lifecycle phases. In fact, you will need to be somewhat comfortable with these concepts as you dive deeper into JSF. Both @johny and @Xtreme Biker gave you some good links to start with. I am copying them below just in case their comments get erased.

Debug JSF Lifecycle

Doubt on immediate attribute for command button

Upvotes: 1

Related Questions