zoukankan      html  css  js  c++  java
  • jfinal常见问题

    2014年的时候,学过一段时间的JFinal,当时主要是了解这个框架,研究了下源码,看懂了部分。
    今天,2015年2月7日,弄了一下午的JFinal,把未来要上线的一个官网项目,迁移到了JFinal。
    下面是8个最常见的问题总结。 

    1.Can not create instance of class: demo.DemoConfig.
    觉得应该是你的路径有问题, 打开你项目的java build path面板, 然后找到default output folder, 把这里的输出改为your_project/WebRoot/WEB-INF/classes。
     
    2.jfinal自带demo中如何在_layout.html加行<base href="${CONTEXT_PATH!}/"/>
     
    按照如下步骤可解决问题:
     
    在JFinalConfig中添加该ContextPathHandler,代码如下
     
    public void configHandler(Handlers me) {
        me.add(new ContextPathHandler());
    }
    在_layout.html 的 head标记中添加 base 标记,代码如下
    <base href="${CONTEXT_PATH}/" />
    修改页面中的链接标签 a ,将最前面的 "/" 去掉,以下是要改的地方,可能有遗漏
    比如:<link rel="stylesheet" type="text/css" href="static/framework/bootstrap/css/bootstrap.css" />
     
    本质上来说context_path的问题仅与view有关,以上是JFinal提供的简单处理方案 :)
     
    3.如果更改JFinal的web.xml 拦截后缀名。
    <filter-mapping>
          <filter-name>jfinal</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
     “/*”不能正确出力“.html”这种后缀的动态请求。
     
     参考资料:
     新增一个HtmSkipHandler文件
     public class HtmSkipHandler extends Handler {  
        public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {  
            int index = target.lastIndexOf(".htm");  
            if (index != -1)  
            target = target.substring(0, index);  
            nextHandler.handle(target, request, response, isHandled);  
        }  
    }
     
    再在JfinalConfig文件增加
    /**
         * 配置处理器
         */
        public void configHandler(Handlers me) {
            me.add(new HtmSkipHandler());
        }
     
    4. URL中的参数,没有在上下文中。
    访问1个url,http://localhost/news/list.html?categoryId=2
    Freemarker页面${categoryId}竟然报错。
    必须在Controller的方法中,手动设置才行:
    setAttr("categoryId",categoryId);
     
    5.JFinal中restful拦截器如何实现。
    jfinal中有restful拦截器,直接添加就是了。
    /**
     * 配置全局拦截器
     */
    public void configInterceptor(Interceptors me) {
     me.add(new Restful());
    }
     
    URL:http://localhost/news/2
    获得参数:Integer id = getParaToInt(0);
     
    但是,JFinal自带的Restful拦截器是写死的,比如"http://localhost/news/2"这个url只能这么写,
    响应方法只能是show,而在SpringMVC中,可以很灵活,比如“/detail/{newsId}”,方法名随便取。
     
    6.JFinal设置404和500等页面。
    public void configConstant(Constants me) {
    me.setError404View(TEMPLATE_PATH+"/error/404.html");
    me.setError500View(TEMPLATE_PATH+"/error/500.html");
    }
     
    7.JFinal统一异常处理。
    public class ExceptionInterceptor implements Interceptor 
      public void intercept(ActionInvocation ai) {
    Controller controller = ai.getController();
    HttpServletRequest request = controller.getRequest();
     
    try {
    ai.invoke();
    } catch (Exception e) {
    }
    }
     
    /**
     * 配置全局拦截器
     */
    public void configInterceptor(Interceptors me) {
    me.add(new GlobalInterceptor());
    me.add(new Restful());
    me.add(new ExceptionInterceptor());
    }
     
    8.JFinal中配置Log4j。

    源代码src目录下放置log4j.properties或log4j.xml,都行,xml格式也不需要额外配置listener之类的。

  • 相关阅读:
    初探linux子系统集之led子系统(一)
    金融大数据分析从认知到实践(第1辑)(套装共3册)
    Blender权威指南
    零基础学Python
    中文版Photoshop CC图像处理与设计
    21天学通C++(第4版)
    Python应用开发实战
    电脑办公实战从入门到精通(超值版)
    Windows 10从新手到高手
    21天学通C语言(第7版)
  • 原文地址:https://www.cnblogs.com/timssd/p/5207955.html
Copyright © 2011-2022 走看看