NutzCN Logo
问答 用哪种@Param,才能拿到前台传过来的值
发布于 2649天前 作者 qq_7fafbecc 2822 次浏览 复制 上一个帖子 下一个帖子
标签:

前台传值的 JS代码:

					var updated = $dg.datagrid('getChanges', "updated");//获取修改状态的行
					var effectRow = new Object();
					if (updated.length) {
						effectRow["updated"] = JSON.stringify(updated);
					}
					
					alert(effectRow["updated"]);
					//return;
					$.ajax({
						url: url,
						data: {
							"commitRows": effectRow
						},
						//表单序列化,会转变为类似a=1&b=2&c=3&d=4&e=5的值,问题是到了中间层,怎么通过@Param("..")赋值给baginfo userinfo的呢?
						dataType: "json",
						success: function(response) {
							if(response.code == '1') {
								$.messager.alert('提示:', '保存成功!');
								//var data = response.result;
							} else {
								//alert('出错了!');
								$.messager.alert('提示:', '保存出错了!'+response.result);
							}
						}
					});

alert出来的效果是:

[{"wardCode":"1010320aa","wardName":"呼吸内科重症监护室","quickInputCode":"HXNKZZJHS"},{"wardCode":"1010324bb","wardName":"呼吸内科重症监护室","quickInputCode":"HXNKZZJHS"},{"wardCode":"1010399cc","wardName":"呼吸内科重症监护室","quickInputCode":"HXNKZZJHS"}]

后台一直取不到前台传来的参数,无论用@Param("commitRows")还是@Param("..")取到的都是null,
想问要怎么才能拿到前台传过来的值?

41 回复

后台代码:

	@At("/commitWard")
	public Object commitWard(@Param("commitRows")Object object){
		HttpServletRequest req = Mvcs.getReq();
		Object obj = Mvcs.getReq().getParameter("commitRows");
		Object result = null;		//封装参数
		NutMap re = new NutMap();
		//re.putAll(object);
		result = comServices.ExcuteServices("B01.01.01.02", re);
		System.out.println(result);
	    return result;  
	}

前端要先转json

data:  JSON.stringify({"commitRows": effectRow})

后端用JsonAdaptor

@AdaptBy(type=JsonAdaptor.class)
public Object commitWard(@Param("commitRows")SomePojoOrMap object){

刚才试了,前台传这个

effectRow["updated"]

不用这句

@AdaptBy(type=JsonAdaptor.class)

也能拿到值,不懂为什么?

那就得看看到底发送了什么内容了

[{"updated":"[{\"wardCode\":\"1010320aaa\",\"wardName\":\"呼吸内科重症监护室\",\"quickInputCode\":\"HXNKZZJHS\"},{\"wardCode\":\"1010324bbb\",\"wardName\":\"呼吸内科重症监护室\",\"quickInputCode\":\"HXNKZZJHS\"},{\"wardCode\":\"1010399ccc\",\"wardName\":\"呼吸内科重症监护室\",\"quickInputCode\":\"HXNKZZJHS\"}]","inserted":"[]","deleted":"[]"}]

传的这个,可是为啥这么多斜杠?

拿到的是这么多斜杠,前台alert没有斜杠

噢,原来你在前面的代码里面已经转过json

data:  JSON.stringify({"commitRows": {"updated" : updated}})

这样咯,然后就需要json适配器了

可以拿到前台参数了,如下:

[{"updated":[{"wardCode":"1010320aaa","wardName":"呼吸内科重症监护室","quickInputCode":"HXNKZZJHS"},{"wardCode":"1010324bbb","wardName":"呼吸内科重症监护室","quickInputCode":"HXNKZZJHS"},{"wardCode":"1010399ccc","wardName":"呼吸内科重症监护室","quickInputCode":"HXNKZZJHS"}],"inserted":[],"deleted":[]}]

后台代码:

	@At("/commitWard")
	@AdaptBy(type=JsonAdaptor.class)
	public Object commitWard(@Param("commitData")Map paramMap){
		HttpServletRequest req = Mvcs.getReq();
		
		Object result = null;		//封装参数
		NutMap re = new NutMap();
		re.putAll(paramMap);
		result = comServices.ExcuteServices("B01.01.01.02", re);
	    return result;  
	}

异常信息:

2017-09-21 17:13:32,088 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) DEBUG - Found mapping for [GET] path=/dict/public/commitWard : PublicDictAction.commitWard(PublicDictAction.java:103)
2017-09-21 17:13:32,089 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'publicDictAction'<class com.shlx.blood.action.dict.PublicDictAction>
2017-09-21 17:13:32,111 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) WARN  - Error@/dict/public/commitWard :
org.nutz.castor.FailToCastObjectException: For the elements in array java.lang.String[], castors don't know which one is the key field.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:17)
	at org.nutz.lang.born.BornContext.doBorn(BornContext.java:60)
	at org.nutz.lang.Mirror.born(Mirror.java:990)
	at org.nutz.lang.Lang.makeThrow(Lang.java:149)
	at org.nutz.castor.castor.Array2Map.cast(Array2Map.java:21)
	at org.nutz.castor.castor.Array2Map.cast(Array2Map.java:10)
	at org.nutz.castor.Castors.cast(Castors.java:252)
	at org.nutz.castor.Castors.castTo(Castors.java:318)
	at org.nutz.mvc.adaptor.injector.NameInjector.fromReqParam(NameInjector.java:117)
	at org.nutz.mvc.adaptor.injector.NameInjector.get(NameInjector.java:102)
	at org.nutz.mvc.adaptor.AbstractAdaptor.adapt(AbstractAdaptor.java:257)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:28)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)

