Bootsfaces inputText因使用ajax和更新bean中的值而失去焦点

时间:2016-05-08 15:55:20

标签: jsf primefaces focus bootsfaces ajax-update

我在使用带有ajax的bootsfaces inputText时发现了一个问题。 我使用的是JSF 2.2,Bootsfaces 0.8.1和Primefaces 5.3。

我试图在inputText字段中输入日期值。只要我输入日期的最后一个值,inputText就应该触发change事件。此时我想使用ajax来调用bean方法。问题是,一旦我尝试输入最后一个值并且方法从未被调用过,我的字段就会失去焦点。

所以我尝试使用Primefaces,它几乎像我想要的那样工作。在这一点上,我有不同的问题:

  1. 为什么输入最后一个值时我的inputText字段会失去焦点? (的 Bootsfaces
  2. 为什么在失去焦点后才会调用bean方法? (的 Bootsfaces
  3. 是否可以在字段设置bean值后调用bean方法? (的 Primefaces
  4. 我添加了以下代码,因此您可以重现此行为。

    test.xhtml - 带有primefaces和bootsfaces字段的示例xhtml

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:b="http://bootsfaces.net/ui"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:p="http://primefaces.org/ui">
    
        <h:head>
            <meta charset="UTF-8"/>
        </h:head>
    
        <h:body>
            <h:form id="form">
                <b:panel id="filterPanel" title="Filter properties" immediate="true" collapsed="false" collapsible="true">
                    <b:row>
                        <b:column span="12">
                            <b:inputText id="dateA" type="date" value="#{test.dateA}" immediate="true" class="form-control">
                                <f:ajax event="change" listener="#{test.searchA()}"/>
                            </b:inputText>
                        </b:column>
                    </b:row>
                    <b:row>
                        <b:column span="12">
                            <p:inputText id="dateB" type="date" value="#{test.dateB}" immediate="true" class="form-control">
                                <p:ajax event="change" listener="#{test.searchB()}"/>
                            </p:inputText>
                        </b:column>
                    </b:row>
                </b:panel>
            </h:form>
        </h:body>
    </html>
    

    TestBean.java - 用于设置值和调用方法的bean

    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ViewScoped;
    
    @ManagedBean(name = "test")
    @ViewScoped
    public class TestBean {
    
        private String dateA;
        private String dateB;
    
        public void searchA() {
            System.out.println("Search A");
        }
    
        public void searchB() {
            System.out.println("Search B");
        }
    
        public String getDateA() {
            return dateA;
        }
    
        public void setDateA(String dateA) {
            this.dateA = dateA;
            System.out.println(dateA);
        }
    
        public String getDateB() {
            return dateB;
        }
    
        public void setDateB(String dateB) {
            this.dateB = dateB;
            System.out.println(dateB);
        }
    
    }
    

    请帮我找一个解决方案,或者解释一下我在这里做错了什么。

    由于 mweber

1 个答案:

答案 0 :(得分:2)

你在BootsFaces和PrimeFaces之间找到了一个微妙的区别。为了清楚起见,我建议您始终定义processupdate的值。在你的情况下,

    <b:inputText id="dateA" type="date" value="#{test.dateA}" immediate="true" class="form-control">
        <f:ajax event="change" listener="#{test.searchA()}" render="@none"/>
    </b:inputText>

使BootsFaces输入字段的行为与PrimeFaces对应字段完全相同。

updateprocess的默认值不同。由于BootsFaces为0.8.5,默认值为:

  • 过程=&#34; @形式&#34; <b:commandButton /><b:commandLink />
  • 过程=&#34; @这&#34;对于其他所有BootsFaces组件
  • 更新=&#34; @形式&#34;对于每个BootsFaces组件

根据Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes,PrimeFaces的默认值为: enter image description here

为方便起见,这是我的XHTML文件版本:         
        http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"                        的xmlns:B =&#34; HTTP://bootsfaces.net/ui"
              的xmlns:H =&#34; HTTP://java.sun.com/jsf/html"
              的xmlns:F =&#34; HTTP://java.sun.com/jsf/core"
              的xmlns:P =&#34; HTTP://primefaces.org/ui"&GT;

        <h:head>                                                                                                            
            <meta charset="UTF-8"/>                                                                                         
        </h:head>                                                                                                           

        <h:body>                                                                                                            
            <h:form id="form">                                                                                              
                <b:panel id="filterPanel" title="Filter properties" immediate="true" collapsed="false" collapsible="true">  
                    <b:row>                                                                                                 
                        <b:column span="12">                                                                                
                            <b:inputText id="dateA" type="date" value="#{test.dateA}" immediate="true" class="form-control">
                                <f:ajax event="change" listener="#{test.searchA()}" render="@none"/>                        
                            </b:inputText>                                                                                  
                        </b:column>                                                                                         
                    </b:row>                                                                                                
                    <b:row>                                                                                                 
                        <b:column span="12">                                                                                
                            <p:inputText id="dateB" type="date" value="#{test.dateB}" immediate="true" class="form-control">
                                <p:ajax event="change" listener="#{test.searchB()}"/>                                       
                            </p:inputText>                                                                                  
                        </b:column>                                                                                         
                    </b:row>                                                                                                
                </b:panel>                                                                                                  
            </h:form>                                                                                                       
        </h:body>                                                                                                           
    </html>                                                                                                                 

我已经用BootsFaces 0.8.5测试了它。