java学习

java学习

 

jfinal笔记2

基于JFinal 的web项目需要创建一个 继承自 JFinalConfig类的 子类 ,该类用 于对整个  web项目进行配置 项目进行配置 。
JFinalConfig 子类需要实现 五个抽象方法 ,如:
public class DemoConfig extends JFinalConfig {
public void configConstant(Constants me) {}
public void configRoute(Routes me) {}
public void configPlugin(Plugins me) {}
public void configInterceptor(Interceptors me) {}
public void configHandler(Handlers me) {}
}

configConstant
此方法用来配置 JF inal 常量 值,如开发模式 devMode 的配置,默认 视 图类型 ViewType 的配置 的配置 ,如下 代码 配置了 JFinal 运行在开发模式下且默认视图 类型为 JSP:
public void configConstant(Constants me) {
me.setDevMode(true);
me.setViewType(ViewType.JSP);
}


在开发模式下 ,JFinal会对每次 请求输出报告,如本会对每次 请求输出报告,如本请求的 Controller、 Method 以及请求所携带的参数。 以及请求所携带的参数。JFinal 支持 JSP 、 FreeMarker、Velocity三种常 用视图 。
configRoute
此方法用来配置 JF inal 访问 路由  ,如下 代码 配置了 将 ”/hello” 映射到 HelloController  这个控制器 , 通过以下的配置,http://localhost/hello将访问HelloController.index()方法,而
http://localhost/hello/other将访问到HelloController.other()方法.
字符串与控制类的映射是:
public void configRoute(Routes me) {
me.add("/hello", HelloController.class);
}
Routes 类主要 有如下 两个 方法:
public Routes add(String controllerKey, Class<? extends Controller> controllerClass, String viewPath)
public Routes add(String controllerKey, Class<? extends Controller> controllerClass)


第一个参数 controllerKey是指访问某个 Controller所需要的一个字符串 ,该 字符串唯一对应个 Controller,controllerKey仅能定位到 仅能定位到 Controller。第二个参 数 controll er Class 是该 controllerKey所对应 到的 Controller。第三个参数 view Path 是指 该 Controller返回的视图  的相对路径。当 view Path未指定时默认值为 controllerKey。
1.当url是http://localhost/controllerKey时,调用的是对应控制类的index()方法;
当需要传参数时,url这样写:http://localhost/controllerKey/a-b-c,参数之间用中横线分开,
index()方法中调用getPara(i)得到参数,i是参数对应的下标,例如a的下标是0,b的下标是1,c的下标是2.
2.当url是http://localhost/controllerKey/method时,调用的是对应控制类的method()方法;
3.
JFinal 在以上路由 规则之外 还提供了 ActionKey注解, 可以打破 原有 规则, 以下是代码示例 :
public class HelloController extends Controller{
    @ActionKey("second")
    public void second(){
        System.out.println("0="+getPara(0));
        System.out.println("1="+getPara(1));
        System.out.println("2="+getPara(2));
        renderText("yjw");
    }
}
这样url可以写成http://localhost/second/1-2-3,不用写控制类的映射了。
4.
如果以上所有路由规则都不能满足需求,开发者还可根据要使用 Handler定制更加个性化的路由,大体思就是在Handl er 中改变第一个参数 String target的值。

posted on 2013-01-08 15:45 杨军威 阅读(3755) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