JSF 2:在提交之前的表单中访问selectOneMenu的选定值

时间:2012-09-06 10:30:26

标签: ajax jsf jsf-2 primefaces

查看

<h:form id="main_form">
    <p:inputText id="title" required="true" label="Title" value="#{myBean.myLink.title}" immediate="true" />

    <p:selectOneMenu id="scope" required="true" label="Scope" value="#{myBean.myLink.scope}" immediate="true" >
        <f:selectItem itemLabel="Please choose" itemValue="" />
        <f:selectItems value="#{myBean.availableScopes}" id="selScope"/>
    </p:selectOneMenu>

    <p:inputText id="link" required="true" label="URL" value="#{myBean.myLink.link}" immediate="true">      
        <p:ajax event="blur" update="msgLink" listener="#{myBean.checkUrl}" />
    </p:inputText>

    ... msgLink and other (required) elements

    ... submit button
</h:form>

托管Bean

@Component("myBean")
@Scope("session")
public class MyBean implements Serializable {

    private Link myLink;
    private Map<String, String> availableScopes;

    public MyBean() {
        this.availableScopes = new HashMap<String, String>();
        this.availableScopes.put("Intranet", "Intranet");
        this.availableScopes.put("Internet", "Internet");   
    }

    // setter/getters etc.

    public void checkUrl() {
        System.out.println(myLink.getTitle());  // works
        System.out.println(myLink.getScope());  // DOES NOT work
        System.out.println(myLink.getLink());   // works
    }

}
  • 我想根据所选范围检查网址 提交表单。但被调用的方法可以访问对象的inputText值。不是selectOneMenu中选择的值。
  • 我用getExternalContext().getSessionMap().get("scope")尝试过,但当时SessionMap为空。

是否有机会访问组合框的选定值?

由于 吉姆

1 个答案:

答案 0 :(得分:4)

<p:ajax>组件中的<f:ajax>(和UIInput)默认执行/处理当前 UIInput组件({{1} }),而不是其他人。

如果要在调用侦听器方法时执行/处理所有@this组件,则应在UIInput(或<p:ajax process>)属性中指定:

<f:ajax execute>

对具体问题

无关,我想知道所有这些<p:inputText id="title" ... /> <p:selectOneMenu id="scope" ... > ... </p:selectOneMenu> <p:inputText id="link" ...> <p:ajax process="title scope link" ... /> </p:inputText> 属性在这种情况下是如何有用的。你确定你需要它们吗?