Jfinal 框架简介:
JFinal 框架:
MVC架构,设计精巧,使用简单
遵循COC原则,零配置,无xml
独创Db + Record模式,灵活便利
ActiveRecord支持,使数据库开发极致快速
自动加载修改后的java文件,开发过程中无需重启web server
AOP支持,拦截器配置灵活,功能强大
Plugin体系结构,扩展性强
多视图支持,支持FreeMarker、JSP、Velocity
强大的Validator后端校验功能
功能齐全,拥有struts2的绝大部分功能
体积小仅339K,且无第三方依赖
个人看法:
JFinal 框架==Struts2+Hibernate4+Spring4+ThinkPhp (部分功能的合成版。)
第一天配置 上手入门项目
HelloWorld项目
步骤:
1放入JFinal 库文件。
2修改web.xml 文件.
3创建MyConfig.java文件。
4 创建HelloController.java文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<filter>
<filter-name>jfinal</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
<init-param>
<param-name>configClass</param-name>
<param-value>com.weichengtan.jfandroid.config.MyConfig</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
-----------
学过web的都知道,在web.xml 里配置的东西的作用了。
先了解web.xml文件是用来干什么的。web.xml文件是用来配置:欢迎页、servlet、filter等的。当你的web工程没用到这些时,你可以不用web.xml文件来配置你的web工程。
package com.weichengtan.jfandroid.config;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.render.ViewType;
import com.weichengtan.jfandroid.controller.HelloController;
/**
* @author:tanweicheng
*
*/
public class MyConfig extends JFinalConfig {
@Override
public void configConstant(Constants me) {
//配置开发模式下默认视图jsp
me.setDevMode(true);
me.setViewType(ViewType.JSP);
}
/**
*1 此方法配置JFinal访问路由,将/hello映射到HelloController中去。
*
*/
@Override
public void configRoute(Routes me) {
me.add("/hello", HelloController.class);
}
@Override
public void configPlugin(Plugins me) {
}
@Override
public void configInterceptor(Interceptors me) {
}
@Override
public void configHandler(Handlers me) {
}
}
创建一个MyConfig文件,extends JFinalConfig。
最后
创建HelloController.java
package com.weichengtan.jfandroid.controller;
import com.jfinal.core.Controller;
/**
* @author:tanweicheng
*
*/
public class HelloController extends Controller {
/**
* @method:默认走的方法。
*/
public void index() {
renderText("Hello World");
}
public void login() {
renderText("this is login()");
}
}
部署tomcat7以上。
1 http://localhost:8080/JFinalAsAndroid/hello/login
2http://localhost:8080/JFinalAsAndroid/hello
就可以看到效果了。比较简单。
public void configRoute(Routes me) {
me.add(“/hello”, HelloController.class);
}
Routes类是什么鬼??
Rotes—直接翻译路线。官方叫路由。
package com.weichengtan.jfandroid.yuanma;
/**
* @author:tanweicheng
*
*/
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.jfinal.core.Controller;
/**
* Routes.
*/
public abstract class Routes {
private static String baseViewPath;
// Map 集合 key-value键值对。
private Map<String, Class<? extends Controller>> map = new HashMap<String, Class<? extends Controller>>();
private Map<String, String> viewPathMap = new HashMap<String, String>();
/**
* you must implement config method and use add method to config route
* 你需要实现config方法配置路由。
*/
public abstract void config();
public Routes add(Routes routes) {
if (routes != null) {
// 当你调用add方法时候。然后调用 抽象方法---->very important!!!
routes.config();
// for循环输出集合。
for (Entry<String, Class<? extends Controller>> e : routes.map
.entrySet()) {
// 然后获取对应的key
String controllerKey = e.getKey();
if (this.map.containsKey(controllerKey)) {
// 抛出一个异常--->字面 非法参数 通常情况为找不到对应key等
throw new IllegalArgumentException(
"The controllerKey already exists: "
+ controllerKey);
}
// 然后通过集合保存对应的key-value 键值对。
this.map.put(controllerKey, e.getValue());
// 视图路径集合保存--->对应的键值对。
this.viewPathMap.put(controllerKey,
routes.getViewPath(controllerKey));
}
}
// 最后返回这个对象。
return this;
}
/**
* Add route
*
* @param controllerKey
* A key can find controller
* 第一个参数controllerkey-->指访问Controller所需要的一个字符串。
* 该字符串对应唯一一个Controller,controllerKey只能定位到Controller
*
* @param controllerClass
* Controller Class
* 第二个参数controllerClass--->指controllerClass是该controllerKey
* 所对应到的Controller。 (反射机制实现的东西。)
*
* @param viewPath
* View path for this Controller 第三个 参数 viewPath
* 是指该Controller返回的路径。
*
*
*
*
*/
public Routes add(String controllerKey,
Class<? extends Controller> controllerClass, String viewPath) {
if (controllerKey == null)
throw new IllegalArgumentException(
"The controllerKey can not be null");
controllerKey = controllerKey.trim();
if ("".equals(controllerKey))
throw new IllegalArgumentException(
"The controllerKey can not be blank");
if (controllerClass == null)
throw new IllegalArgumentException(
"The controllerClass can not be null");
if (!controllerKey.startsWith("/"))
controllerKey = "/" + controllerKey;
if (map.containsKey(controllerKey))
throw new IllegalArgumentException(
"The controllerKey already exists: " + controllerKey);
map.put(controllerKey, controllerClass);
// 判断是否为空。
if (viewPath == null || "".equals(viewPath.trim())) // view path is
// 当viewPath 没有指定的时候,默认值为controllerKey。
viewPath = controllerKey;
viewPath = viewPath.trim();
//没有“/” 就自动加个开始。
if (!viewPath.startsWith("/")) // "/" added to prefix
viewPath = "/" + viewPath;
//没有“/” 就自动加个结束。
if (!viewPath.endsWith("/")) // "/" added to postfix
viewPath = viewPath + "/";
if (baseViewPath != null) // support baseViewPath
viewPath = baseViewPath + viewPath;
viewPathMap.put(controllerKey, viewPath);
return this;
}
/**
* Add url mapping to controller. The view path is controllerKey
*
* @param controllerKey
* A key can find controller
* @param controllerClass
* Controller Class
*/
//调用三个参数的方法。
public Routes add(String controllerKey,
Class<? extends Controller> controllerClass) {
return add(controllerKey, controllerClass, controllerKey);
}
public Set<Entry<String, Class<? extends Controller>>> getEntrySet() {
return map.entrySet();
}
//提供一个getViewPath方法--->获取路径。
public String getViewPath(String key) {
return viewPathMap.get(key);
}
/**
* Set the base path for all views (为所有视图设置 base path) 静态方法
*/
static void setBaseViewPath(String baseViewPath) {
if (baseViewPath == null)
throw new IllegalArgumentException(
"The baseViewPath can not be null");
baseViewPath = baseViewPath.trim();
if ("".equals(baseViewPath))
throw new IllegalArgumentException(
"The baseViewPath can not be blank");
if (!baseViewPath.startsWith("/")) // add prefix "/"
baseViewPath = "/" + baseViewPath;
if (baseViewPath.endsWith("/")) // remove "/" in the end of baseViewPath
baseViewPath = baseViewPath.substring(0, baseViewPath.length() - 1);
Routes.baseViewPath = baseViewPath;
}
//退出时清空。回收内存。
public void clear() {
map.clear();
viewPathMap.clear();
map = null;
viewPathMap = null;
}
}
欢迎讨论。哈哈哈哈。