前端通过ajax传过来多条数据,存入两张表,怎么用类似getModel的存储?

如果通过getPara逐个获取参数再存储,由于参数较多代码写很长,不雅观。

有没有好的办法?

评论区

杜福忠

2017-12-18 09:15

前端传的时候,参数名用不同的前缀, 如:
a.nema
b.nema

后端取的时候, 使用定义的前缀,如:
getModel(youModel.class, "a")
getModel(youModel.class, "b")
这样就可以了

混世侃

2017-12-18 15:03

@杜福忠 我知道form表单可以,还没试过用ajax这样做。

混世侃

2017-12-18 15:05

@杜福忠 我还发现了一个问题,不知是不是jfinal的bug,get传参时,如果参数是小数,后台无法接受类似于 http://localhost/controllerKey/method/3.1425-2.568

杜福忠

2017-12-18 16:55

@混世侃 不推荐这样使用, 使用传统挂参形式会方便功能扩展管理:
http://localhost/controllerKey/method?a=3.1425&b=2.568

由于开发环境中一般不会使用NGINX等做动静分离,
所以ActionHandler.java 》 handle 中使用:
if (target.indexOf('.') != -1) {
return ;
}
做了一个判断, 不处理带 . 的路径,也没有使用正则,为保持性能。

如果业务一定需要这样的参数形式, 那就自己继承Handler处理一下
YouJFinalConfig 中》 configHandler:

@Override
public void configHandler(Handlers me) {
// 自己处理
me.add(new YouHandler());
。。。
}

具体操作见手册: 2.7 configHandler (Handlers me)

杜福忠

2017-12-18 17:23

PS: 不推荐斜杠(/参数)和横杠(-)挂参的原因:
ActionMapping.java 中 》 getAction:
/**
* Support four types of url
* 1: http://abc.com/controllerKey ---> 00
* 2: http://abc.com/controllerKey/para ---> 01
* 3: http://abc.com/controllerKey/method ---> 10
* 4: http://abc.com/controllerKey/method/para ---> 11
* The controllerKey can also contains "/"
* Example: http://abc.com/uvw/xyz/method/para
*/
public Action getAction(String url, String[] urlPara) {
Action action = mapping.get(url);
if (action != null) {
return action;//《《 如果取到了就直接返回了
}

// -------- 没有的话。。还得找,裁剪判断。。后面取值的时候也是一顿split
int i = url.lastIndexOf('/');
if (i != -1) {
action = mapping.get(url.substring(0, i));
urlPara[0] = url.substring(i + 1);
}

return action;
}

热门反馈

扫码入社