初学Jfinal

初学框架 第一个就是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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值