试了,用数组是可以取到参数,

@Param("commitData")String[] params

但是感觉很变扭啊,请问怎么能转变成List、或者Map这种我们常用的格式,因为容易增删改查,
不然,这一个String咋操作啊?

你前台发过来的就是数组...

没办法啊,你说只能用转成JSON,才能被@PARAM接收到呀
话说,String[]咋转成Map[] ??

你先试试Map[]嘛

直接声明Map[]

	@At("/commitWard")
	@AdaptBy(type=JsonAdaptor.class)
	public Object commitWard(@Param("commitData")Map[] params){
	//public Object commitWard(@Param("commitData")String[] params){
		HttpServletRequest req = Mvcs.getReq();
		
		Object result = null;		//封装参数
		NutMap re = new NutMap();
		re.put("commitData", params);
		System.out.println("params[0]=========="+params[0]);
		System.out.println("params[1]=========="+params[1]);
		System.out.println("params[2]=========="+params[2]);
		//re.put("inserted", params[0]);
		//re.put("updated", params[1]);
		//re.put("deleted", params[2]);
		result = comServices.ExcuteServices("B01.01.01.02", re);
	    return result;  
	}

这个获取的Map[] ,只有一个Map[0] Map[1] 和Map[2]都是空的

params[0]=========={updated=[{id=1, wardCode=1010320, wardName=呼吸内科重症监护aa, quickInputCode=HXNKZZJHS}, {id=2, wardCode=1010324, wardName=呼吸内科重症监护室bbb, quickInputCode=HXNKZZJHS}, {id=3, wardCode=1010399, wardName=呼吸内科重症监护室cc, quickInputCode=HXNKZZJHS}], inserted=[], deleted=[]}
2017-09-22 09:10:06,995 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) WARN  - Error@/dict/public/commitWard :
java.lang.ArrayIndexOutOfBoundsException: 1
	at com.shlx.blood.action.dict.PublicDictAction.commitWard(PublicDictAction.java:110)
	at com.shlx.blood.action.dict.PublicDictAction$$FASTCLASS._invoke(PublicDictAction.java:1)
	at org.nutz.lang.reflect.AbstractFastClass.invoke(AbstractFastClass.java:143)
	at org.nutz.lang.reflect.FastClassFactory.invoke(FastClassFactory.java:67)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)

刚在社区里找了找资料,文档里确实说了可以传LIST或者MAP呀:

示例代码,页面发送一个User类的json到服务器, 同理可以发送List,Map等等, 然后服务器用json响应
页面端的代码
$.ajax({
    url : ${base}/user/update
    type : "POST",
    data : $.toJSON({ id: 88, name:"wendal", location : "地球" }),
    dataType : "json", // 这是服务器响应的数据格式,不是发送内容的格式
    success : function (re) {
        if (re.ok) {
            console.log(re.data.id);
            console.log($.toJSON(re.data));
        }
    } 
});

