Form submit vs ajax execute = "@form"

I am using JSF2.1.

What is the difference between execute="@form"and this.submit()in the code below?

<f:ajax execute="@form" render="@form"/>

and

<h:form onkeypress="if (event.keyCode == 13) this.submit();">

The former seems to pass values โ€‹โ€‹and re-display the form, while the latter causes the page to refresh. Is there a way to use ajax when the enter key is pressed on a form? I am trying to detect that an input key is pressed in a field inputText. I tried things like this:

<h:inputText value="#{a.name}" >
  <f:ajax execute="@this" />
</h:inputText>

but it just leads to the fact that the values โ€‹โ€‹will be sent when you click on something else (after valueChange).

+4
source share
1 answer

, , , ajax , ajax, , .

, , Enter, . javascripting onkeypress, .

, , "", Enter: value. , , <h:inputText /> ajax, h:outputText ( render) Enter, - ajax, . Google Chrome, , : httpError: Http Transport 0. ajax .

:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">
<h:head />
<body>
    <h:form>
        <h:inputText value="#{bean.value}" />
        <h:commandButton value="Send" />
    </h:form>
</body>
</html>

Ajax:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">
<h:head />
<body>
    <h:form>
        <h:inputText value="#{bean.value}">
            <f:ajax />
        </h:inputText>
        <h:outputText value="Echo: #{bean.value}" />
    </h:form>
</body>
</html>

, โ€‹โ€‹ javascript , , . , .

, ajax- , onkeypress:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">
<h:head />
<body>
    <h:form>
        <h:inputText value="#{bean.value}"
            onkeypress="if (event.keyCode == 13) {onchange(); return false; }">
            <f:ajax render="echo" />
        </h:inputText>
        <h:inputText value="#{bean.value2}" />
        <h:outputText id="echo" value="Echo: #{bean.value}" />
    </h:form>
</body>
</html>

, .

. :

+3

Source: https://habr.com/ru/post/1524636/


All Articles