-1

I have this code:

<h:selectOneMenu id="select" value="#{bean.code}"
                        valueChangeListener="#{bean.setAdress}">
    <f:selectItems value="#{bean.list}" />
        <f:ajax execute="dest" render="dest" />
</h:selectOneMenu>

<h:selectOneRadio id="adressChoice" value="#{bean.choice}">
      <f:selectItem id="item1" itemLabel="Post adress" itemValue="1" />
      <f:selectItem id="item2" itemLabel="Other" itemValue="2" />
      <f:ajax execute="adressChoice" render="dest" />
</h:selectOneRadio>

<h:inputText id="dest" value="#{bean.dest}" />

In my bean, I have this method:

public void setAdress(final ValueChangeEvent event) {
      if (choice.equals("1")) {
            dest = "rererer";
      } 
}

My problem is, even though the method setAdress is called, my inputText does not change. Why? Is it because of the execute attribute?

Thanks.

Chris
  • 107
  • 1
  • 1
  • 8

1 Answers1

0

valueChangeListener is called after validation and before updating model (in Jsf life cycle), so value is not changed use :

<h:selectOneMenu id="select" value="#{bean.code}">
    <f:selectItems value="#{bean.list}" />
        <f:ajax listener="#{bean.setAdress}" />
</h:selectOneMenu>

public void setAdress(AjaxBehaviorEvent event) {
    //set value here.
}

or use getNewValue inside valueChangeListener to get new value not by using bean property.

Example:

JSF Controller:

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.event.AjaxBehaviorEvent;

public class Bean {

    private String choice;

    public String getChoice() {
        return choice;
    }
    public void setChoice(String choice) {
        this.choice = choice;
    }
    private String code;

    private String dest;

    private List<String> list;

    @PostConstruct
    public void init()
    {
        list=new ArrayList<String>();
        list.add("a");
        list.add("b");
    }
    public void setAdress(AjaxBehaviorEvent event) {
        //set value here.
        if (choice.equals("1")) {
            dest = "rererer";
      }
        System.out.println("code: "+code);
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public String getDest() {
        return dest;
    }
    public void setDest(String dest) {
        this.dest = dest;
    }


}

JSF Page:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
    <h:form id="main">


        <table>


            <tr>
                <td colspan="2"><h:selectOneMenu id="select"
                        value="#{bean.code}">
                        <f:selectItems value="#{bean.list}" />
                        <f:ajax listener="#{bean.setAdress}" render="dest"
                            execute="select adressChoice" />
                    </h:selectOneMenu></td>
            </tr>
            <tr>
                <td><h:selectOneRadio id="adressChoice" value="#{bean.choice}">
                        <f:selectItem id="item1" itemLabel="Post adress" itemValue="1" />
                        <f:selectItem id="item2" itemLabel="Other" itemValue="2" />

                    </h:selectOneRadio></td>
            </tr>
            <tr>
                <td><h:inputText id="dest" value="#{bean.dest}" /></td>
            </tr>
        </table>
    </h:form>
</h:body>
</html>

when value changes setAddress will be called.

Safwan Hijazi
  • 2,089
  • 2
  • 17
  • 29
  • I tried it and it still doesn't work... my method setAdress is not even called... I am totally lost here... – Chris Jun 15 '15 at 11:27
  • @Chris try the example I put in the answer – Safwan Hijazi Jun 15 '15 at 11:43
  • yes the method is called :) The thing is my values are never updated. In the line `System.out.println("code: "+code);`, it's the old code that is displayed. I tried to put `execute="select"` but I have the same problem... – Chris Jun 15 '15 at 12:16
  • It's updated i tried on my machine, please try again – Safwan Hijazi Jun 15 '15 at 12:18
  • Thanks Safwan. If I use this exact same code, it's working. But the thing is, in my method `setAdress`, I have a condition on the value of the input `dest`. If I don't put it on the `execute` attribute, `dest` will always be null in the bean. When I put it in the `execute` attribute, its value is changed in the `setAdress` method, but is never updated in my view... – Chris Jun 15 '15 at 12:39
  • @Chris please note that execute attribute evaluates to Collection. It is a space separated list of client ids of components that will participate in the "execute" portion of the Request Processing Lifecycle. so execute value should be "select" not dest – Safwan Hijazi Jun 15 '15 at 12:55
  • and render attribute is the space-separated list of IDs for components that will be updated after an Ajax request. In your case you update the component with id = dest, not the variable in the bean. For that reason dest variable will be always null – Safwan Hijazi Jun 15 '15 at 12:58
  • I admit it's confusing... I changed my code so you can understand better. When I select `post adress` in the `selectOneRadio`, the `dest` input must change when I select an element from the `selectOneMenu` list. If I select an element from this list, and that the choice is set to `other`, then my field `dest` must not change. The thing is, its value is lost if I don't put it in the `execute`. If I put in the `execute`, then its value is never changed, even when `choice = "1"`... I don't know if I made myself clearer... – Chris Jun 15 '15 at 14:00
  • The problem is clear for me, please note that render and execute in ajax don't copy value to bean.dest variable, I will update my example – Safwan Hijazi Jun 15 '15 at 14:06
  • Thanks Safwan. I have a `SelectOneRadio` that lists diffent ways to send a package (personal adress, post office adress), and a `selectOneMenu` that lists the different post offices in the area, and an input text that represents the addressee. When I select a post office from my `selectOneMenu`, if the delivery mode is set to post office adress, then the addressee must change automatically to contain the right value. If the delivery mode is set to personal adress, then the addressee must not change even when I select a post office from my `selectOneMenu` list. – Chris Jun 15 '15 at 14:20
  • Thank you for your answer. I selected `Other` in the `SelectOneRadio` component. Then, I entered `Paul` in the `dest` field. After that, I selected a post office. My `dest` field was emptied, so I lost my value `Paul`. Do you have th same result as mine? – Chris Jun 15 '15 at 14:42
  • remove from SelectOneRadio – Safwan Hijazi Jun 15 '15 at 14:45
  • This is weird beacause I am in debug mode, and when I evaluate `bean.dest` in the `setAdress` method, its value is null. When does the view set the value to `bean.dest`? – Chris Jun 15 '15 at 15:04
  • as you put in condition, when choice equals 1, if the choice never be a 1, so It's value will be null – Safwan Hijazi Jun 15 '15 at 17:29