weixin_33699914 2017-01-20 23:17 采纳率: 0%
浏览 65

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

(这个问题已得到解答: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>

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

  • 写回答

1条回答 默认 最新

  • weixin_33743703 2017-01-20 23:32
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题