• JFinal开发web项目出现故障小记


    导读

    作为中国优秀的开源项目之中的一个JFinal有着极速开发的优点,是中小型应用开发的首选。在导师的建议下。我使用了JFinal来开发一个Java服务端应用,官方教程非常easy。就几十页(当然是中文的),学起来非常快。

    然而在开发过程中肯定会遇到这样那样“无厘头”的问题,怎样使用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包

    參考资料

  • 相关阅读:
    如何在winform的numericUpDown中显示小数点
    Jquery attr 和removeAttr 的简单使用
    Linux下的多进程编程初步(转载)
    扩展GCD和线性模方程组
    05、Flutter常用组件
    12、Flutter组件装饰
    10、Flutter资源和图片
    09、Flutter手势控制
    04、FlutterDart语法
    07、FluterCupertino
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5211339.html
Copyright © 2020-2023  润新知