Jfinal 初次尝试及 Jetty 使用相关问题

本贴最后更新于 2292 天前,其中的信息可能已经时移世易

Jetty 介绍

1. 使用

  • 配置:
    • jetty.version=9.2.7.v20150116
    • jfinal 是自己重新编译的 jfinal-java8 3.4-SNAPSHOT
  • 参考上面文档,但是有些更新:
echo $JETTY_HOME
/Users/aliyun/Downloads/softwoare/jetty-distribution-9.4.7.v20170914
ali-2cf0ee28bc54:jetty aliyun$ java -jar $JETTY_HOME/start.jar
ERROR : Nothing to start, exiting ...

Usage: java -jar $JETTY_HOME/start.jar [options] [properties] [configs]
       java -jar $JETTY_HOME/start.jar --help  # for more information
ali-2cf0ee28bc54:jetty aliyun$ pwd
/Users/aliyun/code/web/jetty
ali-2cf0ee28bc54:jetty aliyun$ JETTY_BASE=/Users/aliyun/code/web/jetty/
ali-2cf0ee28bc54:jetty aliyun$ java -jar $JETTY_HOME/start.jar
ERROR : Nothing to start, exiting ...

Usage: java -jar $JETTY_HOME/start.jar [options] [properties] [configs]
       java -jar $JETTY_HOME/start.jar --help  # for more information
ali-2cf0ee28bc54:jetty aliyun$ java -jar $JETTY_HOME/start.jar --add-to-startd=http,deploy
WARN  : --add-to-startd is deprecated! Instead use: --create-startd --add-to-start=http,deploy
MKDIR : ${jetty.base}/start.d
INFO  : webapp          transitively enabled, ini template available with --add-to-start=webapp
INFO  : server          transitively enabled, ini template available with --add-to-start=server
INFO  : security        transitively enabled
INFO  : servlet         transitively enabled
INFO  : http            initialized in ${jetty.base}/start.d/http.ini
INFO  : deploy          initialized in ${jetty.base}/start.d/deploy.ini
MKDIR : ${jetty.base}/webapps
INFO  : Base directory was modified
ali-2cf0ee28bc54:jetty aliyun$
建议使用:
Instead use: --create-startd --add-to-start=http,deploy

1.1 Jetty 开发

  • 基本用法:
  • 参考 solo 项目的用法及文章:Jetty 的工作原理
    • 在 solo 中使用了 WebAppContext,使用 web.xml 文件来定义一个 web 上下文,初始化一个基于 servlet 的 web 应用
//几个重要的类:Server,ServerConnector,HttpConfiguration,ServletContextHandler,WebAppContext
public class JettyTest{
  public static void main(String[] args) throws Exception{
    Server server = new Server(8888);
    server.start();
  }
}

2. Jfinal 使用问题

  • 接触 Jfinal 框架的时候发现它依赖的都是比较旧的类库,因为我本机上大部分 mvn 类库都有,不想占用电脑空间不想加载多种版本的类库了,所以更新了一下 maven 依赖。不明白为什么 Jfinal 没有更新一些类库,需要的同学可以参考以下问题来自己修改 Jfinal 的依赖和代码。
  • 使用发现的异常:
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jetty/server/nio/SelectChannelConnector
  • 原因时 Jfinal 依赖的 jetty 版本是 8.1.8,我本机已经有的依赖是 9.2.7.v20150116 版本,故重新 fork 了 jfinal 的源代码,pull 了新分支发现在类 com.jfinal.server.JettyServer 中使用了 9.2.7 版本中没有的类 org.eclipse.jetty.server.nio.SelectChannelConnector,参考别的 jetty 使用代码,修改 server 启动时配置端口的地方即可,去掉已经没有的依赖。还发现 jfinal 主线是 java1.6 的版本,,有一些地方提示代码有错,如没有加 @Override,异常处理等。于是修改了一些 IDE 报错,重新编译发布了 jfinal 的包,才好用。
java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I
  • javax-servlet-api-3.1.0 版本的类库里确实没有该方法,使用 mvn dependency:tree 来查看当前依赖,发现是 jfinal 中 velocity-tools 依赖了 servlet2.3 版本,如下:而我本机又没有新的依赖,所以在 maven 中去掉该版本依赖:
 +- org.apache.velocity:velocity-tools:jar:2.0:provided
[INFO] |  +- commons-beanutils:commons-beanutils:jar:1.7.0:provided
[INFO] |  +- commons-digester:commons-digester:jar:1.8:provided
[INFO] |  +- commons-chain:commons-chain:jar:1.1:provided
[INFO] |  +- commons-logging:commons-logging:jar:1.1:provided
[INFO] |  |  \- javax.servlet:servlet-api:jar:2.3:provided
[INFO] |  +- commons-validator:commons-validator:jar:1.3.1:provided

<dependency>
	<groupId>org.apache.velocity</groupId>
	<artifactId>velocity-tools</artifactId>
	<version>2.0</version>
	<scope>provided</scope>
	<exclusions>
		<exclusion>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
		</exclusion>
	</exclusions>
</dependency>
  • 参考基础教程 Jfinal 教程手册,使用 Jfinal 来写一个简单的 web 应用。与使用 django 的体验类似,没有 spring 繁杂的配置,几行代码就可以。现在 spring-boot 等也在向一个 main 函数启动应用的简化方向演化。
  • 使用 maven package 来打包应用。在输出的 target 目录下运行 main 方法,即可实现。
  • 运行成功结果:
ali-2cf0ee28bc54:jfinal-web-1.0.0-RELEASE aliyun$ java -cp WEB-INF/lib/*:WEB-INF/classes/ top.hunaner.JfinalApplication
Starting JFinal 3.3
2017-12-20 17:02:16.036:INFO::main: Logging initialized @293ms
Starting scanner at interval of 5 seconds.
Starting web server on port: 8006
2017-12-20 17:02:16.197:INFO:oejs.Server:main: jetty-9.2.7.v20150116
2017-12-20 17:02:16.462:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
2017-12-20 17:02:16.607:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@59494225{/,file:/Users/aliyun/Documents/GitHub/hello-world/code/jfinal/project/jfinalTest/target/jfinal-web-1.0.0-RELEASE/,AVAILABLE}
//注意这里说明了jfinal项目的地址,与单独使用jetty一致。
2017-12-20 17:02:16.641:INFO:oejs.ServerConnector:main: Started ServerConnector@55b9ae50{HTTP/1.1}{0.0.0.0:8006}
2017-12-20 17:02:16.642:INFO:oejs.Server:main: Started @901ms
Starting Complete. Welcome To The JFinal World :)
Before method invoking
After method invoking

JFinal action report -------- 2017-12-20 17:02:28 ------------------------------
Url         : GET /hello
Controller  : top.hunaner.controller.HelloController.(HelloController.java:1)
Method      : index
Interceptor : top.hunaner.interceptor.LogInterceptor.(LogInterceptor.java:1)
--------------------------------------------------------------------------------

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...