贴你现在的前端代码

		function accept(url){
			if (endEditing()){
				//$('#dg').datagrid('acceptChanges');
				//===============开始和后台交互===============================================
				var rows = $('#dg').datagrid('getChanges');
				var $dg = $('#dg');
				alert(rows.length);
				if ($dg.datagrid('getChanges').length) {
					var inserted = $dg.datagrid('getChanges', "inserted"); //获取添加状态的行
					var deleted = $dg.datagrid('getChanges', "deleted");//获取删除状态的行
					var updated = $dg.datagrid('getChanges', "updated");//获取修改状态的行
					var effectRow = new Object();
					effectRow["updated"] = updated;
					effectRow["inserted"] = inserted;
					effectRow["deleted"] = deleted;

					$.ajax({
						url: url,
						type : "POST",
						//data:  {"inserted": JSON.stringify(inserted) },
						data: {
							"commitData": JSON.stringify(effectRow)
						},
						dataType: "json",
						success: function(response) {
							if(response.code == '1') {
								$.messager.alert('提示:', '保存成功!');
								//var data = response.result;
							} else {
								//alert('出错了!');
								$.messager.alert('提示:', '保存出错了!'+response.result);
							}
						}
					});
					
				}
				//================结束和后台交互==============================================
			}
		}

我晕,你就没按我说的写呢

这样写:

data: JSON.stringify(effectRow), // 是整个data变json!!!

