使用Spring MVC时如何在不刷新整个页面的情况下重新加载HTML表

使用Spring MVC时如何在不刷新整个页面的情况下重新加载HTML表,第1张

使用Spring MVC时如何在不刷新整个页面的情况下重新加载HTML表

如果要使用Javascript重新加载页面的一部分,基本上是AJAX。
这就是你应该怎么做。

客户端

假设您使用jQuery作为Javascript框架。
您需要在客户端使用jQuery.ajax()。

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  });};

reloadData()
需要重新加载表时调用该函数。

服务器端

您正在使用Spring MVC。然后,您的控制器应如下所示:

 // 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);   } }

您不必使用JSON,但我认为这是最好的方法。希望这会帮助你。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4946723.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存