JFinal开发8个常见问题

下面是8个最多见的问题总结。 css

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之类的。
相关文章
相关标签/搜索