首页 / 操作系统 / Linux / JFinal开发8个常见问题
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=2Freemarker页面${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之类的。
JFinal的详细介绍:请点这里
JFinal的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-02/113421.htm