开发者

Easy way for AXAX refresh JSP or Spring MVC page?

开发者 https://www.devze.com 2023-02-13 11:44 出处:网络
This seems a bit like a rudimentary question for java web development but... How would I go about refreshing data in a JSP page? I mean, I get the basics (use jQuery for AJAX, Spring MVC for the \"Co

This seems a bit like a rudimentary question for java web development but...

How would I go about refreshing data in a JSP page? I mean, I get the basics (use jQuery for AJAX, Spring MVC for the "Controller" & get data requests). What has开发者_开发问答 me stumbling is what is the easiest way to render the updated data to the page (given, the JSP is all server side...which is not conducive to client side updates)?

I've considered:

  • Using Mozilla Rhino + Velocity in the javascript - this seems a bit cumbersome

  • Using the "new" Spring AJAX MVC improvements - the examples for this seem a bit confusing to me.

  • Returning a semi-rendered String in the Spring Controller get method via the business logic+velocity - I'm not sure if it is "correct" to do it this way, it feels a bit dirty to build up the view object in the Controller class.

Is there an easy way to do what I am asking? I basically have an html table that needs to be repopulated on an interval. Surely I'm missing something here.

TIA


My suggestion would be to specify a div for the content you want to refresh. At the specified intervals, reload the div with fresh content from the server. I would recommend generating the html at the server and just jQuery('').load() the url. But you could also just get json data from the server and create your markup on the fly, but this is problematic with large records. Hope that helps.


About generating JSON or a partial view in the controller, both options are valid. I'd go with JSON if the HTML to generate/modify isn't too complex, and I'd choose returning a HTML fragment for something like refreshing a big table, or load a new complex panel, etc. To generate JSON I usually go with a Spring MVC controller method with a bean return type annotated with @ResponseBody.

0

精彩评论

暂无评论...
验证码 换一张
取 消