查看未更新

时间:2015-06-12 12:41:50

标签: jsf

我有这段代码:

<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}" />

在我的bean中,我有这个方法:

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

我的问题是,即使调用方法setAdress,我的inputText也不会改变。为什么?是因为execute属性吗?

感谢。

1 个答案:

答案 0 :(得分:0)

valueChangeListener在验证之后和更新模型之前调用(在Jsf生命周期中),因此值不会更改 使用:

<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.
}

或使用valueChangeListener中的getNewValue来获取新值,而不是使用bean属性。

示例:

JSF控制器:

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页面:

    <!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>

当值改变时,将调用setAddress。