简体   繁体   中英

Asynch http requests from spring mvc controller to update div contents?

I'm trying to figure out the best way to re-factor the following into an http asynch GET scenario where multiple http GETs are processed in parallel and then either 1) wait for the http responses to return and pass respective response data into the view or 2) return the view immediately and somehow update the divs in my view asynchronously from my controller or callback once the responses from each http GET are received.

This is what I currently have in it's current state as synchronously processed.

Spring MVC Controller (current): MyController.java

// Make multiple http calls...get the values from each call and pass into the view
@RequestMapping(value="", method = RequestMethod.GET)   
public String index(ModelMap model) {                       

    // http.getData calls apache HttpGet httpget = new HttpGet(url); ... to get values
    String values2 = http.getData(http://mysrv.com/data?stuff="1").toString(); 
    model.addAttribute("highchartvalues1", highchartvalues1);

    String values2 = http.getData(http://mysrv.com/data?stuff="2").toString();
    model.addAttribute("highchartvalues2", highchartvalues2);           

    return "index";
}

View (.jsp/jstl): index.jsp

     // the values are actually consumed via highcharts

    <div id="highchart1">${highchartvalues1}</div>
    <div id="highchart2">${highchartvalues2}</div>

For Option 1. This Springsource Blog introduces Spring MVC 3.2 Asynchronous support

Spring MVC 3.2 - Introducing Servlet 3 Async Support

For Option 2. Spring 4.0 (only M1 release at present) will support websockets

Spring 4.0M1 Web Socket Support

Other options could be to return nothing to the view and use javascript to poll the server for the data. Once the data is ready, retrieve it and stop the polling.

Other server push technology I've used in the past is Direct Web Remoting (DWR) but this could be a fair amount of work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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