一、实验要求
根据参考资料,学习JFinal极速开发框架的使用并如下任务:
任务一:导入JFinal工程
任务二:阅读JFinal的源码并对每一部分的功能进行介绍
任务三:基于JFinal完成一个简单的学生信息管理系统
二、实验步骤
任务一:导入JFinal工程
1.配置maven
2.导入jfinal-4.9.01_demo_for_maven项目
3. 使用 blog.sql 中的 sql 语句创建数据库与数据库表
4. 修改 src/main/resources/demo-config-dev.txt 文件,填入正确的数据库连接用户名、密码
5. 打开 com.demo.common包下的 DemoConfig 文件,右键单击该文件并选择 Debug As---> Java Application,运行
6. 打开浏览器输入 localhost 即可查看运行效果
任务二:阅读JFinal的源码并对每一部分的功能进行介绍
1.控制器(支持FreeMarker、JSP、Velocity、JSON等以及自定义视图渲染):
|
@Before(BlogInterceptor.class)
public class BlogController extends Controller {
@Inject
BlogService service;
public void index() {
setAttr("blogPage", service.paginate(getParaToInt(0, 1), 10));
render("blog.html");
}
public void add() {
}
/**
* save 与 update 的业务逻辑在实际应用中也应该放在
serivce 之中,
* 并要对数据进正确性进行验证,在此仅为了偷懒
*/
@Before(BlogValidator.class)
public void save() {
getBean(Blog.class).save();
redirect("/blog");
}
public void edit() {
setAttr("blog", service.findById(getParaToInt()));
}
/**
* save 与 update 的业务逻辑在实际应用中也应该放在
serivce 之中,
* 并要对数据进正确性进行验证,在此仅为了偷懒
*/
@Before(BlogValidator.class)
public void update() {
getBean(Blog.class).update();
redirect("/blog");
}
public void delete() {
service.deleteById(getParaToInt());
redirect("/blog");
}
}
|
2.Service(所有业务与sql放在Service层)
|
/** *
* BlogService
* 所有 sql 与业务逻辑写在
Service 中,不要放在 Model 中,更不
* 要放在 Controller 中,养成好习惯,有利于大型项目的开发与维护
*/
public class BlogService {
/**
* 所有的 dao 对象也放在 Service 中,并且声明为 private,避免 sql 满天飞
* sql 只放在业务层,或者放在外部 sql 模板,用模板引擎管理:
*
http://www.jfinal.com/doc/5-13
*/
private Blog dao = new Blog().dao();
public Page<Blog> paginate(int pageNumber, int pageSize) {
return dao.paginate(pageNumber, pageSize, "select *", "from blog order by id asc");
}
public Blog findById(int id) {
return dao.findById(id);
}
public void deleteById(int id) {
dao.deleteById(id);
}
}
|
3.Model(无xml、无annotation、无attribute)
|
@SuppressWarnings("serial")
public class Blog extends BaseBlog<Blog> {
}
|
4.Validator(API引导式校验,比xml校验方便N倍,有代码检查不易出错)
|
public class BlogValidator extends Validator {
protected void validate(Controller controller) {
validateRequiredString("blog.title", "titleMsg", "请输入Blog标题!");
validateRequiredString("blog.content", "contentMsg", "请输入Blog内容!");
}
protected void handleError(Controller controller) {
controller.keepModel(Blog.class);
String actionKey =
getActionKey();
if (actionKey.equals("/blog/save"))
controller.render("add.html");
else if (actionKey.equals("/blog/update"))
controller.render("edit.html");
}
}
|
5.拦截器(在此demo中仅为示例,本demo不需要此拦截器)
|
public class BlogInterceptor implements Interceptor {
public void intercept(Invocation inv) {
System.out.println("Before invoking " + inv.getActionKey());
inv.invoke();
System.out.println("After invoking " +
inv.getActionKey());
}
}
|
任务三:基于JFinal完成一个简单的学生信息管理系统
- 新建student表
- 代码
DemoConfig:配置路由
StudentController:控制器
StudentService:
StudentValidator:
_layout.html:
_form.html:
Add.html:
edit.html
student.html
- 结果:
首页:

添加:


删除:

三、实验总结
这次实验接触了新的框架,使用jfinal,刚开始接很不熟悉,用了很大的力气才将项目导入进来,后来想通过命令进行打包和运行,但是一直报错,这个以后会再尝试。刚开始运行项目的时候,没有注意到在_MappingKit写学生的信息,就一直报错。通过学习博客项目,最后完成了这次学生信息管理系统,过程很不是很顺利但是成功了。通过这次作业我收获了一种新的框架的使用方法,了解到了jfinal框架的结构,丰富了自己的知识。