How to reload a JSF page from a ValueChangeEvent?

前端 未结 2 660
耶瑟儿~
耶瑟儿~ 2021-01-02 22:40

I have a selectonemenu where a change in the selection should navigate the user to the related page.

So, how do I simulate the action handling of a commandbutton usi

相关标签:
2条回答
  • 2021-01-02 23:09

    You can't go around a shot of Javascript for this. Basically you need to let the Javascript submit the request to the server side. In a HTML <select> element (which is been generated by the JSF h:selectOneMenu) you can best use the onchange attribute for this. Any JS which you attach to this event will be invoked whenever the user changes the value of the element.

    <h:selectOneMenu onchange="this.form.submit();">
    

    or if you're lazy in writing, this shorthand is also correct:

    <h:selectOneMenu onchange="submit()">
    

    This will submit the form in combination with the firstnext HTML input type="submit" element inside the same form (which is been generated by the JSF h:commandButton).

    <h:form>
        <h:selectOneMenu value="#{bean.page}" onchange="submit()" required="true">
            <f:selectItem itemLabel="Select page.." />
            <f:selectItems value="#{bean.pages}" />
        </h:selectOneMenu>
        <h:commandButton value="submit" action="#{bean.submit}" />
        <h:commandButton value="other" action="#{bean.other}" /> <!-- won't be submitted -->
    </h:form>
    

    You need to write logic in the action method which causes the navigation action as definied in faces-config.xml to be taken place. Example:

    public String submit() {
        return this.page;
    }
    

    If you do not want to use an commandButton, then you can also go ahead with abusing the valueChangeListener:

    <h:form>
        <h:selectOneMenu value="#{bean.page}" onchange="submit()"
            valueChangeListener="#{bean.change}" required="true">
            <f:selectItem itemLabel="Select page.." />
            <f:selectItems value="#{bean.pages}" />
        </h:selectOneMenu>
    </h:form>
    

    In the bean you then have:

    public void change(ValueChangeEvent event) {
        String page = (String) event.getNewValue(); // Must however be the exact page URL. E.g. "contact.jsf".
        FacesContext.getCurrentInstance().getExternalContext().redirect(page);
    }
    

    Alternatively, if you already have the desired URL's as f:selectItem values, then you can also go ahead with just only JS and no bean actions:

    <h:selectOneMenu value="#{bean.page}"
        onchange="window.location = this.options[this.selectedIndex].value">
        <f:selectItem itemLabel="Select page.." />
        <f:selectItem itemLabel="home" itemValue="home.jsf" />
        <f:selectItem itemLabel="faq" itemValue="faq.jsf" />
        <f:selectItem itemLabel="contact" itemValue="contact.jsf" />
    </h:selectOneMenu>
    
    0 讨论(0)
  • 2021-01-02 23:21
    <h:selectOneMenu onchange="document.getElementById('myform').submit();" ...>
    
    0 讨论(0)
提交回复
热议问题