日期:2014-05-20  浏览次数:20754 次

求教 spring mvc3 的路径配置问题
web.xml部分配置如下:
XML code
    
    <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部分配置如下:
XML code

    <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是怎么找到引用的路径的呢?谢谢

------解决方案--------------------
程序跳转之后,路径变化之后,使用相对路径找不到相应的文件。
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/scripts/ext/ext-all.js"></script>

修改成上面就可以了。