JFinal框架学习笔记二
2015-12-02 22:21
239 查看
了解了基本的配置,现在就开始愉快的玩耍了!我们先看看JFianl中的Controller是怎么回事吧,还有就是它提供了那些方法让我们的开饭更加方便呢?
一、JFinal中的Controler基本的实现
Controller作为MVC模式中的控制器,也是JFianl的核心之一,在JFianl框架中,控制器需要继承Controller这个类,Controller是定义action的方法的地点,是组织Action的一种方式,一个Controller可以包含多个Action。
关于JFianl的Controller中封装了一系列的方法来实现Controller控制器的功能。
比如在Spring中的Controller是通过注解来实现一系列的Controller控制器功能:
在JFinal总则是通过继承Controller控制器,并通过Controller内置的一些方法来实现Controller控制器功能:
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。
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 之外的视图。
一、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 之外的视图。
相关文章推荐
- 对存取控制的认识
- Beta版本冲刺———第二天
- Servlet小总结(转)
- 编写一个程序从键盘输入字符,并按要求输出
- python显示中文
- android Camera
- asp连接sql server
- 我的第一个VTK程序--绘制球体
- struts2 集成webservice 的方法
- 设计模式-外观模式
- 临时想法:上级布置一个编程任务,刚开始不要太抠细节,先把架构做出来
- gcc 内联asm
- leetcode---Add Digits
- asp连接sql server
- 超级强大的formValidator
- u3d关于system.data每次在vs上重新加载都会丢失引用的问题
- Java关键字instanceof
- memcpy, memccpy函数实现——string.h库函数
- Ubuntu14.04下手动安装Firefox的Flash插件
- submit与execute区别