最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to reload a HTML table without refreshing the whole page while working with Spring MVC - Stack Overflow

programmeradmin8浏览0评论

I am using Spring MVC , example.jsp file with Javascript.
I have been stuck on this for a long time.

Is it possible to use Javascript to laod new data from DB into HTML tables without refreshing the whole page?

I just don't want my whole page being replaced when new data is loaded into table with some event.

I am using Spring MVC , example.jsp file with Javascript.
I have been stuck on this for a long time.

Is it possible to use Javascript to laod new data from DB into HTML tables without refreshing the whole page?

I just don't want my whole page being replaced when new data is loaded into table with some event.

Share Improve this question edited Sep 17, 2012 at 14:50 mmmmmm 32.7k28 gold badges91 silver badges122 bronze badges asked Sep 17, 2012 at 11:46 NarayanNarayan 1692 silver badges11 bronze badges 2
  • Yes it is. What have you tried? – sp00m Commented Sep 17, 2012 at 11:48
  • Not much After working for 3 years in Desktop with java and spring , i am a month old in Web, so i didn't tried much sP00m , looked on some 30-40 links on web .. nothing informing me with spring MVC . – Narayan Commented Sep 17, 2012 at 12:00
Add a ment  | 

2 Answers 2

Reset to default 3

If you want to reload a part of you page using JavaScript, basically AJAX.
This is how you should do it.

Client Side

Let's assume that you're using jQuery as a JavaScript Framework.
You'll need to use jQuery.ajax() in the Client Side.

var successHandler = function( data, textStatus, jqXHR ) {
  // After success reload table with JavaScript
  // based on data...
};

var errorHandler = function( jqXHR, textStatus, errorThrown ) {
  // Error handler. AJAX request failed.
  // 404 or KO from server...
  alert('Something bad happened while reloading the table.');
};

var reloadData = function() { 
  // Process your request
  var request = new Object();
  request.id = 'some id'; // some data

  // Make the AJAX call
  jQuery.ajax({
    type       : 'POST',
    url        : 'http://domain/context/reload-data-url',
    contentType: 'application/json',
    data       : JSON.stringify(request)
    success    : successHandler,
    error      : errorHandler
  });
};

Call the reloadData() function when you need to reload the table.

Server Side

You're using Spring MVC. Then your Controller should look like:

 // Spring MVC Controller
 @Controller
 public class ReloadDataController {

   // Request Mapping
   @RequestMapping(value = '/reload-data-url', method = RequestMethod.POST)
   @ResponseBody
   public ResponseEntity<String> processReloadData(@RequestBody String body) {

     // Get your request
     JSONObject request = new JSONObject(body);
     String id = request.getString("id"); // Here the value is 'some id'

     // Get the new data in a JSONObject
     JSONObject response = new JSONObject();
     // build the response...

     // Send the response back to your client
     HttpHeaders headers = new HttpHeaders();
     headers.add("Content-Type", "application/json; charset=utf-8");
     return new ResponseEntity<String>(response.toString(),
                headers, HttpStatus.OK);
   }

 }

You don't have to use JSON but I think this is the best way. Hope this will help you.

I was also stucked into it than i finally found this solution which use ModelAndView See this solution it may work Thymeleaf table update without page reload

My answer would be late but someone might help...

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论