【问题标题】:Spring how to refresh the page when I received a transaction?Spring收到交易时如何刷新页面?
【发布时间】:2017-11-20 01:56:17
【问题描述】:

我使用 Spring Mvc 应用程序开发比特币钱包,并且我有控制器定义,

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

这会返回index.jsp页面提供相关信息,

直到应用程序没有同步到区块链的那一刻,它会从脚本每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;
        }
    }

因此,意图是在&lt;script&gt; 中编写代码,如果isMoneyReceived 返回true,那么我需要刷新页面。在这种情况下,我可能需要将该方法放在 iteration 中,例如 while 并继续使用 if 条件调用。

controller 中可能有第二个选项可以完全完成它。我曾尝试在&lt;script&gt; 标记内的index.jsp 页面中执行此操作,但没有成功,

<% while(true) {%>

    <% boolean moneyReceived = BitcoinWalletController.isMoneyReceived(); %>

    <% if(moneyReceived) {%>

     // tried to print something .. 
    <% System.out.println("-----------------------"); %>
    <% System.out.println("Hello, Money Received"); %>
    <% System.out.println("-----------------------"); %>
    <% moneyReceived = false; %>
    <% }%>
    <%}%>

我最终得到了错误,

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 重定向页面,那也很好。

【问题讨论】:

    标签: java spring maven jsp spring-mvc


    【解决方案1】:

    您的代码的问题是jsp 页面是在服务器上呈现的。因此,放置一个 while 循环将在很大程度上阻止页面到达客户端浏览器(并被显示)。

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

    这是一个带有 jQ​​uery ajax 获取请求的代码示例:

        $("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 应该是您的方法的接口(例如,通过 servlet、Web 服务等)。 您可以将 $("button").click 替换为超时(例如每 3 秒)。

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

    【讨论】:

    • 我不使用Javascript/ Ajax 进行大量编程。完成实施后,我将接受您的回答。
    猜你喜欢
    • 2013-02-11
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 2021-07-02
    • 2013-03-20
    • 1970-01-01
    • 2016-01-18
    相关资源
    最近更新 更多