的commandButton这是用f启用:当表单提交的ajax没有调用操作 [英] commandButton which is enabled by f:ajax does not invoke action when form is submitted

查看:126
本文介绍了的commandButton这是用f启用:当表单提交的ajax没有调用操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个命令启用/使用复选框禁用。该命令最初是被禁用。当用户选中的复选框,则命令变为启用。但它没有响应,当单击该按钮。

如果我做出独立于复选框的命令,它工作正常。但随着复选框,我得到了我上面提到的问题。请帮我

下面是codeS。

的index.xhtml

 < H:形式GT;
  < H:selectBooleanCheckbox值=#{formSettings.licenseAccepted}ID =CB>
    < F:Ajax事件=点击渲染=分CB/>
  < / H:selectBooleanCheckbox>
  < H:的outputText值=#{formSettings.msg}/>< BR />

  < H:的commandButton ID =亚健康已禁用=假值=保存行动=loginSuccessfull/>
< /小时:形式GT;
 

FormSettings.java

 包中的类;
进口javax.faces.bean.ManagedBean;
进口javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
公共类FormSettings
{
    私人布尔licenseAccepted = FALSE;
    私人弦乐味精=接受许可证;

    公共FormSettings(){};

    公共布尔isLicenseAccepted(){返回this.licenseAccepted;};
    公共无效setLicenseAccepted(布尔licenseAccepted){this.licenseAccepted = licenseAccepted;};
    公众的getMsg字符串(){返回this.msg;};
    公共无效setMsg(弦乐味精){this.msg =味精;};
}
 

faces-config.xml中

 < XML版本=1.0编码=UTF-8&GT?;

<脸,配置
    的xmlns =htt​​p://java.sun.com/xml/ns/javaee
    的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance
    XSI:的schemaLocation =htt​​p://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd
    版本=2.0>
    <导航规则>
        &LT;从-view-id的&GT; /index.xhtml< /从-view-id的&GT;
        &LT;导航情况&GT;
            &LT;从-结果&GT; loginSuccessfull&LT; /从-结果&GT;
            &LT;到视图ID&GT; /login.xhtml< /浏览的-ID&GT;
        &LT; /导航案例&GT;
    &LT; /导航规则&GT;

&LT; /面,配置&GT;
 

解决方案

这个bean必须放置在视图范围,以得到它的工作。

  @ManagedBean
@ViewScoped
公共类FormSettings {}
 

阿贾克斯计为一个HTTP请求启用按钮。用按钮提交表单之后算作另一个HTTP请求。当你的bean是请求范围,它得到新鲜的每个HTTP请求创建和请求结束garbaged。由于布尔属性默认为,该按钮得到有效地再次被禁用时,JSF将要处理表单上的第二个请求提交。

参见:

  • <一个href="http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope/7031941#7031941">How要选择合适的豆范围有多大?

I am trying to make a commandButton enabled/disabled by using a checkbox. The commandbutton is disabled initially. When user checks the checkbox, the commandbutton turns into enabled. But it does not response when clicked the button.

If I make the commandbutton independent from checkbox, it works fine. But with checkbox, I get the problem that I mentioned above. Please help me

Here are codes.

index.xhtml

<h:form>        
  <h:selectBooleanCheckbox value="#{formSettings.licenseAccepted}" id="cb">
    <f:ajax event="click" render="suB cb"/>
  </h:selectBooleanCheckbox>
  <h:outputText value="#{formSettings.msg}"/><br/>

  <h:commandButton id="suB" disabled="false" value="Save" action="loginSuccessfull"/>
</h:form>

FormSettings.java

package classes;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class FormSettings 
{
    private boolean licenseAccepted = false;
    private String msg = "Accept License";

    public FormSettings(){};

    public boolean isLicenseAccepted(){return this.licenseAccepted;};
    public void setLicenseAccepted(boolean licenseAccepted){this.licenseAccepted = licenseAccepted;};
    public String getMsg(){return this.msg;};
    public void setMsg(String msg){this.msg = msg;};
}

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
    <navigation-rule>
        <from-view-id>/index.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>loginSuccessfull</from-outcome>
            <to-view-id>/login.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>

</faces-config>

解决方案

The bean has to be placed in the view scope in order to get it to work.

@ManagedBean
@ViewScoped
public class FormSettings {}

Enabling the button by ajax counts as one HTTP request. Submitting the form with the button thereafter counts as another HTTP request. As your bean is request scoped, it get freshly created on every HTTP request and garbaged by the end of request. As the boolean property defaults to false, the button get effectively disabled again when JSF is about to process the form submit on the second request.

See also:

这篇关于的commandButton这是用f启用:当表单提交的ajax没有调用操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