为什么在单击按钮之前就显示字符串?

(这个问题已得到解答:How make commandButton not fully refresh page? How to use f:ajax?

我在JSF中使用Ajax遇到了一些问题。我有返回字符串的方法,并且只想在单击按钮时打印此字符串。这个方法使用两个参数,我想用2个inputBox传递给这个方法。这里是我到目前为止提供的代码,但是它还没有完成:

<h:commandButton id="submit" value="Submit"> 
            <f:ajax event="click" />
        </h:commandButton>
        <h:outputText id="result" value="#{cSVobjectCtrl.getJson(48.866667, 2.33333)}" />

这段代码的问题是在单击按钮之前直接得到字符串。我应该更改什么来使代码按照我想要的方式工作?

更新 1 :

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>JsonValue</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
       <script>
         var show = function(){
         document.getElementById('result').style.display = 'block';
        }
        </script>
    </h:head>

    <h:form>


        <h:commandButton id="submit" value="Submit" onclick="show();" > 
     <f:ajax event="click" render="result"/>
</h:commandButton>
<h:outputText id="result" value="#{cSVobjectCtrl.getJson(48.866667, 2.33333)}" 
              style="display:'none'"/>

    </h:form>


</html>

但是问题仍然存在:在单击按钮之前就显示字符串。

This is the solution that have worked for me:

JSF Page

<h:commandButton id="submit" value="Submit">
        <f:ajax event="click" render="resultGroup" listener="#{cSVobjectCtrl.doRender}"/>
</h:commandButton>

<h:panelGroup id = "resultGroup" layout="block" >
    <h:panelGroup rendered="#{cSVobjectCtrl.rendered}">
         <h:outputText id="result" value="#{cSVobjectCtrl.getJson(48.866667, 2.33333)}}" />
    </h:panelGroup>
</h:panelGroup>

Backing Bean

@ManagedBean
@ViewScoped // this is important
public class CSVobjectCtrl{

   private boolean rendered = false;

   public boolean isRendered() {
        return rendered;
    }

    public void setRendered(boolean rendered) {
        this.rendered = rendered;
    }

    public void doRender(AjaxBehaviorEvent event){
        rendered = true;
    }
}

On the side, in an ajax event you have to explicitly specify which component to refresh / render (if you wont nothing is):

render - Evaluates to Collection. The clientIds of components that will participate in the "render" portion of the Request Processing Lifecycle. If a literal is specified the identifiers must be space delimited. Any of the keywords "@this", "@form", "@all", "@none" may be specified in the identifier list. If not specified, the default value of "@none" is assumed. For example, @this clientIdOne clientIdTwo.