Arefe
Arefe

Reputation: 12441

Spring how to refresh the page when I received a transaction?

I work with Spring Mvc app to develop a bitcoin wallet and I have the controller definition,

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

This returns the index.jsp page provides the relevant infos,

enter image description here

Till the moment the app is not synchronized to the blockchain, it will refresh in every 3000 ms from the script,

<html>
<body>

     <!- some code ->

     <!- some code ->

</body>

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


</script>
</html>

For the sending operation, a pop-up opens and the user execute the submission. This operation refresh the page and updates the info(e.g balance, address etc). In the instance of receiving, the page is not refreshed and only updates if I manually refresh.

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

I have a method that returns boolean of the receive execution operation,

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;
        }
    }

So, the intension will be to write code the in the <script> that if the isMoneyReceived returns true, then, I would need to refresh the page. In that case, I may need to put the method in an iteration say, while and keep on calling with an if condition.

There might be 2nd option to have done it completely in the controller. I have tried to do it in the index.jsp page inside the <script> tag with no success,

<% 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; %>
    <% }%>
    <%}%>

I ended up getting the error,

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)

How to solve the problem? As mentioned earlier, if i can redirect the page from the Spring controller, that would be fine as well.

Upvotes: 0

Views: 5174

Answers (1)

JovanToroman
JovanToroman

Reputation: 705

The problem with your code is that jsp pages are rendered on the server. So, putting a while loop will pretty much prevent the page from ever getting to the clients browser (and being displayed).

Therefore, the way I suggest is to use an AJAX call to the isMoneyReceived() method and then check the return value using a script.

This is a code sample with jQuery ajax get request:

    $("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 should be the interface to your method (e.g. through a servlet, web service, etc.). You can replace $("button").click with a timeout (e.g. every 3 secs).

Then you can process it with a script and setup the application logic accordingly.

Upvotes: 1

Related Questions