2017-06-17 54 views
0

我和春天MVC应用程序一起制定一个比特币钱包和我有控制的定义,春天如何在我收到交易时刷新页面?

@RequestMapping(value = "/") 
    public String showBitcoinWallet() {   
     return "index"; 
    } 

这将返回index.jsp页面提供有关的相关信息,

enter image description here

直到此刻应用程序未同步到区块链,它将在每3000毫秒从脚本中刷新,

<html> 
<body> 

    <!- some code -> 

    <!- some code -> 

</body> 

<script> 
    <% if(!model.isSyncFinished()) {%> 
    setTimeout(function() { 
     window.location.reload(1); 
    }, 3000); 
    <% }%> 


</script> 
</html> 

对于发送操作,弹出窗口打开并且用户执行提交。此操作刷新页面并更新信息(例如余额,地址等)。在接收实例中,页面不刷新,只有在手动刷新时才更新。

I need to refresh the page after the user received the money. 

我有返回接收执行操作的boolean的方法,

public static boolean isMoneyReceived() { 

     try { 
      WalletMain.bitcoin.wallet().addEventListener(new AbstractWalletEventListener() { 

       @Override 
       public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) { 
        // Runs in the dedicated "user thread". 
        // 
        // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast). 
        Coin value = tx.getValueSentToMe(w); 

//     System.out.println("Received tx for " + value.toFriendlyString() + ": " + tx); 
//     System.out.println("Transaction will be forwarded after it confirms."); 
       } 
      }); 
      return true; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      return false; 
     } 
    } 

所以,内涵会写代码中<script>,如果isMoneyReceived回报true,然后,我将需要刷新页面。在这种情况下,我可能需要将方法放入iteration,例如while,并继续使用if条件调用。

可能有第二个选项完成controller。我试图做到这一点在<script>标签没有成功里面index.jsp页面,

我最终得到了错误,

HTTP Status [500] – [Internal Server Error] 

Type Exception Report 

Message java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.jsps.index_jsp 

Description The server encountered an unexpected condition that prevented it from fulfilling the request. 

Exception 

org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.jsps.index_jsp 
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:176) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:380) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 

如何解决这个问题?如前所述,如果我可以从Spring controller重定向页面,那也可以。

回答

1

您的代码的问题是,jsp页面呈现在服务器上。因此,放置一个while循环几乎可以防止页面进入客户端浏览器(并被显示)。

因此,我建议的方式是使用AJAX调用isMoneyReceived()方法,然后使用脚本检查返回值。

这是与jQuery AJAX get请求一个代码示例:

$("button").click(function(){ 
    $.get("yourTargetInterface", function(data, status){ 
     //your processing code here, with the variable "data" being the response to your request; in this case true or false 
    }); 
}); 

yourTargetInterface应的接口的方法(例如,通过小服务程序,web服务等)。 您可以用超时(例如每3秒)替换$(“按钮”)。点击。

然后您可以使用脚本处理它并相应地设置应用程序逻辑。

+0

我不用'Javascript/Ajax'编写很多程序。在完成实施后,我会接受你的回答。 – Arefe