zoukankan      html  css  js  c++  java
  • Jfianl

    http://www.oschina.net/question/257183_149268-----------

    添加Handler: me.add(new ContextPathHandler("ctx")),然后在页面中就可以 ${ctx} 来用

    代码如下:找不到代码写在哪里的,请在整个工程中搜索在CODE上查看代码片派生到我的代码片

    1. /** 
    2.  * 配置处理器 
    3.  */  
    4. public void configHandler(Handlers me) {  
    5.     me.add(new ContextPathHandler("ctx"));        
    6. }  

    Jsp中直接引用就可以了:

      1. <link href="${ctx}/css/login.css" rel="stylesheet" type="text/css" /> 

    JFinal的Interceptor、Controller、Render、Plugin,Handler是对Controller和Interceptor的补充。ORM使用的是Db+ActiveRecord。

    Config是基本的配置。

    在Config这个类中,Routes、Interceptors、Handlers均以成员变量的形式存在。

    Routes
    在Route中有两个Map:map和viewPathMap。
    map中放置的是controllerKey和controllerClass的键值对。
    viewPathMap中放置的是controllerKey和viewPath的键值对。

    在initActionMapping中将Routes和Interceptors组织起来。其中,在initActionMapping中有一个重要的方法:buildActionMapping

    JFinalFilter中的doFilter。在doFilter中主要是调用了handle方法。

    https://segmentfault.com/a/1190000003028991

    http://www.cnblogs.com/istianyu/p/3267012.html-----------jfinal框架教程-学习笔记(二)

     Handler 会接管所有请求,包括静态请求,如 localhost/css/style.css 或者 locahost/img/logo.jpg,并且可以改变请求所指向的资源参数如 String target参数,所以 Handler 具有可以处理更多的事情,例如改变 target 参数做url 伪静态或者 url 重写。 而拦截器只能拦截对 action 的请求,对静态资源的请求是完全无法感知的,并且拦截器无法改变 starget 参数,当拦截发生时,很多事情已经确定了,例如拦截的 controller 和 action 都确定了。

         Controller 是请求希望到达的最终目标,通常只做三件事:接收参数、调用业务、使用业务结果渲染页面。Handler、Interceptor 辅助 Controller 解决切面问题,例如权限验证、事务处理等等。

    也就是说jfinal handler相当于servlet中的filter?

    当 action 中没有调用 render 时,jfinal 会默认给你调用一个 render(view),其中
    view = baseViewPath + viewPath + methodName + 模板扩展名

    1:baseViewPath 通过 me.setBaseViewPath(...) 进行配置
    2:viewPath 是在配置路由的时候通过 me.add(controllerKey,controllerClass, viewPath) 方法的第三个参数进行配置,如果第三个参数未指定则默认与 controllerKey值相同
    3:methodName 就是 controller 中的方法名
    4:当 viewPath 以"/" 打头时 baseViewPath 不起作用,这样设计是让 viewPath有机会回到根路径

    需求如下:

    动态生成的静态html页面在webapp下的doc目录中,但是在doc下的目录级数不固定,是根据文件名字动态生成的,如名字为index,则生成的html页面为:webapp/doc/index.html;名字为2014-02-23-index,则生成的html页面为:webapp/doc/2014/02/23/index.html。以此类推。

    有一个HtmlController接收/doc的请求,可以renderJsp("/doc/")跳转到doc目录下,

    但是现在如何根据不同的/doc的请求返回不同的目录呢?如
    请求:/doc/2014,renderJsp("/doc/2014");
    请求:/doc/2014/02,renderJsp("/doc/2014/02");
    以此类推。

    我记得spring mvc有类似的配置通配的功能,Jfinal能实现类似需求吗

    url目前不支持通配符,像你说的需求可以通过参数的不同render不同的渲染,或者你可以写自己的handler来处理你的全局请求和响应

     JFinal Handler 可以无限扩展路由规则,因为它可以改变到达的 url 值,该值在 Handler 类中是 String target。具体点是创建一个 DocHandler 和一个 DocController 配合处理,大致代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    public class DocHandler extends Handler {
      public void handle(String target,HttpServletRequest request,HttpServletResponse response,boolean[] isHandled) {
      if (target.startsWith("/doc")) {
        target = target.substring(0, 4);
        request.setAttribute("view", target.substring(4));
      }
      nextHandler.handle(target, request, response, isHandled);
    }
     
     

    DocController 就更简单了,一行代码搞定:其实还可以更省代码:

    1
    2
    3
    4
    5
    6
    7
    public class DocHandler extends Handler {
      public void handle(String target,HttpServletRequest request,HttpServletResponse response,boolean[] isHandled) {
      if (target.startsWith("/doc")) {
        RenderFactory.me().getJspRender(target.substring(4)).setContext(request, response).render();
        isHandled[0] = true;
      }
    }

    这样就连 DocController 都不需要了

    1
    render(getAttr("view"));
  • 相关阅读:
    Supervisor安装与使用
    windows常用快捷键和指令
    搜索引擎使用技巧
    golang核心Goroutine和channel
    4、小程序原生底部菜单
    三、小程序值使用vant开发
    axios请求2
    3、小程序消息推送
    居中
    一、底部菜单
  • 原文地址:https://www.cnblogs.com/SZLLQ2000/p/5767837.html
Copyright © 2011-2022 走看看