Java selectOneRadio 单击后立即执行操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/480975/
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-08-11 15:12:26  来源:igfitidea点击:

selectOneRadio with action immediately after click

javajsfseam

提问by

This should be easy for a pro:

这对于专业人士来说应该很容易:

I am using JSF/Facelets/Seam and am trying to display radiobuttons. Then, after the user has clicked on one of the buttons, the value should be saved and the user redirected to another page immediately (i.e. with no needed click on a submit button).

我正在使用 JSF/Facelets/Seam 并试图显示单选按钮。然后,在用户单击其中一个按钮后,应保存该值并且用户立即重定向到另一个页面(即无需单击提交按钮)。

The radio button works, but not the forwarding.

单选按钮有效,但转发无效。

Thanks

谢谢

回答by Romain Linsolas

You can indeed use Richfaces to add an a4j:support component:

您确实可以使用 Richfaces 添加a4j:support 组件

<h:selectOneRadio value="#{myBean.myValue}" ...>
    ...
    <a4j:support event="onclick" action="#{myBean.doSomething}"/>
</h:selectOneRadio>

In your Java code:

在您的 Java 代码中:

public String doSomething() {
    // Your code goes here...
    ...
    // Now, we move to the new page.
    return "some-outcome";
}

However, if you cannot (or do not want) to add a new library, you can do this by the old way:

但是,如果您不能(或不想)添加新库,则可以通过旧方式执行此操作:

<h:selectOneRadio value="#{myBean.myValue}" ... onclick="this.form.submit();" valueChangeListener="#{myBean.doSomething}">
    ...
</h:selectOneRadio>

This code will submit the form where the radio button are contained when the onclick Javascript event is detected. On the server side, the action doSomething will be executed. In this method, you can make a navigation rule to be executed:

当检测到 onclick Javascript 事件时,此代码将提交包含单选按钮的表单。在服务器端,将执行操作 doSomething。在此方法中,您可以制定要执行的导航规则:

public void doSomething(ValueChangeEvent evt) {
    // Your code goes here...
    ...
    // Now, we move to another page...
    FacesContext context = FacesContext.getCurrentInstance();
    NavigationHandler navigation = context.getApplication().getNavigationHandler();
    navigation.handleNavigation(context, "", "some-outcome");
}

where some-outcome is an outcome defined in a navigation rule in your faces-config.xml.

其中 some-outcome 是在 faces-config.xml 中的导航规则中定义的结果。

回答by Oscar Castiblanco

With JSF 2.0 you can do the following

使用 JSF 2.0,您可以执行以下操作

<h:selectOneRadio ...
...
    <f:ajax event="change" render="@form" />
</h:selectOneRadio>