簡體   English   中英

由f:ajax啟用的commandButton在提交表單時不會調用操作

[英]commandButton which is enabled by f:ajax does not invoke action when form is submitted

我試圖通過使用復選框來啟用/禁用commandButton。 最初禁用命令按鈕。 當用戶選中該復選框時,命令按鈕將變為啟用狀態。 但是單擊按鈕時它沒有響應。

如果我使命令按鈕獨立於復選框,則可以正常工作。 但是使用復選框,我遇到了上面提到的問題。 請幫我

這是代碼。

的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>

Bean必須放置在視圖范圍中才能使其正常工作。

@ManagedBean
@ViewScoped
public class FormSettings {}

通過ajax啟用按鈕被視為一個HTTP請求。 此后使用按鈕提交表單將被視為另一個HTTP請求。 當您的bean處於請求范圍內時,它會在每個HTTP請求上重新創建,並在請求結束時進行垃圾回收。 由於boolean屬性默認為false ,因此當JSF將要處理第二個請求的表單提交時,將再次有效禁用按鈕。

也可以看看:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM