JFinal版本:jfinal-1.9.jar
Maven:apache-maven-3.1.1
Tomcat:apache-tomcat-6.0.41(有时也会用Jetty)
(不定期更新)
1、报错
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jetty/webapp/WebAppContextat com.jfinal.server.JettyServer.doStart(JettyServer.java:87) at com.jfinal.server.JettyServer.start(JettyServer.java:64) at com.jfinal.core.JFinal.start(JFinal.java:159) at com.demo.jfinal.main.JFinalStart.main(JFinalStart.java:20)
解决方法:在Maven的pom.xml文件中加入:
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>8.1.8.v20121106</version> </dependency>
2、JFinal的路由配置?
route.add("/", IndexController.class,"/"); route.add("/helloworld", HelloWorldController.class," /WEB-INF/views/helloworld"); //路径写出WEB-INF
如果这样配置,那么http://localhost:8080/ 和http://localhost:8080/helloworld都是跳转到index()。
假如我的Controller是这么写的:
public class HelloWorldController extends Controller { public void index() { renderText("JFinal Demo!"); } //@ActionKey("helloworld") public void helloworld() { //renderText("Hello JFinal."); String message = "JFinal,My Friend!"; setAttr("helloworld", message); renderFreeMarker("helloworld.html"); } }
为啥我路由按照下面这么配置,路由配置如下:
route.add("/helloworld", HelloWorldController.class," /WEB-INF/views/helloworld"); route.add("/helloworld/helloworld", HelloWorldController.class," /WEB-INF/views/helloworld");
老是警告我:
Warnning!!! ActionKey already used: "/helloworld/helloworld"
按照手册上说的,ControllerKey对应到的是index(),那我ControllerKey设置为“helloworld”,"/helloworld/helloworld"不是应该对应于 helloworld()吗?难道ControllerKey不能和Action方法重名?
当然在我的尝试过后,我发现如果我把后面一个路由配置注释掉之后,访问http://localhost:8080/demojfinal/helloworld是对应到index(),而http://localhost:8080/demojfinal/helloworld/helloworld对应的是helloworld(),可能一个Controller只需要在路由里面配置一个入口地址即可了吧。
3、 JFinal项目部署Jetty启动运行是出现如下红色字体:
2015-04-10 21:16:39.188:INFO:oejw.StandardDescriptorProcessor:NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet Null identity service, trying login service: null Finding identity service: null
解决办法: 在Maven的pom.xml文件中加入:
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-jsp</artifactId> <version>8.1.8.v20121106</version> </dependency>
4、JFinal项目部署Jetty运行后,访问一直报404?
5、JFinal的contextPath怎么设置?
解答:
public void configHandler(Handlers me) { me.add(new ContextPathHandler("contextPath"));//设置上下文路径 }
6、JFinal如何删除表中全部数据?
1)方法一:查询出所有的记录然后逐个删除
String selectAllSql = "select * from jfinal_user"; List<User> userList = User.dao.find(selectAllSql); for(User user : userList) { user.delete(); }
2)方法二:批量删除
String sql = "delete from jfinal_user"; List<String> sqlList = new ArrayList<String> (); sqlList.add(sql); Db.batch(sqlList, 10); //这种批处理即使设置了arPlugin.setShowSql(true);//控制台打印SQL,好像也不会打印出执行的SQL
7、JFinal的事务应用
8、JFinal在文件上传时报的错:Caused by: java.lang.NoClassDefFoundError: com/oreilly/servlet/multipart/FileRenamePolicy
解答:很明显是因为少了某个jar包,在网上找到FileRenamePolicy这个类所在的jar包cos.jar的Maven依赖
<dependency> <groupId>servlets.com</groupId> <artifactId>cos</artifactId> <version>05Nov2002</version> </dependency>
修改pom.xml文件之后,再次运行发现上传文件成功了。
然后在Oschina上搜索相关的问题,看到这个帖子http://www.oschina.net/question/67067_47226,才知道原来JFinal在文件上传的时候需要cos.jar包的支持,在官网Download下来压缩文件中,文件夹jfinal-1.9-lib下的jfinal-1.9-库文件依赖及使用说明.txt中有提到这个包——5:cos-26Dec2008.jar 支持文件上传功能。