【问题标题】:How to Control JSF form submitting? Primefaces commandButton如何控制 JSF 表单提交? Primefaces 命令按钮
【发布时间】:2012-08-22 17:45:26
【问题描述】:

我有以下表单,我想只使用我的 p:commandButton 提交,但是,我在所有输入框中都收到了提交,如何仅在 p:commandButton 上提交表单?

<h:form>
            <p:panel header="Lista de Referências" id="panel">
                <h:panelGrid columns="2" cellpadding="5">

                    <h:outputLabel>Referência</h:outputLabel>
                    <p:inputText value="#{entradaProdutoController.referencia}" 
                                 onkeypress="if (event.keyCode == 13) {onchange(); return false; }"> 
                        <f:ajax event="change" 
                                render="textDescri" 
                                listener="#{entradaProdutoController.listener}"/> 
                    </p:inputText>

                    <h:outputLabel>Descrição</h:outputLabel>
                    <h:outputText id="textDescri" value="#{entradaProdutoController.replyWith}" /> 

                    <h:outputLabel>Quantidade</h:outputLabel>
                    <p:inputText size="5" value="#{entradaProdutoController.quantidade}" />

                </h:panelGrid>
            </p:panel>
            <p:commandButton value="Adicionar" update="printTable" actionListener="#{entradaProdutoController.addAction(event)}">
            </p:commandButton>

【问题讨论】:

    标签: java jsf jakarta-ee


    【解决方案1】:

    我相信onchange() 方法会导致在按下回车时提交页面,如果您不希望这种行为尝试删除...

    onkeypress="if (event.keyCode == 13) {onchange(); return false; }"
    

    【讨论】:

    • 如果我删除它,我无法进行产品查找,这是使用该代码背后的全部想法,但是我不想仅将整个表单提交到该字段
    【解决方案2】:

    厌倦了上述建议而没有成功的结果,所以作为一种解决方法,这就是我正在做的,这可能不是最佳实践,但它有效(这样做的锤击方式):

    <?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:h="http://java.sun.com/jsf/html"
          xmlns:p="http://primefaces.org/ui"
          xmlns:f="http://java.sun.com/jsf/core"
          >
        <h:head>
            <title>Facelet Title</title>
        </h:head>
        <h:body>
            <h1>Adicionar Referências</h1>
            <h:form id="formID" onkeypress="if (event.keyCode == '13') {return false;}">
                <h:outputLabel>Referência</h:outputLabel>
                <h:inputText  id="referenciaLoookup" value="#{testController.referencia}"
                              onkeypress="if (event.keyCode == '13') {document.getElementById('formID:testeButton').click()}"
                              > 
                </h:inputText>
    
                <h:inputText value="#{testController.referencia}" 
                             onkeypress="if (event.keyCode == 13) { onchange(); return false; }">
                    <f:ajax event="change" listener="#{testController.teste(event)}" />
                </h:inputText>
    
    
                <p:commandButton id="lookup" value="lookup"
                                 actionListener="#{testController.listener(a)}"/>
    
                <p:commandButton id="adicionar" value="Adicionar" 
                                 actionListener="#{testController.addAction(e)}">
                </p:commandButton>
    
                <p:commandButton id="testeButton" value="teste" style="visibility: hidden;"
                                 actionListener="#{testController.teste(event)}"/>
            </h:form>
        </h:body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2017-10-04
      • 2013-06-08
      • 1970-01-01
      • 2016-09-19
      • 1970-01-01
      • 2013-10-24
      • 2012-10-20
      • 1970-01-01
      相关资源
      最近更新 更多