首页 > 代码库 > JFinal开发web项目出现问题小记

JFinal开发web项目出现问题小记

导读

作为中国优秀的开源项目之一JFinal有着极速开发的好处,是中小型应用开发的首选。在导师的建议下,我使用了JFinal来开发一个Java服务端应用,官方教程非常简单,就几十页(当然是中文的),学起来很快。然而在开发过程中肯定会遇到这样那样“无厘头”的问题,如何使用JFinal开发参考官方文档或网上某些人的教程即可,我再重写一遍也无多大意义,我这篇文章仅记录自己在使用JFinal开发过程中遇到的问题及注意事项,这样更有参考价值,也希望大家在遇到类似的问题后能及时找到解决方案,提高开发效率。

JFinal开发问题集锦

1. 中文乱码问题

这是几乎每个进行Java web开发的程序员都遇到过的问题,不同技术或框架处理的方式可能不太一样,对JFinal来说,有以下几个配置字符集的地方:

(1)Config配置:

YourJFinalConfig.configConstant(Constants me) {me.setEncoding("utf-8")};

(2)数据库连接配置:

jdbcUrl = jdbc:mysql://127.0.0.1/jfinal?characterEncoding=utf8

(3)如果是页面发送 GET 请求,中文字符需要 encoding,除非在 tomcat 的 server.xml 中的 Connector 标记中添加了 URIEncoding="UTF-8" 属性

(4)如果是 jsp 页面:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
pageEncoding="UTF-8"%>

(5)如果是其它 html 页面:

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

(6)另外 eclipse workspace 的字符编码最好也配置成 utf-8

2. “Illegal access”问题

出现这种问题的原因很多,比如我遇到的有以下3种:

情况1INFO: Illegal access: this web application instance has been stopped already. 
Could not load com.mysql.jdbc.authentication.MysqlOldPasswordPlugin. The eventual
following stack trace is caused by an error thrown for debugging purposes as well
as to attempt to terminate the thread which caused the illegal access, and has no 
functional impact.

情况2INFO: Illegal access: this web application instance has been stopped already. 
Could not load com.mchange.v2.c3p0.ConnectionCustomizer. The eventual following 
stack trace is caused by an error thrown for debugging purposes as well as to 
attempt to terminate the thread which caused the illegal access, and has no 
functional impact.

情况3INFO: Illegal access: this web application instance has been stopped already. 
Could not load com.mchange.v2.resourcepool.BasicResourcePool$1. The eventual 
following stack trace is caused by an error thrown for debugging purposes as well 
as to attempt to terminate the thread which caused the illegal access, and has no 
functional impact.

解决方法: 查看上述日志,可能会很明显的看到下面很多的exception。都是this web application instance has been stopped already以及Could not load XX Class。但是不要被这些异常迷惑,他们只是结果而不是原因。那么如果查找原因,可以通过查找error-debug日志文件来解决。

error-debug日志文件日志文件默认是不生成的,我们需要通过log配置文件来设置让其生成,具体方法如下:
(1)在WEB-INF/classes目录下新建logging.properties,内容如下:

handlers = org.apache.juli.FileHandler, java.util.logging.ConsoleHandler    

############################################################    
# Handler specific properties.    
# Describes specific configuration info for Handlers.    
############################################################    

org.apache.juli.FileHandler.level = FINE    
org.apache.juli.FileHandler.directory = ${应用目录}/logs    
org.apache.juli.FileHandler.prefix = error-debug.    

java.util.logging.ConsoleHandler.level = FINE    
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter    

(2)重新启动应用,指定目录下会有一个error-debug.yyyy-mm-ss.log的错误日志,通过分析上述日志可以找到问题发生的真正原因,然后进行解决。

3. “Plugin start error”问题

java.lang.RuntimeException: Plugin start error: com.jfinal.plugin.activerecord.ActiveRecordPlugin.
java.sql.SQLException: Connections could not be acquired from the underlying database!
        at com.jfinal.core.Config.startPlugins(Config.java:95)
        at com.jfinal.core.Config.configJFinal(Config.java:48)
        at com.jfinal.core.JFinal.init(JFinal.java:67)
        at com.jfinal.core.JFinalFilter.init(JFinalFilter.java:49)
        at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:277)
        at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:258)
        at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:382)
        at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:103)
        at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4649)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5305)

解决方法: 删除跟jetty相关的jar包,因为我是在Tomcat下部署的,而JFinal自带的是jetty服务器。

4. 启动时老是出现“jfinal config it in web.xml”错误

解决方法: 删除web.xml中默认生成的下面这段:

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

5. 缺少c3p0的jar包会出现下面错误:

java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/ComboPooledDataSource
 at com.jfinal.plugin.c3p0.C3p0Plugin.start(C3p0Plugin.java:145)
 at com.jfinal.core.Config.startPlugins(Config.java:85)
 at com.jfinal.core.Config.configJFinal(Config.java:48)
 at com.jfinal.core.JFinal.init(JFinal.java:67)
 at com.jfinal.core.JFinalFilter.init(JFinalFilter.java:49)
 at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
 at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
 at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
 at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4809)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5485)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
 at java.util.concurrent.FutureTask.run(FutureTask.java:138)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
 at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: com.mchange.v2.c3p0.ComboPooledDataSource
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
 ... 18 more

而缺少与c3p0一起的mchange-commons-java-0.2.7.jar包,会出现下面错误:

java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector
 at com.jfinal.plugin.c3p0.C3p0Plugin.start(C3p0Plugin.java:145)
 at com.jfinal.core.Config.startPlugins(Config.java:85)
 at com.jfinal.core.Config.configJFinal(Config.java:48)
 at com.jfinal.core.JFinal.init(JFinal.java:67)
 at com.jfinal.core.JFinalFilter.init(JFinalFilter.java:49)
 at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
 at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
 at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
 at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4809)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5485)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
 at java.util.concurrent.FutureTask.run(FutureTask.java:138)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
 at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: com.mchange.v2.ser.Indirector
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
 ... 18 more

6. freemarker相关的jar包缺乏问题:

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
 at com.jfinal.core.ActionInvocation.invoke(ActionInvocation.java:61)
 at com.jfinal.core.ActionHandler.handle(ActionHandler.java:77)
 at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:72)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
 at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
 at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
 at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
 at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.reflect.InvocationTargetException
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at com.jfinal.core.ActionInvocation.invoke(ActionInvocation.java:55)
 ... 19 more
Caused by: java.lang.NoClassDefFoundError: freemarker/template/TemplateException
 at com.jfinal.render.RenderFactory$FreeMarkerRenderFactory.getRender(RenderFactory.java:233)
 at com.jfinal.render.RenderFactory.getRender(RenderFactory.java:127)
 at com.jfinal.core.Controller.render(Controller.java:892)
 at com.demo.controller.StudentController.index(StudentController.java:16)
 ... 24 more
Caused by: java.lang.ClassNotFoundException: freemarker.template.TemplateException
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
 ... 28 more

解决方法: JFinal 默认使用 FreeMarker作为 View,故需freemarker的jar包

参考资料

  • JFinal 学习笔记一(config route配置,controller配置)
  • Tomcat启动失败问题解决

JFinal开发web项目出现问题小记