java JSF如何通过selectOneRadio的onchange提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11761068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:16:17  来源:igfitidea点击:

JSF how to submit a form via onchange of a selectOneRadio

javajsfjsf-2primefaces

提问by Catfish

I'm trying to submit a form when a selectOneRadio value is selected. I have the code below, but it's not getting inside my #{contentEditorBacking.addNewsArticle}.

我正在尝试在选择 selectOneRadio 值时提交表单。我有下面的代码,但它没有进入我的#{contentEditorBacking.addNewsArticle}.

Does anyone know how i can get into the method when a selectOneRadio is clicked?

有谁知道单击 selectOneRadio 时如何进入该方法?

<p:panel header="News Article Title">
    <h:panelGrid columns="2">
        <h:outputLabel for="title" value="Title" style="font-weight: bold;"/>
        <p:inputText id="title" required="true" value="#{contentEditorBacking.newsTitle}" />
        <h:outputLabel value="Site" />
        <p:selectOneRadio value="#{contentEditorBacking.selectedNews}" layout="pageDirection" onchange="submit()">
            <f:selectItem itemLabel="Public" itemValue="Public" />
            <f:selectItem itemLabel="Member" itemValue="Member" />
        </p:selectOneRadio>
        <p:commandButton value="submit" action="#{contentEditorBacking.addNewsArticle}" />
    </h:panelGrid>
</p:panel>

回答by BalusC

Two problems:

两个问题:

  • To intercept on "change" in radio buttons (and checkboxes) You need the clickevent instead.
  • The <p:commandButton>is by default an ajax button which isn't prepared for synchronous submits like as performed by JS submit().
  • 要拦截单选按钮(和复选框)中的“更改”,您需要该click事件。
  • <p:commandButton>默认情况下一个Ajax按钮未同步的提交等作为由JS进行制备submit()

Just use <p:ajax>instead so that the submit is performed by ajax. Note that you don't need to specify the eventattribute, it defaults to clickalready.

只需使用,<p:ajax>以便由 ajax 执行提交。请注意,您不需要指定event属性,它默认为click已经。

<p:selectOneRadio value="#{contentEditorBacking.selectedNews}" layout="pageDirection">
    <f:selectItem itemLabel="Public" itemValue="Public" />
    <f:selectItem itemLabel="Member" itemValue="Member" />
    <p:ajax listener="#{contentEditorBacking.addNewsArticle}" />
</p:selectOneRadio>
<p:commandButton value="submit" action="#{contentEditorBacking.addNewsArticle}" />


Updateas per the comments, you seem to want to navigate to another page with a parameter. The addNewsArticle()method should then look like as follows so that it is compatible with both <p:ajax listener>and <p:commandButton action>.

根据评论更新,您似乎想使用参数导航到另一个页面。该addNewsArticle()方法应如下所示,以便它与<p:ajax listener>和兼容<p:commandButton action>

public void addNewsArticle() throws IOException {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.redirect(ec.getRequestContextPath() + "/next.xhtml?action=add");
}