ajax传多个实体参数到后端controller

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31638493/article/details/75669419
从ajax传多个对象参数到controller时,
如果,采用request.getParamter("roleName");的方式,那么就要一个参数一个参数的取,这样不灵活。

此时我们可以自定义一个PO类或者VO类来包装这些参数:

/**
 * ajax传递参数类
 * @author Administrator
 *
 */
public class AjaxParamsVO implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 222255551L;

	private PayAssembly assembly;
	
	private PayTemplate template;
	
	private OperateLog operateLog;

	/**
	 * @return the assembly
	 */
	public PayAssembly getAssembly() {
		return assembly;
	}

	/**
	 * @param assembly the assembly to set
	 */
	public void setAssembly(PayAssembly assembly) {
		this.assembly = assembly;
	}

	/**
	 * @return the operateLog
	 */
	public OperateLog getOperateLog() {
		return operateLog;
	}

	/**
	 * @param operateLog the operateLog to set
	 */
	public void setOperateLog(OperateLog operateLog) {
		this.operateLog = operateLog;
	}

	public PayTemplate getTemplate() {
		return template;
	}

	public void setTemplate(PayTemplate template) {
		this.template = template;
	}
	
	
}
controller中可以这样处理:
@PostMapping(value = "/add" ,consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public RestfulResponse add(@RequestBody AjaxParamsVO ajaxParamsVO){
    	
        RestfulResponse restfulResponse =  restTemplate.postForObject(getUrl(ControllerConstant.PAY_SERVICE, ServicePortConstant.ASSEMBLY_ADD), ajaxParamsVO, RestfulResponse.class);
        return restfulResponse;
    }

这样参数AjaxParamsVO就能一次性全部拿到前端的参数了。

使用ajax方式传递:

//添加保存功能
function addRender() {
    if(!$('#addForm').valid()){
        return;
    }
     var payAssemblyVO = collectAdd("add");
     //判断是否配置支付方式
    if(payAssemblyVO.assembly.payModes.length <=0){
    	swal("保存失败!", "请配置支付方式!", "error");
    	return;
    }
    
    $.ajax({
        type: "POST",
        url: '/payAssembly/add',
        contentType: 'application/json',
        data: JSON.stringify(payAssemblyVO),
        success: function(result) {
            $('#addModel').modal('hide');
            if(result.meta && result.meta.success == true) {
            	swal({
		    		title:"保存成功!",
		    		type:"success",
		    		confirmButtonText:"确定",
		    		confirmButtonColor: "#DD6B55",
		    		showConfirmButton:true,
		    		showCancelButton: false, 
		    		timer:2000
		    	},function() {
		    		window.location.href="/payAssembly/list";//跳转到list页面
				});
            }else {
                swal("保存失败!", "请稍后操作!", "error");
            }
        }
    });
}

//收集添加表单参数
function collectAdd(type) {
    var assembly = {};//bean类
    
    assembly.code = $("#"+type+"-assemblyCode").val();
    assembly.name = $("#"+type+"-assemblyName").val();
    assembly.asName = $("#"+type+"-assemblyAsName").val();
   	assembly.sort = $("#"+type+"-assemblySort").val();
   	assembly.delSta = $("#"+type+"-delSta").val();
   	assembly.useSta = $("#"+type+"-useSta").val();
   	
    if(type == 'edit'){
        assembly.id = $.trim($('#'+type+'-id').val());
    }
    
//    var opts = document.getElementById("select2").options;//获取所有option对象
    var opts = $("#select2 option");
    
    var payModes = new Array();
//获得payMode对象数组
    for(var i = 0 ; i < opts.length ; i++){
    	var payMode = {};
		/*//排除空对象,alert(opts.item(i).innerHTML)显示option内的值。
		if(opts.item(i).innerHTML!=undefined){
			payMode.id = opts.item(i).value;
		}*/
    	//排除空对象
		if(opts.get(i) != null){
			payMode.id = opts.get(i).value;
		}
		payModes.push(payMode);
    }
    assembly.payModes = payModes;
    
   	
   	var operateLog ={};//操作日志bean类
   	operateLog.operDetail = $("#"+type+"-description").val();//备注
   	
   	var payAssemblyVO ={};//参数类
   	payAssemblyVO.assembly = assembly;
   	payAssemblyVO.operateLog = operateLog;

    return payAssemblyVO;
}
如此我们便能在controller中得到从前端传过来的多个bean参数

猜你喜欢

转载自blog.csdn.net/qq_31638493/article/details/75669419
今日推荐