Home >Web Front-end >JS Tutorial >How to implement data update using jquery+ajaxform+springboot

How to implement data update using jquery+ajaxform+springboot

亚连
亚连Original
2018-06-11 11:23:513579browse

This article mainly introduces the jquery ajaxform springboot control to implement data update operations. Using jquery's ajaxform plug-in is a good choice. Please refer to this article for the specific implementation project

Application background

Use springboot architecture to save data in the interface layout as shown below Or update, it must be submitted to the background. How to prompt success or failure? If you use the traditional springmvc mode, you must pass a page to the front end. This page only prompts whether the operation was successful! After the prompt, the data must be updated, just like some prompts given when we browse some websites The operation is successful and will be returned after 5 seconds.

is relatively stupid and the customer experience is also relatively poor.

Transformation process

##Can the use of ajax solve the above problems?

The answer is yes. After clicking save, an ajax request is made to the background, using the ResponseBody tag to limit the return to only data. Then just give the corresponding prompt information based on the returned data.

Traditional ajax request data requires reorganizing the data in the form and putting it all into our request body. As shown in the following code:

$.ajax({
 type: 'POST',
 url: url,
 data: data,
 success: success,
 dataType: dataType
});

If the form data I submit is relatively large, then the data will be relatively large. The code is relatively bloated. At the same time, the form form has also lost its meaning of existence.

Is there a more elegant way to implement ajax submission of form forms? certainly.

jquery’s ajaxform plug-in is a relatively good choice.

Implementation process

The first step. Of course, we introduce our powerful plug-ins.

<script src="static/assets/js/jquery.form.min.js"></script>

To download the js, I will provide you with the github address: https://github.com/jquery-form/form.

The second step. Send the ajax form request where we want to send the request.

function insert() {
    var options = {
     // target:  &#39;#output1&#39;, // 用服务器返回的数据 更新 id为output1的内容.
     // beforeSubmit: showRequest, // 提交前
     success:  showResponse, // 提交后
     //另外的一些属性:
     //url:  url   // 默认是form的action,如果写的话,会覆盖from的action.
     //type:  type  // 默认是form的method,如果写的话,会覆盖from的method.(&#39;get&#39; or &#39;post&#39;).
     //dataType: null  // &#39;xml&#39;, &#39;script&#39;, or &#39;json&#39; (接受服务端返回的类型.)
     //clearForm: true  // 成功提交后,清除所有的表单元素的值.
     resetForm: false  // 成功提交后,重置所有的表单元素的值.
     //由于某种原因,提交陷入无限等待之中,timeout参数就是用来限制请求的时间,
     //当请求大于3秒后,跳出请求.
     //timeout: 3000
    };
    //&#39;ajaxForm&#39; 方式的表单 .
    $(&#39;#baseForm&#39;).ajaxSubmit(options);
   }

Process the method returned after submission

// 提交后
   function showResponse(responseText, statusText) {
    alert(&#39;状态: &#39; + statusText + &#39;\n 返回的内容是: \n&#39; + responseText);
    
   }

Backend implementation code

@RequestMapping("/basicSave")
 @ResponseBody
 public String BasicSave(PointInfo pi){
  if(!pi.getSaddress().equals("")){
   pi.setType("1");
  }else{
   pi.setType("0");
  }
  int newId = pointInfoService.saveorupdate(pi);
  return newId+"";
 }

The logic is self-defined

Note

  • Be sure to use the ajaxSubmit method when submitting

  • Submitted action, request method (post or get), dataType (json , xml), etc. are used by default. If the data in the form is modified in the parameters, it will be overwritten.

Follow-up research

##About the prompt box Use alert? I tried my best to use bootstrap. If you create an alert here, it is like spitting on a plate of delicious food. Is there a more beautiful prompt box? Of course, the answer is yes!

Use the notifications plug-in to complete the beautification of the prompt box

First the rendering:

Implementation steps 1. Import the js and css required for notifications

<script src="static/assets/plugins/notifyjs/dist/notify.min.js"></script>
 <script src="static/assets/plugins/notifications/notify-metro.js"></script>
 <script src="static/assets/plugins/notifications/notifications.js"></script>
 <link href="static/assets/plugins/notifications/notification.css" rel="external nofollow" rel="stylesheet">

2. Just call the interface

$.Notification.notify(&#39;success&#39;,&#39;top center&#39;, &#39;温馨提示&#39;, &#39;保存成功!&#39;);

The above is what I compiled for everyone, I hope It will be helpful to everyone in the future.

Related articles:

About how to use ejsExcel template

NavigatorIOS component in React Native (detailed tutorial description)

Detailed description of abstraction between components in React

The datepicker plug-in in Vue cannot monitor the value of the datepicker input box

Sample code for datepicker in Vue

How to solve the problem that the website cannot copy content without logging in

The above is the detailed content of How to implement data update using jquery+ajaxform+springboot. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn