velocity toolbox.xml中自定义类中引用*.properties异常处理

架构说明:velocity+spring+ibates架构

使用场合:

  1.toolbox.xml中使用自定义类代替apache的相关类(如:org.apache.velocity.tools.view.tools.LinkTool,org.apache.velocity.tools.generic.DateTool等)。

  2.在自定义类中引用了java.util.ResourceBundle,并使用了getBundle(String name)加载*.properties文件时,出现异常。

  3.异常如下:

2013-01-16 21:48:20,578 INFO [org.apache.velocity.tools.view.servlet.ServletToolboxManager] - Using config file '/WEB-INF/toolbox.xml'
2013-01-16 21:48:20,579 WARN [org.apache.velocity.tools.view.XMLToolboxManager] - XMLToolboxManager has been deprecated. Please use org.apache.velocity.tools.ToolboxFactory instead.
2013-01-16 21:48:20,579 WARN [org.apache.velocity.tools.view.servlet.ServletToolboxManager] - ServletToolboxManager has been deprecated. Please use org.apache.velocity.tools.ToolboxFactory instead.
2013-01-16 21:48:20,731 INFO [org.apache.velocity.tools.view.servlet.ServletToolboxManager] - XHTML is set to true
2013-01-16 21:48:21,047 ERROR [org.apache.commons.digester.Digester] - End event threw exception
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 org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1773)
    at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1759)
    at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648)
    at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:1677)
    at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1022)
    at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:313)
    at org.apache.commons.digester.BeanPropertySetterRule.end(BeanPropertySetterRule.java:202)
    at org.apache.commons.digester.Digester.endElement(Digester.java:1345)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1774)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2930)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at org.apache.commons.digester.Digester.parse(Digester.java:1916)
    at org.apache.velocity.tools.view.XMLToolboxManager.load(XMLToolboxManager.java:216)
    at org.apache.velocity.tools.view.servlet.ServletToolboxManager.getInstance(ServletToolboxManager.java:173)
    at org.springframework.web.servlet.view.velocity.VelocityToolboxView.createVelocityContext(VelocityToolboxView.java:109)
    at org.springframework.web.servlet.view.velocity.VelocityView.renderMergedTemplateModel(VelocityView.java:287)
    at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:167)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1265)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1016)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:781)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ExceptionInInitializerError
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at org.apache.velocity.tools.view.ViewToolInfo.setClassname(ViewToolInfo.java:98)
    ... 49 more
Caused by: java.util.MissingResourceException: Can't find bundle for base name source, locale zh_CN
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1521)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1260)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:715)
    at com.brin.core.util.PropertiesUtil.<clinit>(PropertiesUtil.java:28)
    ... 56 more
2013-01-16 21:48:21,076 ERROR [org.apache.velocity.tools.view.servlet.ServletToolboxManager] - Problem loading toolbox '/WEB-INF/toolbox.xml'
java.lang.reflect.InvocationTargetException
    at org.apache.commons.digester.Digester.createSAXException(Digester.java:3363)
    at org.apache.commons.digester.Digester.createSAXException(Digester.java:3389)
    at org.apache.commons.digester.Digester.endElement(Digester.java:1348)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1774)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2930)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at org.apache.commons.digester.Digester.parse(Digester.java:1916)
    at org.apache.velocity.tools.view.XMLToolboxManager.load(XMLToolboxManager.java:216)
    at org.apache.velocity.tools.view.servlet.ServletToolboxManager.getInstance(ServletToolboxManager.java:173)
    at org.springframework.web.servlet.view.velocity.VelocityToolboxView.createVelocityContext(VelocityToolboxView.java:109)
    at org.springframework.web.servlet.view.velocity.VelocityView.renderMergedTemplateModel(VelocityView.java:287)
    at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:167)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1265)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1016)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:781)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)

在com.brin.core.util.PropertiesUtil中找不到source.properties文件(路径有误):

  protected static final String GLOBAL_PROPERTISE_FILE = "source";
    
     static {
          if(res == null) res = ResourceBundle.getBundle(GLOBAL_PROPERTISE_FILE);
     }

 

总结:

  1.修改常量GLOBAL_PROPERTISE_FILE = "source"为项目中的路径

  本项目为GLOBAL_PROPERTISE_FILE = "config/source" 或 GLOBAL_PROPERTISE_FILE = "/config/source"

  2.public static final ResourceBundle getBundle(String baseName)使用指定的基本名称、默认的语言环境和调用者的类加载器获取资源包。调用此方法等同于调用
  getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader()),不同之处在于要使用 ResourceBundle 的安全特权来运行 getClassLoader()。有关搜索和实例化策略的信息,请参阅 getBundle。
  参数:
  baseName - 资源包的基本名称,是一个完全限定类名
  返回:
    具有给定基本名称和默认语言环境的资源包
  抛出:
    NullPointerException - 如果 baseName 为 null
    MissingResourceException - 如果未找到指定基本名称的资源包

posted @ 2013-01-16 22:05  Brin Page  阅读(2721)  评论(0编辑  收藏  举报