分享一个本身写的JFinal的BaseController (1)

 

之前用struts的时候本身就写了一个BaseAction
因此用JFinal的时候也写了一个BaseControllerjava

但愿对你们有所帮助,让JFinal保持大道至简this

 

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
public class BaseController extends Controller {
 private Class<?> clazz; // 对应的实体
 public Class<?> getClazz() {
  return clazz;
 }
 public void setClazz(Class<?> clazz) {
  this.clazz = clazz;
 }
 /**
  * 通用分页查找
  */
 public void getByPage() {
  Page<Record> list = Db.paginate(getParaToInt("pageNumber"),
    getParaToInt("pageSize"), "select *", "from "
      + getClazz().getSimpleName() + " order by id desc");
  renderJson(list);
 }
 /**
  * 通用查找所有
  */
 public void getAll() {
  renderJson(Db.find("select * from " + getClazz().getSimpleName() + ";"));
 }
 /**
  * 通用根据id查找
  */
 public void getById() {
  renderJson(Db.findById(getClazz().getSimpleName(), getParaToInt("id")));
 }
 /**
  * 通用新增
  * 
  * @throws Exception
  */
 public void save() throws Exception {
  renderText(getModel(
    ((Model<?>) Class.forName(clazz.getName()).newInstance())
      .getClass()).save()
    + "");
 }
 /**
  * 通用修改
  * 
  * @throws Exception
  */
 public void update() throws Exception {
  renderText(getModel(
    ((Model<?>) Class.forName(clazz.getName()).newInstance())
      .getClass()).update()
    + "");
 }
 /**
  * 通用删除
  * 
  * @throws Exception
  */
 public void delete() throws Exception {
  renderText(getModel(
    ((Model<?>) Class.forName(clazz.getName()).newInstance())
      .getClass()).delete()
    + "");
 }
}

而后你的Controller只须要继承BaseController.net

就自动有了BaseController的全部方法的,须要在构造方法里把Mode的class映射进去code

 

Controller的代码以下blog

public class CardController extends BaseController {
 public CardController() {
  setClazz(Card.class);
 }
}

 

权限之类的就须要你本身处理过滤了,过滤也很是方便的。继承

代码写得很差的地方请你们给予纠正。get

 

 

通过@JFinal的评论提示io

修改版的 BaseContorller 以下 :class

 分享一个本身写的JFinal的BaseController (2)import

相关文章
相关标签/搜索