Java Spring MVC ScriptTemplateViewConfigurer无法找到javascript资源

时间:2015-09-04 00:22:02

标签: java spring spring-mvc

我尝试使用基于javascript的模板创建一个小型测试webapp。从Spring 4.2.0开始,有一个特殊的ScriptTemplateViewResolver类。在本地tomcat(8.0.21)服务器上部署我的应用程序时,我得到以下异常:

java.lang.IllegalStateException: Resource /WEB-INF/resources/js/lib/ejs.min.js not found
at org.springframework.web.servlet.view.script.ScriptTemplateView.loadScripts(ScriptTemplateView.java:234)

这是webapp目录结构:

src/main/java/com/itsl/mercado/pages/PagesController.java   <-- here my controller lives
src/main/webapp/WEB-INF/pages/index.ejs                     <-- index page template
src/main/webapp/WEB-INF/pages/index.jsp                     <-- index.jsp is here also (for debug experiment)
src/main/webapp/WEB-INF/resources/js/lib/ejs.min.js         <-- javascript libraries (EJS in my case)
src/main/webapp/WEB-INF/resources/js/render.js              <-- javascript render function
src/main/webapp/WEB-INF/pages-servlet.xml                   <-- servlet context config
src/main/webapp/WEB-INF/web.xml                             <-- here web.xml lives

这是web.xml:

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>

<servlet>
    <servlet-name>pages</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>pages</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

和pages-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<mvc:annotation-driven />

<context:component-scan base-package="com.itsl.mercado.pages"/>

<mvc:view-resolvers>
    <mvc:script-template prefix="/WEB-INF/pages/" suffix=".ejs"/>
</mvc:view-resolvers>
<mvc:script-template-configurer engine-name="nashorn" render-function="render">
    <mvc:script location="/WEB-INF/resources/js/lib/ejs.min.js" />
    <mvc:script location="/WEB-INF/resources/js/render.js" />
</mvc:script-template-configurer>
<!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
    <!--<property name="prefix" value="/WEB-INF/pages/"/>-->
    <!--<property name="suffix" value=".jsp"/>-->
<!--</bean>-->

</beans>

奇怪的是,当我评论ScriptTemplateViewResolver并取消注释InternalResourceViewResolver时,它设法找到index.jsp,但是,它无法找到/WEB-INF/resources/js/lib/ejs.min.js

另外,我在pages-servlet.xml中试过了以下内容(没有任何其他结果):

<mvc:annotation-driven />

<context:component-scan base-package="com.itsl.mercado.pages"/>

<mvc:view-resolvers>
    <mvc:script-template prefix="/WEB-INF/pages/" suffix=".ejs"/>
</mvc:view-resolvers>
<mvc:script-template-configurer engine-name="nashorn" render-function="render">
    <mvc:script location="/resources/js/lib/ejs.min.js" />
    <mvc:script location="/resources/js/render.js" />
</mvc:script-template-configurer>
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/>
<mvc:default-servlet-handler />

如果有人有想法,请帮我弄清楚如何处理问题。提前致谢。

0 个答案:

没有答案