google-code-export / nutz

Automatically exported from code.google.com/p/nutz
Apache License 2.0
1 stars 1 forks source link

JSF与Nutz.dao结合使用出现异常 #405

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
问题产生的条件:
在JSF的BackBean中使用Nutz.Dao

发生问题的调用代码:
index.xhtml代码:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
  <h:head>
    <title>Facelet Title</title>
  </h:head>
  <h:body>
    <h:form prependId="false" id="form1" >
      <h:commandButton action="#{testBB.action}" id="btn1" value="DO"/>
    </h:form>
  </h:body>
</html>

BackBean代码:
package backbean;

import com.xl.eos.eo.User;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;
import org.nutz.dao.impl.NutDao;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.NutIoc;
import org.nutz.ioc.loader.json.JsonLoader;

/**
 *
 * @author Administrator
 */
@ManagedBean
@RequestScoped
public class TestBB {

    /** Creates a new instance of TestBB */
    public TestBB() {
    }

    public String action(){
      String result = null;
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl("jdbc:mysql://localhost:3306/jobdb");
    ds.setUsername("root");
    ds.setPassword("1234");

    NutDao dao = new NutDao(ds);
    System.out.println("---------->dao" + dao);
    System.out.println("---------->ds=" + dao.getDataSource());

    User user = new User();
    user.setUserid("30001786");
    user.setUsername("肖磊");
    user.setUserpwd("1234");
    user.setUserrole(9);

    dao.insert(user);

      return result;
    }

}

问题的详细说明:

异常堆栈:
信息: Reloading Context with name [/Test02] is completed
------------->1
------------->2
2011-03-11 16:28:36 WARN ["http-apr-8084"-exec-4] !!You are using default 
SystemLog! Don't use it in Production environment!!
---------->daoorg.nutz.dao.impl.NutDao@5a9c6e
---------->ds=org.apache.tomcat.dbcp.dbcp.BasicDataSource@d420eb
2011-03-11 16:28:37 DEBUG ["http-apr-8084"-exec-4] Parse POJO 
<com.xl.eos.eo.User> for DB[MYSQL]
2011-03-11 16:28:37 DEBUG ["http-apr-8084"-exec-4] INSERT INTO 
user(userpwd,username,userid,userrole) VALUES( '1234', 'John', '123456', 9)
2011-03-11 16:28:37 DEBUG ["http-apr-8084"-exec-4] Prepare: INSERT INTO 
user(userpwd,username,userid,userrole) VALUES( ?, ?, ?, ?)
2011-03-11 16:28:37 DEBUG ["http-apr-8084"-exec-4] SELECT MAX(id) FROM user
2011-03-11 16:28:37 DEBUG ["http-apr-8084"-exec-4] Prepare: SELECT MAX(id) FROM 
user
2011-03-11 16:28:37 DEBUG ["http-apr-8084"-exec-4] Scan Resource by 
org.nutz.resource.impl.LocalResourceScan@b48230
2011-3-11 16:28:37 com.sun.faces.application.ActionListenerImpl processAction
严重: java.lang.RuntimeException: java.lang.ExceptionInInitializerError
javax.faces.el.EvaluationException: java.lang.RuntimeException: 
java.lang.ExceptionInInitializerError
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:244)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:550)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:284)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:322)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1684)
    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.RuntimeException: java.lang.ExceptionInInitializerError
    at org.nutz.lang.Lang.wrapThrow(Lang.java:121)
    at org.nutz.trans.Trans.exec(Trans.java:127)
    at org.nutz.trans.Trans.exec(Trans.java:80)
    at org.nutz.dao.impl.NutDao._insertSelf(NutDao.java:617)
    at org.nutz.dao.impl.NutDao.access$400(NutDao.java:56)
    at org.nutz.dao.impl.NutDao$9.invoke(NutDao.java:649)
    at org.nutz.lang.Lang.each(Lang.java:1219)
    at org.nutz.dao.impl.NutDao.insert(NutDao.java:647)
    at backbean.TestBB.action(TestBB.java:45)
    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 org.apache.el.parser.AstValue.invoke(AstValue.java:262)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 26 more
Caused by: java.lang.ExceptionInInitializerError
    at org.nutz.dao.entity.EntityField.setValue(EntityField.java:225)
    at org.nutz.dao.entity.next.FieldQueryImpl.update(FieldQueryImpl.java:93)
    at org.nutz.dao.impl.NutDao.runFieldQuery(NutDao.java:612)
    at org.nutz.dao.impl.NutDao.access$200(NutDao.java:56)
    at org.nutz.dao.impl.NutDao$7.run(NutDao.java:624)
    at org.nutz.trans.Trans.exec(Trans.java:122)
    ... 41 more
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of 
range: -1
    at java.lang.String.substring(String.java:1937)
    at org.nutz.resource.impl.LocalResourceScan.list(LocalResourceScan.java:83)
    at org.nutz.resource.Scans.scanPackage(Scans.java:109)
    at org.nutz.resource.Scans.scanPackage(Scans.java:117)
    at org.nutz.castor.Castors.reload(Castors.java:163)
    at org.nutz.castor.Castors.resetPaths(Castors.java:109)
    at org.nutz.castor.Castors.<init>(Castors.java:142)
    at org.nutz.castor.Castors.<clinit>(Castors.java:38)
    ... 47 more
