1 快速上手

2 JFinalConfig

3 Controller

4 AOP

5 ActiveRecord

6 Enjoy 模板引擎

7 EhCachePlugin

8 RedisPlugin

9 Cron4jPlugin

10 Validator

11 国际化

12 Json 转换

13 JFinal架构及扩展

14 升级到 5.1.2

3.5 getBean / getModel 系列

    getModel 用来接收页面表单域传递过来的model对象,表单域名称以”modelName.attrName”方式命名,getModel使用的attrName必须与数据表字段名完全一样。

    getBean 方法用于支持传统Java Bean,包括支持使用jfinal生成器生成了getter、setter方法的Model,页面表单传参时使用与setter方法相一致的attrName,而非数据表字段名。

    getModel与getBean区别在于前者使用数据库表字段名而后者使用与setter方法一致的属性名进行数据注入。建议优先使用getBean方法。

    以下是一个简单的示例:

// 定义Model,在此为Blog
public class Blog extends Model<Blog> {
    
}
 
// 在页面表单中采用modelName.attrName形式为作为表单域的name
<form action="/blog/save" method="post">
  <input name="blog.title" type="text">
  <input name="blog.content" type="text">
  <input value="提交" type="submit">
</form>
 
public class BlogController extends Controller {
  public void save() {
    // 页面的modelName正好是Blog类名的首字母小写
    Blog blog = getModel(Blog.class);
 
    // 如果表单域的名称为 "otherName.title"可加上一个参数来获取
    blog = getModel(Blog.class, "otherName");
  }
}

    上面代码中,表单域采用了 "blog.title"、"blog.content" 作为表单域的name属性,"blog" 是类文件名称 "Blog" 的首字母变小写, "title" 是blog数据库表的title字段,如果希望表单域使用任意的modelName,只需要在getModel时多添加一个参数来指定,例如:getModel(Blog.class, "otherName")。

     如果希望传参时避免使用modelName前缀,可以使用空串作为modelName来实现:getModel(Blog.class, ""); 这对开发纯API项目非常有用。(getBean 同样适用)

     如果希望在接收时跳过数据转换或者属性名错误异常可以传入true参:getBean(…, true)