web.xml部分配置如下:

<servlet>
<servlet-name>resource-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:resource-web-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>resource-servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>resource-web-context.xml部分配置如下: <mvc:resources location="/resources/" mapping="/resources/**" cache-period="3600"/> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="ignoreAcceptHeader" value="true" />
<property name="defaultContentType" value="text/html" />
<property name="mediaTypes">
<map>
<entry key="xls" value="application/vnd.ms-excel" />
<entry key="csv" value="application/csv" />
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp"></property>
</bean>包结构如图所示:
我现在想在 config.jsp 里面引用 resources 目录下面的 scripts/ 目录里面的js文件
比如:
    <script type="text/javascript" src="./../resources/scripts/ext/ext-all.js"></script>
    还是 <script type="text/javascript" src="./../../resources/scripts/ext/ext-all.js"></script>为什么?spring是怎么找到引用的路径的呢?谢谢

解决方案 »

  1.   

    程序跳转之后,路径变化之后,使用相对路径找不到相应的文件。
    <script type="text/javascript" src="${pageContext.request.contextPath}/resources/scripts/ext/ext-all.js"></script>修改成上面就可以了。
      

  2.   

    谢谢你的回答,你用的绝对路径肯定没问题,但是相对路径
    <script type="text/javascript" src="./../resources/scripts/ext/ext-all.js"></script>
    是可以访问到的。因为我在该jsp文件的js中发ajax请求也是用相对路径
    './../config/insertConfig.json',
    我就是不明白spring是怎么找到相对路径的,看起来好像应该是../../resources/才对啊,但是这样就会报NOT found 404错误,希望指教!
      

  3.   

    这个与Spring没有任何关系。客户端是无法直接访问JSP的。
    假设你的admin.jsp使用刚才的脚本引用了一个JS文件。
    <script type="text/javascript" src="./../resources/scripts/ext/ext-all.js"></script>
    是这样写的。假设:
    http://addr:port/project/WEB-INF/views/admin.jsp是不可能直接这样来访问的是吧。
    你可能提供了一个地址访问如下。
    http://addr:port/project/admin.do然后Spring会转发到admin.jsp。
    你仔细看这两个目录对应的相对路径。
    admin.jsp -> ../../找到resources路径。
    而admin.do -> 就在当前路径就能找到resources路径。
    根本就变化了,使用相对路径当然找不到相应的文件,你程序出现找不到路径的原因是一样的。
      

  4.   

    注:客户端是无法直接访问WEB-INF下面的JSP。
    口误。