初学Jfinal

来源:互联网 发布:java导出jar包 编辑:程序博客网 时间:2024/06/10 20:44

初学框架 第一个就是jfinal 小白自己也不懂 自己在写着给自己看吧

1.Model类

package demo;import com.jfinal.plugin.activerecord.Model;import com.jfinal.plugin.activerecord.Page;public class LYBModel extends Model<LYBModel> {public static final LYBModel dao = new LYBModel();public Page<LYBModel> paginate(int pageNumber, int pageSize) {return paginate(pageNumber, pageSize, "select *", "from message order by id asc");}}



2.配置jfinal

这里用的是mysql,

package demo;import com.jfinal.config.*;import com.jfinal.plugin.c3p0.C3p0Plugin;import com.jfinal.plugin.activerecord.ActiveRecordPlugin;import com.jfinal.config.Constants;import com.jfinal.core.JFinal;public class DemoConfig extends JFinalConfig {<span style="white-space:pre"></span>public void configConstant(Constants me) {
<span style="white-space:pre"></span>//加载属性文件<span style="white-space:pre"></span>loadPropertyFile("a_little_config.txt");                                me.setDevMode(getPropertyToBoolean("devMode", false));<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void configRoute(Routes me) {
<span style="white-space:pre"></span>//配置访问路由<span style="white-space:pre"></span>me.add("/", HelloController.class);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void configPlugin(Plugins me) {<span style="white-space:pre"></span>// 配置C3p0数据库连接池插件<span style="white-space:pre"></span>C3p0Plugin cp = new C3p0Plugin(getProperty("DB_url"),getProperty("DB_user"),getProperty("DB_password"));<span style="white-space:pre"></span>me.add(cp);<span style="white-space:pre"></span><span style="white-space:pre"></span>// 配置ActiveRecord插件<span style="white-space:pre"></span>ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);<span style="white-space:pre"></span>me.add(arp);<span style="white-space:pre"></span>// 映射message表  到   LYBModel模型<span style="white-space:pre"></span>arp.addMapping("message",LYBModel.class);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void configInterceptor(Interceptors me) {<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void configHandler(Handlers me) {<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>public static void main(String[] args) {<span style="white-space:pre"></span>JFinal.start("WebRoot", 80, "/", 5);<span style="white-space:pre"></span>}}


3.
package demo;import demo.LybValidator;import com.jfinal.aop.Before;import com.jfinal.core.Controller;import demo.LYBModel;public class HelloController extends Controller {public void index() {//返回url请求中的urlPara参数的第1个值,没有的话默认为1,并转换成int型        setAttr("messageList", LYBModel.dao.paginate(getParaToInt(0, 1), 6));        render("list.html");}@Before(LybValidator.class)public void add() {// 获取前端传来的参数有几种方式,根据名字或者根据下标顺序String title = this.getPara("message.title");String content = this.getPara("message.content");new LYBModel().set("title", title).set("content", content).save();redirect("/");}public void delete() {// 获取url请求中第一个值LYBModel.dao.deleteById(getParaToInt());redirect("/");}@Before(LybValidator.class)public void update() {/* * getModel(ModelName.class) 相当于调用了: getModel(ModelName.class, "modelName") 这个方法, * 即: getModel(ModelName.class) 将 ModelName 的首字母变小写当成 modelName 来使用,modelName 是指页面表单中使用的 modelName。 * 当页面表单中的modelName 正好是ModelName 类的首字母小写则可以省去getModel 的第二个参数    */getModel(LYBModel.class,"message").update();redirect("/");}public void get() {setAttr("message", LYBModel.dao.findById(getParaToInt()));render("/edit.html");}}


4.显示留言板的list页

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>留言板</title></head><body><h2>留言板</h2><table border="1"><tr><#list messageList.getList() as msg><td>${msg.id}</td><td>${msg.title}</td><td>${msg.content}</td><td><a href="/jfinal_demo/get/${msg.id}">更改</a></td><td><a href="/jfinal_demo/delete/${msg.id}">删除</a></td></tr></#list></table><h3><a href="/jfinal_demo/add">添加留言</a></h3><#include "/_paginate.html" /><@paginate currentPage = messageList.pageNumber totalPage = messageList.totalPage actionUrl = "/jfinal_demo/" /></body></html>

5.add.html

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body><h3>添加留言</h3><form action="/jfinal_demo/add" method="post"><table><tr><td>標題:</td><td><input type="text" name="message.title" size="53">${titleMsg!}</td></tr><tr><td>留言內容</td><td><textarea name="message.content" cols="55" rows="15"></textarea>${contentMsg!}</td></tr><tr><td><input type="submit" value="提交"></td></tr></table></form></body></html>


6.edit.html

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body><h3>添加留言</h3><form action="/jfinal_demo/add" method="post"><table><tr><td>標題:</td><td><input type="text" name="message.title" size="53">${titleMsg!}</td></tr><tr><td>留言內容</td><td><textarea name="message.content" cols="55" rows="15"></textarea>${contentMsg!}</td></tr><tr><td><input type="submit" value="提交"></td></tr></table></form></body></html>



0 0
原创粉丝点击