JFinal开发8个常见问题

来源:互联网 发布:女士牛仔裤品牌 知乎 编辑:程序博客网 时间:2024/05/02 01:36
  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。
参考资料:http://www.oschina.net/question/53137_124287

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提供的简单处理方案 :)

参考资料:http://www.oschina.net/question/260040_45773

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之类的。

小雷FansUnion
2015年2月8日
湖北-武汉-循礼门 
1 0
原创粉丝点击