博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JFinal源码--获取表单数据
阅读量:6898 次
发布时间:2019-06-27

本文共 4491 字,大约阅读时间需要 14 分钟。

hot3.png

使用request获取表单中数据的方法
String name = request.getParameter("user.name");User user = new User();user.setName(name);
1.JFinal中可以直接将表单的数据注入到Model中
User u = getModel(User.class);

getModel方法是继承自Controller,注入的具体实现在ModelInjector类中

/**     * Get model from http request.     */    public 
T getModel(Class
modelClass) { return (T)ModelInjector.inject(modelClass, request, false); }

2.ModelInjector 表单数据注入的具体实现类

    通过传入的 model.class 得到 model类的所有方法,然后找到属性所对应的set方法,将request取到的值转换为对应的数据类型,最后把值赋给model类

final class ModelInjector {        public static 
T inject(Class
modelClass, HttpServletRequest request, boolean skipConvertError) { String modelName = modelClass.getSimpleName(); return inject(modelClass, StringKit.firstCharToLowerCase(modelName), request, skipConvertError); } @SuppressWarnings({ "rawtypes", "unchecked" }) public static final
T inject(Class
modelClass, String modelName, HttpServletRequest request, boolean skipConvertError) { Object model = null; try { model = modelClass.newInstance();//采用反射创建一个model的实例 } catch (Exception e) { throw new RuntimeException(e); } if (model instanceof Model) injectActiveRecordModel((Model)model, modelName, request, skipConvertError);//继承Model类注入表单数据的实现 else injectCommonModel(model, modelName, request, modelClass, skipConvertError);//普通Moedl类注入表单数据的实现 return (T)model; } private static final void injectCommonModel(Object model, String modelName, HttpServletRequest request, Class
modelClass, boolean skipConvertError) { Method[] methods = modelClass.getMethods();//获得Model类的所有方法 for (Method method : methods) { String methodName = method.getName();//方法名 //只有实现了属性对应的set方法并且传入的参数为一个的时候才能把form里的数据注入到对应的model属性上 if (methodName.startsWith("set") == false) // only setter method continue; Class
[] types = method.getParameterTypes(); if (types.length != 1) // only one parameter continue; String attrName = methodName.substring(3); String value = request.getParameter(modelName + "." + StringKit.firstCharToLowerCase(attrName));//拼接出model.属性(JFinal默认form里数据名必须是类名.属性)这样才能用request取到对应的值 if (value != null) { try { method.invoke(model, TypeConverter.convert(types[0], value));//TypeConverter将value值转换成类属性对应的数据类型,执行model的method方法将value set到model的属性上 } catch (Exception e) { if (skipConvertError == false) throw new RuntimeException(e); } } } } @SuppressWarnings("rawtypes") private static final void injectActiveRecordModel(Model
model, String modelName, HttpServletRequest request, boolean skipConvertError) { TableInfo tableInfo = TableInfoMapping.me().getTableInfo(model.getClass());//TableInfo保存有Model类的所有属性,相当于一个Model实例 String modelNameAndDot = modelName + "."; Map
parasMap = request.getParameterMap(); for (Entry
e : parasMap.entrySet()) {//request中的数据是以key-value的方式保存的,遍历提交来form的所有数据 String paraKey = e.getKey(); if (paraKey.startsWith(modelNameAndDot)) {//满足“类.属性”格式的的数据才能被识别保存到model中 String paraName = paraKey.substring(modelNameAndDot.length()); Class colType = tableInfo.getColType(paraName);//从tableInfo中查找 属性对应的type if (colType == null) throw new ActiveRecordException("The model attribute " + paraKey + " is not exists."); String[] paraValue = e.getValue();//取数据 try { // Object value = Converter.convert(colType, paraValue != null ? paraValue[0] : null); Object value = paraValue[0] != null ? TypeConverter.convert(colType, paraValue[0]) : null;//将数据转换为正确类型 model.set(paraName, value);//保存数据到model里 } catch (Exception ex) { if (skipConvertError == false) throw new RuntimeException("Can not convert parameter: " + modelNameAndDot + paraName, ex); } } } }}

转载于:https://my.oschina.net/u/1182234/blog/264583

你可能感兴趣的文章
来,带你鸟瞰 Java 中4款常用的并发框架!
查看>>
MySQL5.7安装部署
查看>>
经典排序算法python回顾之五 归并排序
查看>>
JAVA监测tomcat是否宕机,控制重启
查看>>
ForkJoinPool
查看>>
Python转换dict到object
查看>>
Java8 默认方法
查看>>
python 获取行号,函数名等信息
查看>>
css
查看>>
php 上传图片文件类型整理
查看>>
设计模式-单一职责原则
查看>>
const,static,extern
查看>>
python使用pickle,marshal进行序列化、反序列及JSON的使用
查看>>
ubuntu无法sudo解决
查看>>
关于java nio socket的2个小问题的研究
查看>>
我的友情链接
查看>>
LoadRunner监控服务器性能指标
查看>>
制作一张属于DIY的CnetOS 7安装光盘
查看>>
备份介绍以及mysqldump用法
查看>>
文件,文本文件以及编码——乱码探源(1)
查看>>