zoukankan      html  css  js  c++  java
  • JFinal开发8个常见问题

    下面是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之类的。
  • 相关阅读:
    AQS笔记二 ---- 使用AQS自定义锁
    AQS笔记一 --- 源码分析
    ElasticSearch(二十一)正排和倒排索引
    ElasticSearch(二十)定位不合法的搜索及其原因
    ElasticSearch(十八)初识分词器
    ElasticSearch(十七)初识倒排索引
    还在用 kill -9 停机?这才是最优雅的姿势(转)
    一文快速搞懂MySQL InnoDB事务ACID实现原理(转)
    你真的理解零拷贝了吗?(转)
    关于分布式事务的读书笔记
  • 原文地址:https://www.cnblogs.com/chen-lhx/p/4855997.html
Copyright © 2011-2022 走看看