使用Velocity的tool出現的問題,velocitytool出現

來源:互聯網
上載者:User

使用Velocity的tool出現的問題,velocitytool出現

       開發使用的是SpringMVC+Velocity的架構,要使用Velocity的tools,在整合時代碼如下:

      <bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true" />
<property name="suffix" value=".vm" />
<property name="contentType">
<value>text/html;charset=UTF-8</value>
</property>
<property name="requestContextAttribute" value="rc" />
<property name="toolboxConfigLocation" value="velocity-toolbox.xml" />
<!-- 定義velocity DATE 格式化 -->
<property name="dateToolAttribute">
<value>dateTool</value>
</property>

      </bean>

        這個volocity-toolbox.xml設定檔是放在src下,也就是說在打成war包之後是放在WEB-INF/classes這個目錄下,可是一直報錯誤如下:

Caused by: java.lang.NullPointerException
at org.springframework.web.servlet.view.velocity.VelocityToolboxView.createVelocityContext(VelocityToolboxView.java:111)
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:264)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
... 23 more

        網上也沒有查到資料,於是調試了下源碼發現在類org.apache.velocity.tools.view.servlet.ServletToolboxManager中讀取這個檔案使用servletContext.getResourceAsStream這個API來讀取的,這樣問題就清楚了,開始以為是用類載入器的方式讀取的。具體servletContext.getResourceAsStream如何使用大家可以看其他資料,這裡不再贅述。不過後來我回頭看別人的配置文檔時,的確發現他們在配置這個檔案路徑時使用的是項目的相對路徑,於是我將這個路徑改成:/WEB-INF/classes/velocity-toolbox.xml,問題解決!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.