前两天听说了JFinal,简介甚是牛擦,号称java框架终结者,也是醉了,言归正传,我们就不研究他的宣传语了。不过他这个框架确实有他所说的一些特性,一起看看。
http://www.jfinal.com/
JFinal 是基于 Java 语言的极速 WEB + ORM 框架,其核心设计目标是开发迅速、代码量少、学习简单、功能强大、轻量级、易扩展、Restful。 在拥有Java语言所有优势的同时再拥有ruby、python、php等动态语言的开发效率!为您节约更多时间,去陪恋人、家人和朋友 :)
注:以上全部摘自官网,勿喷,该了解的还是要了解。
01.
<dependencies>
02.
<dependency>
03.
<groupId>junit</groupId>
04.
<artifactId>junit</artifactId>
05.
<version>
3.8
.
1
</version>
06.
<scope>test</scope>
07.
</dependency>
08.
<dependency>
09.
<groupId>com.jfinal</groupId>
10.
<artifactId>jetty-server</artifactId>
11.
<version>
8.1
.
8
</version>
12.
<scope>provided</scope>
13.
<exclusions>
14.
<exclusion>
15.
<artifactId>jetty-server</artifactId>
16.
<groupId>org.eclipse.jetty</groupId>
17.
</exclusion>
18.
</exclusions>
19.
</dependency>
20.
<dependency>
21.
<groupId>com.jfinal</groupId>
22.
<artifactId>jfinal</artifactId>
23.
<version>
1.9
</version>
24.
</dependency>
25.
<dependency>
26.
<groupId>c3p0</groupId>
27.
<artifactId>c3p0</artifactId>
28.
<version>
0.9
.
1.2
</version>
29.
</dependency>
30.
<dependency>
31.
<groupId>org.freemarker</groupId>
32.
<artifactId>freemarker</artifactId>
33.
<version>
2.3
.
20
</version>
34.
</dependency>
35.
<dependency>
36.
<groupId>log4j</groupId>
37.
<artifactId>log4j</artifactId>
38.
<version>
1.2
.
16
</version>
39.
</dependency>
40.
<dependency>
41.
<groupId>mysql</groupId>
42.
<artifactId>mysql-connector-java</artifactId>
43.
<version>
5.1
.
20
</version>
44.
</dependency>
45.
<dependency>
46.
<groupId>com.jfinal</groupId>
47.
<artifactId>cos</artifactId>
48.
<version>26Dec2008</version>
49.
</dependency>
50.
</dependencies>
3. web.xml添加如下代码
01.
<filter>
02.
<filter-name>jfinal</filter-name>
03.
<filter-
class
>com.jfinal.core.JFinalFilter</filter-
class
>
04.
<init-param>
05.
<param-name>configClass</param-name>
06.
<param-value>com.demo.jfinal.config.DemoConfig</param-value>
07.
</init-param>
08.
</filter>
09.
<filter-mapping>
10.
<filter-name>jfinal</filter-name>
11.
<url-pattern>/*</url-pattern>
12.
</filter-mapping>
4. 创建一个JFinal入口配置文件DemoConfig,需要继承JFinal的JFinalConfig类
01.
package
com.demo.jfinal.config;
02.
03.
import
java.util.Properties;
04.
05.
import
com.demo.jfinal.controller.BlogController;
06.
import
com.demo.jfinal.controller.HelloController;
07.
import
com.demo.jfinal.model.Blog;
08.
import
com.jfinal.config.Constants;
09.
import
com.jfinal.config.Handlers;
10.
import
com.jfinal.config.Interceptors;
11.
import
com.jfinal.config.JFinalConfig;
12.
import
com.jfinal.config.Plugins;
13.
import
com.jfinal.config.Routes;
14.
import
com.jfinal.core.JFinal;
15.
import
com.jfinal.ext.handler.ContextPathHandler;
16.
import
com.jfinal.plugin.activerecord.ActiveRecordPlugin;
17.
import
com.jfinal.plugin.c3p0.C3p0Plugin;
18.
import
com.jfinal.render.FreeMarkerRender;
19.
import
com.jfinal.render.ViewType;
20.
21.
/**
22.
* @author jack.song
23.
* @time 2014年12月26日
24.
*/
25.
public
class
DemoConfig
extends
JFinalConfig{
26.
27.
@Override
28.
public
void
configConstant(Constants arg0) {
29.
arg0.setDevMode(
true
);
//开发模式设置
30.
arg0.setBaseViewPath(
"template"
);
//页面模板根路径
31.
arg0.setFreeMarkerViewExtension(
".ftl"
);
//freemarker 模板后缀名
32.
}
33.
34.
@Override
35.
public
void
configHandler(Handlers arg0) {
36.
arg0.add(
new
ContextPathHandler(
"base"
));
//添加项目contextPath,以便在页面直接获取该值 ${base?if_exists}
37.
}
38.
39.
@Override
40.
public
void
configInterceptor(Interceptors arg0) {
41.
// TODO Auto-generated method stub
42.
43.
}
44.
45.
@Override
46.
public
void
configPlugin(Plugins me) {
47.
loadPropertyFile(
"common_config.txt"
);
//添加相关插件,读取配置文件
48.
C3p0Plugin c3p0Plugin =
new
C3p0Plugin(getProperty(
"jdbcUrl"
),
49.
getProperty(
"user"
), getProperty(
"pass<a href="
http:
//www.it165.net/edu/ebg/" target="_blank" class="keylink">word</a>"));
50.
me.add(c3p0Plugin);
51.
ActiveRecordPlugin arp =
new
ActiveRecordPlugin(c3p0Plugin);
52.
me.add(arp);
53.
arp.addMapping(
"blog"
, Blog.
class
);
//添加相关model配置
54.
}
55.
56.
@Override
57.
public
void
configRoute(Routes arg0) {
58.
arg0.add(
"/"
, HelloController.
class
);
//添加项目路由,类似于struts的action 配置文件,此处配置controller名,action名称对应方法名
59.
arg0.add(
"/blog"
, BlogController.
class
);
60.
}
61.
62.
public
void
afterJFinalStart() {
63.
Properties p = loadPropertyFile(
"freemarker.properties"
);
//由于我们用到freemarker,所以在此进行freemarker配置文件的装载
64.
FreeMarkerRender.setProperties(p);
65.
}
66.
/**
67.
* 建议使用 JFinal 手册推荐的方式启动项目
68.
* 运行此 main 方法可以启动项目,此main方法可以放置在任意的Class类定义中,不一定要放于此
69.
*/
70.
public
static
void
main(String[] args) {
71.
JFinal.start(
"src/main/webapp"
,
80
,
"/"
,
5
);
//启动配置项
72.
}
73.
}
5. 上一步中为JFinal的主要入口类,我们必须创建它,并进行相应的配置改动,其中的common_config.txt,freemarker.properties附下
1.
##common_config.txt
2.
jdbcUrl = jdbc:mysql:
//127.0.0.1/jfinal_demo?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
3.
user = root
4.
pass<a href=
"http://www.it165.net/edu/ebg/"
target=
"_blank"
class
=
"keylink"
>word</a> = password
01.
#freemarker.properties
02.
default_encoding=utf-
8
03.
#
for
production environment
04.
template_exception_handler=ignore
05.
06.
#
for
testing
07.
#template_exception_handler=DEBUG
08.
09.
template_update_delay=
5
10.
auto_import=/macros/demo.ftl as demo
11.
12.
13.
locale=no_NO
14.
datetime_format=yyyy-MM-dd HH:mm:ss
15.
date_format=yyyy-MM-dd
16.
number_format=#.##
6. 创建第一个controller,需要继承JFinal的controller 类
01.
package
com.demo.jfinal.controller;
02.
03.
import
com.jfinal.core.Controller;
04.
05.
/**
06.
* @author jack.song
07.
* @time 2014年12月26日
08.
*/
09.
public
class
HelloController
extends
Controller {
10.
11.
public
void
index(){
12.
render(
"/index.ftl"
);
13.
}
14.
}
7. 创建index.ftl
01.
<
@demo
.layout>
02.
<h3>JFinal Demo 项目首页</h3>
03.
<div
class
=
"table_box"
>
04.
<p>欢迎来到 JFinal极速开发世界!</p>
05.
06.
<br><br><br>
07.
08.
本Demo采用FreeMarker 作为视图文件,您还可以使用Jsp、Velocity或自定义类型视图。
09.
点击<a href=
"${base?if_exists}/blog"
><b>此处</b></a>开始试用Demo。
10.
11.
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
12.
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
13.
</div>
14.
</
@demo
.layout>
01.
<#assign staticCssPath=
"${base}/static/css/"
/>
02.
<#assign staticImgPath=
"${base}/static/img/"
/>
03.
<#macro layout>
04.
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
05.
<html xml:lang=
"zh-CN"
xmlns=
"http://www.w3.org/1999/xhtml"
lang=
"zh-CN"
>
06.
<head>
07.
<meta http-equiv=
"content-type"
content=
"text/html; charset=UTF-8"
/>
08.
<link href=
"${staticCssPath}manage.css"
media=
"screen"
rel=
"stylesheet"
type=
"text/css"
/>
09.
</head>
10.
<body>
11.
<div
class
=
"manage_container"
>
12.
<div
class
=
"manage_head"
>
13.
<div
class
=
"manage_logo"
><a href=
"http://www.jfinal.com"
>JFinal web framework</a></div>
14.
<div id=
"nav"
>
15.
<ul>
16.
<li><a href=
"${base?if_exists}/"
><b>首页</b></a></li>
17.
<li><a href=
"${base?if_exists}/blog"
><b>Blog管理</b></a></li>
18.
</ul>
19.
</div>
20.
</div>
21.
<div
class
=
"main"
>
22.
<#nested>
23.
</div>
24.
</div>
25.
</body>
26.
</html>
27.
</#macro>
28.
29.
<#macro paginate currentPage totalPage actionUrl urlParas=
""
>
30.
<#
if
(totalPage <=
0
) || (currentPage > totalPage)><#
return
></#
if
>
31.
<#local startPage = currentPage -
4
>
32.
<#
if
(startPage <
1
)><#local startPage =
1
></#
if
>
33.
34.
<#local endPage = currentPage +
4
>
35.
<#
if
(endPage > totalPage)><#local endPage = totalPage></#
if
>
36.
37.
<div
class
=
"pagination"
>
38.
<#
if
(currentPage <=
8
)>
39.
<#local startPage =
1
>
40.
</#
if
>
41.
<#
if
((totalPage - currentPage) <
8
)>
42.
<#local endPage = totalPage>
43.
</#
if
>
44.
45.
<#
if
(currentPage ==
1
)>
46.
上页
47.
<#
else
>
48.
<a href=
"${actionUrl}#{currentPage - 1}${urlParas!}"
class
=
"prev_page"
>上页</a>
49.
</#
if
>
50.
51.
<#
if
(currentPage >
8
)>
52.
<a href=
"${actionUrl}#{1}${urlParas!}"
>#{
1
}</a>
53.
<a href=
"${actionUrl}#{2}${urlParas!}"
>#{
2
}</a>
54.
…
55.
</#
if
>
56.
57.
<#list startPage..endPage as i>
58.
<#
if
currentPage == i>
59.
#{i}
60.
<#
else
>
61.
<a href=
"${actionUrl}#{i}${urlParas!}"
>#{i}</a>
62.
</#
if
>
63.
</#list>
64.
65.
<#
if
((totalPage - currentPage) >=
8
)>
66.
…
67.
<a href=
"${actionUrl}#{totalPage - 1}${urlParas!}"
>#{totalPage -
1
}</a>
68.
<a href=
"${actionUrl}#{totalPage}${urlParas!}"
>#{totalPage}</a>
69.
</#
if
>
70.
71.
<#
if
(currentPage == totalPage)>
72.
下页
73.
<#
else
>
74.
<a href=
"${actionUrl}#{currentPage + 1}${urlParas!}"
class
=
"next_page"
rel=
"next"
>下页</a>
75.
</#
if
>
76.
</div>
77.
</#macro>
9. 启动
eclipse用run as 配置Main class为com.jfinal.core.JFinal
上述就是一些简单的部署启动步骤,由于是入门级,所以比较简单,一些代码也为直接放出,但是在此给出整个demo的代码,大家可以下载直接浏览
demo实例下载地址:TestJFinal.rar
总结:初步感觉JFinal确实比较快速,易学,以及一站化(前端+ORM),但是有比较大的侵入性,比如必须继承之类的
出处http://blog.csdn.net/abudexiatian/article/details/42234617