Spring MVC Refactoring a jQuery Ajax Post example

摘要: Reviewing a jQuery Ajax form POST and Spring MVC example, find out the following patterns :

Reviewing a jQuery Ajax form POST and Spring MVC example, find out the following patterns :

<script>
  jQuery(document).ready(
	function($) {
	  $("#btn-save").click(function(event) {
		var id = $('#id').val();
		var domain = $('#domain').val();
		var name = $('#name').val();
		var desc = $('#desc').val();
		var tags = $('#tags').val();
		var afflink = $('#afflink').val();
		var cdn = $("#cdn").prop("checked") ? true : false;
		var hosting = $("#hosting").prop("checked") ? true : false;
		var paas = $("#paas").prop("checked") ? true : false;
		var display = $("#display").prop("checked") ? true : false;
		var imageUrl = $('#imageUrl').val();
		var favUrl = $('#favUrl').val();
		var whoisPattern = $('#whoisPattern').val();
		$("#btn-save").prop("disabled", true);
		$.post("/path-to/hosting/save", {
			id : id,
			domain : domain,
			name : name,
			desc : desc,
			tags : tags,
			afflink : afflink,
			display : display,
			hosting : hosting,
			cdn : cdn,
			paas : paas,
			imageUrl : imageUrl,
			favUrl : favUrl,
			whoisPattern : whoisPattern
		}, function(data) {
			var json = JSON.parse(data);
			//...
		}).done(function() {
		}).fail(function(xhr, textStatus, errorThrown) {
		}).complete(function() {
			$("#btn-save").prop("disabled", false);
		});
	});
  });
</script>

In Spring MVC, use @RequestParam to accept the Ajax POST data.

	@RequestMapping(value = "/path-to/hosting/save", method = RequestMethod.POST)
	@ResponseBody
    public String saveHosting(
		@RequestParam int id, @RequestParam String domain, @RequestParam String name,
		@RequestParam String desc, @RequestParam String tags, @RequestParam String afflink,
		@RequestParam boolean display, @RequestParam boolean hosting, 
		@RequestParam boolean cdn, @RequestParam boolean paas,
		@RequestParam String imageUrl, @RequestParam String favUrl,
		@RequestParam String whoisPattern
    ) {
        //...do something

The above code is working fine, just a bit weird and hard to maintain. Both Javascript $.post and Spring MVC @RequestParam is dealing with too many parameters.

1. Refactoring JavaScript

In Javascript, I prefer to use $.ajax, store everything into an array and POST it to Spring MVC.

<script>
  jQuery(document).ready(
	function($) {
		$("#btn-save").click(function(event) {
			var data = {}
			data["id"] = $("#id").val();
			data["domain"] = $("#domain").val();
			data["name"] = $("#name").val();
			data["desc"] = $("#desc").val();
			data["tags"] = $("#tags").val();
			data["afflink"] = $("#afflink").val();
			data["cdn"] = $("#cdn").prop("checked") ? true : false;
			data["hosting"] = $("#hosting").prop("checked") ? true : false;
			data["paas"] = $("#paas").prop("checked") ? true : false;
			data["display"] = $("#display").prop("checked") ? true : false;
			data["imageUrl"] = $("#imageUrl").val();
			data["favUrl"] = $("#favUrl").val();
			data["whoisPattern"] = $("#whoisPattern").val();
			$("#btn-save").prop("disabled", true);
			$.ajax({
		             type: "POST",
		             contentType: "application/json",
		             url: "/path-to/hosting/save",
		             data: JSON.stringify(data),
		             dataType: 'json',
		             timeout: 600000,
		             success: function (data) {
		                 $("#btn-update").prop("disabled", false);
		                 //...
		             },
		             error: function (e) {
		                 $("#btn-save").prop("disabled", false);
		                 //...
			});
		});
	});
</script>

2. Refactoring Spring MVC

2.1 Create a POJO to store the Ajax POST data.

public class HostingForm {
    private boolean display;
    private boolean cdn;
    private boolean hosting;
    private boolean paas;
    private String whoisPattern;
    private long id;
    private String domain;
    private String name;
    private String desc;
    private String tags;
    private String affLink;
    private String imageUrl;
    private String favUrl;
    //getters and setters

2.2 Accept the Ajax POST data with @RequestBody

@RestController
//...
    @RequestMapping(value = "/path-to/hosting/save", method = RequestMethod.POST)
    public String updateHosting(@RequestBody HostingForm hostingForm) {
        //...

With @RequestBody, Spring will maps the POST data to HostingForm POJO (by name) automatically. Done.

Note
You mat interest at this tutorial – Complete Spring 4 MVC + Ajax Form Post example

References

  1. jQuery $.ajax documentation
  2. Spring @RequestBody
  3. Spring @RequestParam

上一篇: Python How to loop a List
下一篇: Spring Inject value into static variables
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号