然后后端这样写:

	@At("/commitWard")
	@AdaptBy(type=JsonAdaptor.class)
	public Object commitWard(@Param("..")Map maps){
                System.out.println(Json.toJson(maps)); // 打印一下内容

可以取到了,知识service层仍然无法处理这个MAP中的内容:

	@ServiceLog
	@Aop(TransAop.READ_COMMITTED)
	public Object commitWard(NutMap map) throws BusinessException {
		System.out.println("打印(这是从ACTION中传过来的MAP内容):");
		System.out.println(map);
		NutMap resultMap = new NutMap();
		int insertNum = 0;
		int updateNum = 0;
		int deleteNum = 0;
		List<Ward> insertList = new ArrayList<Ward>();
		List<Ward> updateList = new ArrayList<Ward>();
		List<Ward> deleteList = new ArrayList<Ward>();
		if(map.get("inserted")!=null){
			insertList = (ArrayList)map.get("inserted");
		}else{
			insertList = null;
		}
		if(map.get("updated")!=null){
			updateList = (ArrayList)map.get("updated");
		}else{
			updateList = null;
		}
		if(map.get("deleted")!=null){
			deleteList = (ArrayList)map.get("deleted");
		}else{
			deleteList = null;
		}
	
		//新增操作
		for(int i=0; i<insertList.size(); i++){
			Ward ward = insertList.get(i);
			dao.insert(ward);
			insertNum = insertNum + 1;
		}
		
		//更新操作
		for(int i=0; i<updateList.size(); i++){
			Ward ward = updateList.get(i);
			dao.update(ward);
			updateNum = updateNum + 1;
		}
		
		//删除操作
		for(int i=0; i<deleteList.size(); i++){
			Ward ward = deleteList.get(i);
			dao.delete(ward);
			deleteNum = deleteNum + 1;
		}
		resultMap.setv("insertNum", insertNum).setv("updateNum", updateNum).setv("deleteNum", deleteNum);
		return resultMap;
	}
打印(这是从ACTION中传过来的MAP内容):
{{"updated":[{"id":1,"wardCode":"1010320","wardName":"呼吸内科重症监护室aa","quickInputCode":"HXNKZZJHS"},{"id":2,"wardCode":"1010324","wardName":"呼吸内科重症监护室bb","quickInputCode":"HXNKZZJHS"},{"id":3,"wardCode":"1010399","wardName":"呼吸内科重症监护室cc","quickInputCode":"HXNKZZJHS"}],"inserted":[],"deleted":[]}=}

从map.get("updated")拿到的内容总是null,
有没有办法从map中拿到List这种对象list,这样才好做dao操作啊???

贴这个语句输出的内容

System.out.println(Json.toJson(maps)); // 打印一下内容
{
   "{\"updated\":[{\"id\":1,\"wardCode\":\"1010320\",\"wardName\":\"呼吸内科重症监护室aa\",\"quickInputCode\":\"HXNKZZJHS\"},{\"id\":2,\"wardCode\":\"1010324\",\"wardName\":\"呼吸内科重症监护室bb\",\"quickInputCode\":\"HXNKZZJHS\"},{\"id\":3,\"wardCode\":\"1010399\",\"wardName\":\"呼吸内科重症监护室cc\",\"quickInputCode\":\"HXNKZZJHS\"}],\"inserted\":[],\"deleted\":[]}": ""
}

我晕,你前端又没改啊!!!!!!!!!!!

真的改了啊

		function accept(url){
			if (endEditing()){
				//$('#dg').datagrid('acceptChanges');
				//===============开始和后台交互===============================================
				var rows = $('#dg').datagrid('getChanges');
				var $dg = $('#dg');
				alert(rows.length);
				if ($dg.datagrid('getChanges').length) {
					var inserted = $dg.datagrid('getChanges', "inserted"); //获取添加状态的行
					var deleted = $dg.datagrid('getChanges', "deleted");//获取删除状态的行
					var updated = $dg.datagrid('getChanges', "updated");//获取修改状态的行
					var effectRow = new Object();
					effectRow["updated"] = updated;
					effectRow["inserted"] = inserted;
					effectRow["deleted"] = deleted;
					$.ajax({
						url: url,
						type : "POST",
						data: JSON.stringify(effectRow), // 是整个data变json!!!
						dataType: "json",
						success: function(response) {
							if(response.code == '1') {
								$.messager.alert('提示:', '保存成功!');
								//var data = response.result;
							} else {
								//alert('出错了!');
								$.messager.alert('提示:', '保存出错了!'+response.result);
							}
						}
					});
				}
				//================结束和后台交互==============================================
			}
		}
var effectRow = {"updated":updated, "inserted":inserted, "deleted": deleted};
console.log(JSON.stringify(effectRow));

改成这样试试

chrome的控制台显示:

{"updated":[{"id":1,"wardCode":"1010320","wardName":"呼吸内科重症监护室aa","quickInputCode":"HXNKZZJHS"},{"id":2,"wardCode":"1010324","wardName":"呼吸内科重症监护室bb","quickInputCode":"HXNKZZJHS"},{"id":3,"wardCode":"1010399","wardName":"呼吸内科重症监护室cc","quickInputCode":"HXNKZZJHS"}],"inserted":[],"deleted":[]}

恩, 那后台呢? 打印什么

{
   "{\"updated\":[{\"id\":1,\"wardCode\":\"1010320\",\"wardName\":\"呼吸内科重症监护室aa\",\"quickInputCode\":\"HXNKZZJHS\"},{\"id\":2,\"wardCode\":\"1010324\",\"wardName\":\"呼吸内科重症监护室bb\",\"quickInputCode\":\"HXNKZZJHS\"},{\"id\":3,\"wardCode\":\"1010399\",\"wardName\":\"呼吸内科重症监护室cc\",\"quickInputCode\":\"HXNKZZJHS\"}],\"inserted\":[],\"deleted\":[]}": ""
}

还是这个

这样写

                                       var effectRow = {"updated":updated, "inserted":inserted, "deleted": deleted};
                                        console.log(JSON.stringify(effectRow));
					$.ajax({
						url: url,
						type : "POST",
						data: JSON.stringify(effectRow), // 是整个data变json!!!
                                                contentType : "text/plain",
						dataType: "json",
						success: function(response) {
							if(response.code == '1') {
								$.messager.alert('提示:', '保存成功!');
								//var data = response.result;
							} else {
								//alert('出错了!');
								$.messager.alert('提示:', '保存出错了!'+response.result);
							}
						}
					});

加上contentType : "text/plain",
@Param("..")Map 就取不到值了,哎

另外,用

var effectRow = {"updated":updated, "inserted":inserted, "deleted": deleted};

这种方式,@Param("..")Map能取到值,但是System.out.println(Json.toJson(paramMap)); 打印不出东西为什么?

当前的后台方法发一下看看

前台:

		function accept(url){
			if (endEditing()){
				//$('#dg').datagrid('acceptChanges');
				//===============开始和后台交互===============================================
				var rows = $('#dg').datagrid('getChanges');
				var $dg = $('#dg');
				alert(rows.length);
				if ($dg.datagrid('getChanges').length) {
					var inserted = $dg.datagrid('getChanges', "inserted"); //获取添加状态的行
					var deleted = $dg.datagrid('getChanges', "deleted");//获取删除状态的行
					var updated = $dg.datagrid('getChanges', "updated");//获取修改状态的行
					//var effectRow = new Object();
					var effectRow = {"updated":updated, "inserted":inserted, "deleted": deleted};
					console.log(JSON.stringify(effectRow));

					//effectRow["updated"] = updated;
					//effectRow["inserted"] = inserted;
					//effectRow["deleted"] = deleted;
					
					
					
/*					if (updated.length) {
						effectRow["updated"] = JSON.stringify(updated);
					}
					if (inserted.length) {
						effectRow["inserted"] = JSON.stringify(inserted);
					}
					if (deleted.length) {
						effectRow["deleted"] = JSON.stringify(deleted);
					}*/
					
					//alert(effectRow["updated"]);
					//return;
					$.ajax({
						url: url,
						type : "POST",
						data: JSON.stringify(effectRow), // 是整个data变json!!!
                        contentType : "text/plain",
/*						data: {
							"commitData": JSON.stringify(effectRow)
						},*/
						dataType: "json",
						success: function(response) {
							if(response.code == '1') {
								$.messager.alert('提示:', '保存成功!');
								//var data = response.result;
							} else {
								//alert('出错了!');
								$.messager.alert('提示:', '保存出错了!'+response.result);
							}
						}
					});
					
					
/*					$.post(url,effectRow, function(rsp) {
						debugger;
						if(rsp.status){
							$.messager.alert("提示", "提交成功!");
								$dg.datagrid('acceptChanges');
							}
						}, "JSON").error(function() {
							// $.messager.alert("提示", "提交错误了!");
						});*/
				}
				//================结束和后台交互==============================================
			}
		}

后台ACTION:

	@At("/commitWard")
	public Object commitWard(@Param("..")Map paramMap){
		HttpServletRequest req = Mvcs.getReq();
        System.out.println(Json.toJson(paramMap)); // 打印一下内容
		Object result = null;		//封装参数
		NutMap re = new NutMap();
		re.putAll(paramMap);
		//System.out.println("params[0]=========="+inserted);
		//System.out.println("params[1]=========="+updated);
		//System.out.println("params[2]=========="+deleted);
		//re.put("inserted", params[0]);
		//re.put("updated", params[1]);
		//re.put("deleted", params[2]);
		result = comServices.ExcuteServices("B01.01.01.02", re);
	    return result;  
	}

缺了@AdaptBy(type=JsonAdaptor.class)

这回打印的是这个了:

{
   "updated": [{
      "id": 1,
      "wardCode": "1010320",
      "wardName": "呼吸内科重症监护室aa",
      "quickInputCode": "HXNKZZJHS"
   }, {
      "id": 2,
      "wardCode": "1010324",
      "wardName": "呼吸内科重症监护室bb",
      "quickInputCode": "HXNKZZJHS"
   }, {
      "id": 3,
      "wardCode": "1010399",
      "wardName": "呼吸内科重症监护室cc",
      "quickInputCode": "HXNKZZJHS"
   }],
   "inserted": [],
   "deleted": []
}

那我接下来,是不是可以在service中,把JSON中的内容转换为对象数组了呢?

		List<Ward> updateList = new ArrayList<Ward>();
		if(map.get("updated")!=null){
			updateList = (ArrayList)map.get("updated");
		}else{
			updateList = null;
		}

恩, 做个pojo来映射比较好吧

		List<Map> insertList = new ArrayList<Map>();
		List<Map> updateList = new ArrayList<Map>();
		List<Map> deleteList = new ArrayList<Map>();
		insertList = (List)map.get("inserted");
		updateList = (List)map.get("updated");
		deleteList = (List)map.get("deleted");
		//新增操作
		for(int i=0; i<insertList.size(); i++){
			Ward ward = new Ward();
			ward = Lang.map2Object(insertList.get(i), Ward.class);
			dao.insert(ward);
			insertNum = insertNum + 1;
		}

非常感谢兽总,前台、后台,都OK了,数据库数据更新也OK了
还额外请教一句,这次报错,是否可以总结为以下几点:
1、前台$.ajax请求中的data,是否只能是json形式?
无论data中要传的是单个变量、多个变量、map、list等形式,都必须转成JSON,才能传给后台?
2、后台是否只有通过@AdaptBy(type=JsonAdaptor.class)注解,才能拿到一整串的JSON字符串?
例如:JSON.stringify(effectRow),而以前不用加adapt是因为没有用没有用JSON.stringify,传的是这个格式:data: {"functionId": nodeId}。
3、前台传过来的JSON.stringify(ooxx),后台一般用@Param("..")Map paramMap接收就行了

data传对象形式的数据, json是最佳的了, 普通key-value就不需要用json

如果data的内容是json字符串,才需要用JsonAdaptor

添加回复
请先登陆
回到顶部