表单数据快速生成JSON

表单数据快速生成JSON

适用场景:向后台提交数据时,表单字段非常多,用于简化操作

一、实体类
1、pom.xml引入依赖
<!--引入lombok-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.12.6</version>
    <scope>provided</scope>
</dependency>
2、实体类代码
package com.java.pojo;

import lombok.Data;

/**
 * @author Liushun
 * @date Created in 2018/6/2 19:21
 * @description User实体类
 */

// 简化JavaBean的代码,比如:getter/setter/equals/hashcod/toString等方法,依赖lombok.jar包
@Data
public class User {
    // 用户名
    private String username;

    // 密码
    private String pwd;

    // 手机号
    private String phone;

    // 邮箱
    private String email;
}
二、表单数据
1、uerRegister.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
<%
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() +request.getContextPath()+"/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath %>">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>用户注册</title>
    <%--引入jquery--%>
    <script src="<%=basePath %>/static/js/jquery-1.8.2.min.js"></script>
    <%--引入自定义的js--%>
    <script src="<%=basePath %>/static/js/jsonHandler.js"></script>
</head>
<body>
    <center>
        <form action="javascript:void(0)" id="fm1">
            <table>
                <tr>
                    <td>用户名</td>
                    <td><input type="text" name="username"></td>
                </tr>
                <tr>
                    <td>密码</td>
                    <td><input type="text" name="pwd"></td>
                </tr>
                <tr>
                    <td>手机</td>
                    <td><input type="text" name="phone"></td>
                </tr>
                <tr>
                    <td>邮箱</td>
                    <td><input type="text" name="email"></td>
                </tr>
                <tr>
                    <td><input type="submit" value="注册" id="register"></td>
                    <td><input type="reset" value="重置"></td>
                </tr>
            </table>
        </form>
    </center>
</body>
    <script type="text/javascript">
        $(function(){
            $("#register").click(function(){
                var json = $("#fm1").serializeObject();	// json对象
                var jsonStr = JSON.stringify(json); // json对象转换成json字符串
                console.log(jsonStr);
                $.ajax({
                    url:"<%=basePath %>/index/userRegister",
                    type:"POST",
                    dataType:"JSON",
                    data:jsonStr,
                    contentType:"application/json",
                    success:function(rs){
                        console.log(rs);
                    },
                    error:function(rs){
                        alert("ajax发生错误了");
                    }
                });
            });
        });
    </script>
</html>
2、自定义的jsonHandler.js
$.fn.serializeObject = function()    
{    
   var o = {};    
   var a = this.serializeArray();    
   $.each(a, function() {    
	   if (o[this.name]) {    
		   if (!o[this.name].push) {    
			   o[this.name] = [o[this.name]];    
		   }    
		   o[this.name].push(this.value || '');    
	   } else {    
		   o[this.name] = this.value || '';    
	   }    
   });    
   return o;    
};
三、controller层
package com.java.controller;

import com.java.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Liushun
 * @date Created in 2018/6/1 21:04
 * @description
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    /**
     * 通过插件将表单属性和值快速变成json数据,通过ajax传递过来
     * @param user
     */
    @RequestMapping("/userRegister")
    public @ResponseBody boolean userRegister(@RequestBody User user){
        System.out.println("user = " + user);
        return true;
    };
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值