简体   繁体   English

在计时器上自动更新 JSP 页面

[英]Auto-update JSP page on timer

I have a monitoring dashboard which runs java spring as the backend and has a jsp front end.我有一个监控仪表板,它运行 java spring 作为后端并有一个 jsp 前端。 Currently I add everything to a ModelAndView including the object which stores all the data and display that in jsp.目前,我将所有内容添加到 ModelAndView 中,包括存储所有数据并在 jsp 中显示的对象。 Setting the @Scheduled does run the method as expected every minute but doesn't output the updated info to the browser.设置 @Scheduled 确实每分钟按预期运行该方法,但不会将更新的信息输出到浏览器。

Is there a way to automatically get this data without having to refresh the page each time?有没有办法自动获取这些数据而不必每次都刷新页面? Code examples of what I'm doing below.下面是我在做什么的代码示例。

Java controller Java控制器

@Scheduled(fixedRate = 60000)
@RequestMapping(method = RequestMethod.GET) // Set this to run every minute and return via @SendTo using the websockets
public ModelAndView getHome() {
    Wrapper wrapper = new Wrapper();
    UATIssues uatIssues = backgroundJobService.runGetCurrentUATIssues();
    wrapper.setUatIssues(uatIssues);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("wrapper", wrapper);
    modelAndView.setViewName("home");
    return modelAndView;
}

Jsp file Jsp文件

<table class="uatIssue">
<c:if test="${not empty uatTicketNumList}">
  <c:forEach items="${uatTicketNumList}" var="uatTicketNumList">
    <tr><td>${uatTicketNumList}</td></tr>
  </c:forEach>
</c:if>

Which browser/client would you like your @Scheduled method to return to?您希望@Scheduled方法返回到哪个浏览器/客户端?

This will NOT work this way.这不会以这种方式工作。 @Scheduled annotation is used to do some operations/jobs on the server side . @Scheduled注解用于在服务器端做一些操作/作业。 It has no idea about the requester (your browser - client side).它不知道请求者(您的浏览器 - 客户端)。

In order to have the fresh data, you need to either:为了有fresh数据,您需要:

1) Call your Controller 's endpoint from your JSP page on a time basis, ie every 2 minutes and then parse the data ( AJAX ). 1)按时间从您的 JSP 页面调用您的Controller的端点,即每 2 分钟一次,然后解析数据( AJAX )。 Example: A Simple AJAX with JSP example示例: 带有 JSP 的简单 AJAX 示例

or或者

2) Implement web sockets and when the changes occur your frontend will be informed immediately. 2) 实施网络套接字,当发生变化时,您的前端将立即收到通知。 More: https://spring.io/guides/gs/messaging-stomp-websocket/更多: https : //spring.io/guides/gs/messaging-stomp-websocket/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM