您的位置:首页 > 其它

JFinal框架学习笔记二

2015-12-02 22:21 239 查看
了解了基本的配置,现在就开始愉快的玩耍了!我们先看看JFianl中的Controller是怎么回事吧,还有就是它提供了那些方法让我们的开饭更加方便呢?

一、JFinal中的Controler基本的实现
Controller作为MVC模式中的控制器,也是JFianl的核心之一,在JFianl框架中,控制器需要继承Controller这个类,Controller是定义action的方法的地点,是组织Action的一种方式,一个Controller可以包含多个Action。
package com.demo.student;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
/**
* Controller
* 所有 sql 与业务逻辑写在 Model 或 Service 中,不要写在 Controller 中,养成好习惯,有利于大型项目的开发与维护
*/
@Before(StudentInterceptor.class)
public class StudentController extends Controller {
public void index() {
setAttr("studentPage", Student.me.paginate(getParaToInt(0, 1), 10));
render("student.html");
}
/**
* 添加
*/
public void add() {
}
@Before(StudentValidator.class)
public void save() {
getModel(Student.class).save();
redirect("/student");
}
/**
* 修改
*/
public void edit() {
setAttr("student", Student.me.findById(getParaToInt()));
}
@Before(StudentValidator.class)
public void update() {
getModel(Student.class).update();
redirect("/student");
}
/**
* 删除
*/
public void delete() {
Student.me.deleteById(getParaToInt());
redirect("/student");
}
/**
* 查看
*/
public void find() {
setAttr("student", Student.me.findById(getParaToInt()));
}
@Before(StudentValidator.class)
public void select() {
getModel(Student.class).findById(getParaToInt());
redirect("/student");
}
}

关于JFianl的Controller中封装了一系列的方法来实现Controller控制器的功能。
比如在Spring中的Controller是通过注解来实现一系列的Controller控制器功能:
@RequestMapping(value="/pub/user",method=RequestMethod.GET)
public String zres(HttpServletRequest request,ModelMap model,String errorMsg) {
<span style="white-space:pre">	</span>request.getSession().getServletContext().setAttribute("session",request.getSession());
return "/pub/user";
}

在JFinal总则是通过继承Controller控制器,并通过Controller内置的一些方法来实现Controller控制器功能:
public class StudentController extends Controller {
public void index() {
setAttr("studentPage", Student.me.paginate(getParaToInt(0, 1), 10));
render("student.html");
}
/**
* 添加
*/
public void add() {
}
@Before(StudentValidator.class)
public void save() {
getModel(Student.class).save();
redirect("/student");
}
。。。 。。。
}

JFinal中Controller方法介绍(关于详细的功法和区分可以参考JFinal的说明文档,我只是在这里和Spring中的一些方法做个粗略的比较):
getPara系列方法:
Controller提供了 getPara系列方法用来从请求中获取参数。getPara系列方法分为两种类型。 第 一 种 类 型 为 第 一 个 形 参 为String 的 getPara 系 列 方 法 。 该 系 列 方 法 是 对 HttpServletRequest.getParameter(String name) 的 封 装 , 这 类 方 法都 是 转 调 了 HttpServletRequest.getParameter(String name)。第二种类型为第一个形参为
int 或无形参的 getPara 系列方法。该系列方法是去获取 urlPara 中所带的参数值。 getParaMap 与 getParaNames 分别对应 HttpServletRequest 的 getParameterMap 与 getParameterNames。
那么getParameter是怎么回事呢?
HttpServletRequest.getParameter("modelName");当两个Web组件之间为转发关系时,转发源会将要共享request范围内的数据先用setAttribute将数据放入到HttpServletRequest对象中,然后转发目标通过 getAttribute方法来取得要共享的数据。
在JFianl中也是提供了关于getPara的一系列方法,具体的操作和用法参见JFianl的说明文档,实践出真理。
getModel 系列方法:
getModel 用来接收页面表单域传递过来的 model 对象,表单域名称以”modelName.attrName” 方式命名,除了支持JFinal 的 Model 对象以外, getModel 同时也支持传统的 Java Bean。
@Before(StudentValidator.class)
public void save() {
getModel(Student.class).save();
redirect("/student");
}

<#include "/common/_layout.html" />
<@layout>
<h1>管理首页 ---> 学生系统录入
</h1>
<div class="form_box">
<form action="/student/save" method="post">
<#include "_form.html" />
</form>
</div>
</@layout>

<fieldset class="solid">
<legend>学生信息录入</legend>
<input type="hidden" name="student.id" value="${(student.id)!}" />
<div>
<label>姓名</label>
<input type="text" name="student.name" value="${(student.name)!}" />${nameMsg!}
</div>
<div>
<label>性别</label>
<input type="text" name="student.sex" value="${(student.sex)!}" />${sexMsg!}
</div>
<div>
<label>班级</label>
<input type="text" name="student.grade" value="${(student.grade)!}" />${gradeMsg!}
</div>
。。。 。。。
>
</fieldset>

getFile 文件上传
Controller 提供了 getFile 系列方法支持文件上传。 特别注意: 如果客户端请求为 multipartrequest( form 表单使用了enctype="multipart/form-data"),那么必须先调用getFile 系列方法才能使 getPara 系列方法正常工作,因为 multipart request 需要通过 getFile 系列方法解析请求体中的数据,包括参数。
setAttr 方法 :
setAttr(String,Object)转调了 HttpServletRequest.setAttribute(String,Object),该方法可以将 各种数据传递给 View 并在View 中显示出来。
session 操作方法
通过setSessionAttr(key, value)可以向 session 中存放数据,getSessionAttr(key)可以从 session 中读取数据。还可以通过 getSession()得到 session 对象从而使用全面的 session API。
render 系列方法
render 系列方法将渲染不同类型的视图并返回给客户端。 JFinal 目前支持的视图类型有: FreeMarker、 JSP、 Velocity、 JSON、 File、 Text、 Html 等等。除了 JFinal 支持的视图型以外, 还可以通过继承 Render 抽象类来无限扩展视图类型。通常情况下使用 Controller.render(String)方法来渲染视图, 使用 Controller.render(String)时 的 视 图 类 型 由JFinalConfig.configConstant(Constants
constants) 配 置 中 的 constants. setViewType(ViewType)来决定,该设置方法支持的 ViewType 有: FreeMarker、 JSP、 Velocity, 不进行配置时的缺省配置为 FreeMarker。 此 外 , 还 可 以 通 过constants.setMainRenderFactory(IMainRenderFactory) 来 设 置 Controller.render(String)所使用的视图, IMainRenderFactory专门用来对
Controller.render(String) 方法扩展除了 FreeMarker、 JSP、Velocity 之外的视图。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: