我使用 Spring Mvc 应用程序开发比特币钱包,并且我有 Controller 定义,
@RequestMapping(value = "/")
public String showBitcoinWallet() {
return "index";
}
这将返回 index.jsp
页面提供相关信息,
直到应用程序未同步到区 block 链的那一刻,它将从脚本中每 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
中完全完成此操作。 。我已经尝试在 index.jsp
中做到这一点页面内 <script>
标记没有成功,
<% 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
重定向页面,那也很好。
最佳答案
您的代码的问题是 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 应该是您的方法的接口(interface)(例如通过 servlet、Web 服务等)。 您可以将 $("button").click 替换为超时(例如每 3 秒)。
然后您可以使用脚本对其进行处理并相应地设置应用程序逻辑。
关于java - Spring如何在收到交易时刷新页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44603641/