2011-3-11 16:28:37 com.sun.faces.lifecycle.InvokeApplicationPhase execute
警告: #{testBB.action}: java.lang.RuntimeException: 
java.lang.ExceptionInInitializerError
javax.faces.FacesException: #{testBB.action}: java.lang.RuntimeException: 
java.lang.ExceptionInInitializerError
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:244)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:550)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:284)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:322)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1684)
    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: javax.faces.el.EvaluationException: java.lang.RuntimeException: 
java.lang.ExceptionInInitializerError
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 25 more
Caused by: java.lang.RuntimeException: java.lang.ExceptionInInitializerError
    at org.nutz.lang.Lang.wrapThrow(Lang.java:121)
    at org.nutz.trans.Trans.exec(Trans.java:127)
    at org.nutz.trans.Trans.exec(Trans.java:80)
    at org.nutz.dao.impl.NutDao._insertSelf(NutDao.java:617)
    at org.nutz.dao.impl.NutDao.access$400(NutDao.java:56)
    at org.nutz.dao.impl.NutDao$9.invoke(NutDao.java:649)
    at org.nutz.lang.Lang.each(Lang.java:1219)
    at org.nutz.dao.impl.NutDao.insert(NutDao.java:647)
    at backbean.TestBB.action(TestBB.java:45)
    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 org.apache.el.parser.AstValue.invoke(AstValue.java:262)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 26 more
Caused by: java.lang.ExceptionInInitializerError
    at org.nutz.dao.entity.EntityField.setValue(EntityField.java:225)
    at org.nutz.dao.entity.next.FieldQueryImpl.update(FieldQueryImpl.java:93)
    at org.nutz.dao.impl.NutDao.runFieldQuery(NutDao.java:612)
    at org.nutz.dao.impl.NutDao.access$200(NutDao.java:56)
    at org.nutz.dao.impl.NutDao$7.run(NutDao.java:624)
    at org.nutz.trans.Trans.exec(Trans.java:122)
    ... 41 more
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of 
range: -1
    at java.lang.String.substring(String.java:1937)
    at org.nutz.resource.impl.LocalResourceScan.list(LocalResourceScan.java:83)
    at org.nutz.resource.Scans.scanPackage(Scans.java:109)
    at org.nutz.resource.Scans.scanPackage(Scans.java:117)
    at org.nutz.castor.Castors.reload(Castors.java:163)
    at org.nutz.castor.Castors.resetPaths(Castors.java:109)
    at org.nutz.castor.Castors.<init>(Castors.java:142)
    at org.nutz.castor.Castors.<clinit>(Castors.java:38)
    ... 47 more

相关日志:

Original issue reported on code.google.com by lein.x...@gmail.com on 11 Mar 2011 at 8:39

GoogleCodeExporter commented 9 years ago
你用的是什么版本呢?

Original comment by wendal1985@gmail.com on 11 Mar 2011 at 9:51

GoogleCodeExporter commented 9 years ago
问题产生的原因:
1. LocalResourceScan的83行没有判断pos是否大于0
2. 在Web环境,但不使用NutMVC的情况下, 
Scans类就使用LocalResourceScan而非WebResourceScan

解决:
1. 修正LocalResourceScan的bug
2. 在哪个文档里面提醒一下要自行调用 Scans.init(ServletContext)??

文档写好再Fix这个bug吧.

Original comment by wendal1985@gmail.com on 11 Mar 2011 at 10:01

GoogleCodeExporter commented 9 years ago
看来需要新增一个文档,讲讲 Scans

Original comment by zozoh...@gmail.com on 11 Mar 2011 at 2:02

GoogleCodeExporter commented 9 years ago
在什么地方调用Scans.init(ServletContext)呢?

Original comment by lein.x...@gmail.com on 13 Mar 2011 at 2:26

GoogleCodeExporter commented 9 years ago
你需要自己定义一个Servlet,并设置为启动即加载.这个servlet无�
��对应任何url,仅需要在init方法中调用Scans.init(ServletContext)

Original comment by wendal1985@gmail.com on 13 Mar 2011 at 3:08

GoogleCodeExporter commented 9 years ago
谢谢,问题已经解决,代码如下,不知道有没有问题
public class ScansServlet extends HttpServlet {

  @Override
  public void init() throws ServletException {
    super.init();
    Scans.me().init(this.getServletContext());
  }

}

Original comment by lein.x...@gmail.com on 13 Mar 2011 at 5:54

GoogleCodeExporter commented 9 years ago
就是这样写,并且load-on-start最好写1,其他servlet都写2或以上

Original comment by wendal1985@gmail.com on 13 Mar 2011 at 11:58

GoogleCodeExporter commented 9 years ago

Original comment by zozoh...@gmail.com on 24 Mar 2011 at 4:21

GoogleCodeExporter commented 9 years ago

Original comment by zozoh...@gmail.com on 15 Jul 2011 at 3:49