最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Spring not finding resource files (css, jsp...) - Stack Overflow

programmeradmin2浏览0评论

I'm using a jsp file as a model from a Controller and I want to use an css styles and js libraries

  • Proyect
    • Webcontent
    • assets
    • WEB-INF
      • jsp

In web.xml:

<web-app version="2.5" xmlns=""
xmlns:xsi=""
xsi:schemaLocation=" .xsd">
<welcome-file-list>
    <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>

<!-- Processes application requests -->
<servlet>
    <servlet-name>MyProject</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>MyProject</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

 </web-app>

In applicationContext.xml:

<context:annotation-config />
<context:component-scan base-package="main.mypack.controller" />



<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp"/>
</bean>

And in my jsp file: href="/assets/css/jquery.mobile.fixedToolbar.polyfill.css"

Noy working, any help?

EDIT: I'm using 2.5 Spring version, and I have errors like: No mapping found for HTTP request with URI [/MyProject/assets/js/jqm-project.js] in DispatcherServlet with name 'MyProject'

I'm using a jsp file as a model from a Controller and I want to use an css styles and js libraries

  • Proyect
    • Webcontent
    • assets
    • WEB-INF
      • jsp

In web.xml:

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
    <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>

<!-- Processes application requests -->
<servlet>
    <servlet-name>MyProject</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>MyProject</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

 </web-app>

In applicationContext.xml:

<context:annotation-config />
<context:component-scan base-package="main.mypack.controller" />



<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp"/>
</bean>

And in my jsp file: href="/assets/css/jquery.mobile.fixedToolbar.polyfill.css"

Noy working, any help?

EDIT: I'm using 2.5 Spring version, and I have errors like: No mapping found for HTTP request with URI [/MyProject/assets/js/jqm-project.js] in DispatcherServlet with name 'MyProject'

Share Improve this question edited May 8, 2012 at 10:59 Marta asked May 8, 2012 at 8:59 MartaMarta 2912 gold badges6 silver badges16 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

The problem is that your requests for CSS and JS files are going through Dispatcher Servlet, which is not correct. Hence Spring won't find the mapping for those files it will not load them.

You need to add the resourceHandler for your application in the applicationContext.xml file as follows. This configuration will bypass the requests for CSS and JS files from the Dispatcher Servlet.

<mvc:resources location="/assets/" mapping="/assets/**" />

Hope this helps you... Cheers.

Please put following code in web.xml

<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.css</url-pattern>
 </servlet-mapping>

 <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
 </servlet-mapping>

Auto-solved:

This mapping is blocking everything, I change this:

    <servlet-mapping>
      <servlet-name>MyProject</servlet-name>
       <url-pattern>/</url-pattern>
   </servlet-mapping>

For this (and I change my calls to "call.do"):

    <servlet-mapping>
      <servlet-name>MyProject</servlet-name>
       <url-pattern>*.do</url-pattern>
   </servlet-mapping>

And it works!

发布评论

评论列表(0)

  1. 暂无评论