user2047366
user2047366

Reputation: 93

Auto-update JSP page on timer

I have a monitoring dashboard which runs java spring as the backend and has a jsp front end. Currently I add everything to a ModelAndView including the object which stores all the data and display that in jsp. Setting the @Scheduled does run the method as expected every minute but doesn't output the updated info to the browser.

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

@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

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

Upvotes: 1

Views: 382

Answers (1)

Pijotrek
Pijotrek

Reputation: 3021

Which browser/client would you like your @Scheduled method to return to?

This will NOT work this way. @Scheduled annotation is used to do some operations/jobs on the server side. It has no idea about the requester (your browser - client side).

In order to have the fresh data, you need to either:

1) Call your Controller's endpoint from your JSP page on a time basis, i.e. every 2 minutes and then parse the data (AJAX). Example: A Simple AJAX with JSP example

or

2) Implement web sockets and when the changes occur your frontend will be informed immediately. More: https://spring.io/guides/gs/messaging-stomp-websocket/

Upvotes: 1

Related Questions