最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Spring MVC, CSS, and JavaScript is not working properly - Stack Overflow

matteradmin11PV0评论

The CSS and JavaScript is not take effect on my page. I googled online, people saying this is the magic, but not happening on my page.

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

This is the error:

Nov 02, 2013 9:19:29 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myweb/resources/css/styles.css] in DispatcherServlet with name 'dispatcher'
Nov 02, 2013 9:19:29 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myweb/resources/script.js] in DispatcherServlet with name 'dispatcher'
Nov 02, 2013 9:19:29 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myweb/resources/js/jquery-1.10.2.min.js] in DispatcherServlet with name 'dispatcher'

Here is the applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=";
xmlns:mvc="; xmlns:xsi=";
xmlns:p="; xmlns:tx=";
xmlns:context=";
xsi:schemaLocation=" 
.2.xsd

.2.xsd
 
.2.xsd

.2.xsd">

<context:ponent-scan base-package="org.peterhuang.myweb" />

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

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
</bean>

<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>

<!-- Hibernate Transaction Manager -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<mvc:annotation-driven />

<!-- Activates annotation based transaction management -->
<tx:annotation-driven />

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties" />
</bean>

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

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="org.peterhuang.myweb" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                ${jdbc.dialect}
            </prop>
            <prop key="hibernate.show_sql">
                ${hibernate.show_sql}
            </prop>
            <prop key="hibernate.format_sql">
                ${hibernate.format_sql}
            </prop>
        </props>
    </property>
</bean>

Here is the web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="; xmlns:xsi=";
xsi:schemaLocation="    .xsd">

<display-name>my web</display-name>

<servlet>
    <servlet-name>dispatcher</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>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<wele-file-list>
    <wele-file>/WEB-INF/jsp/wele.jsp</wele-file>
</wele-file-list>

This is the page got displaied:

<%@taglib uri="; prefix="c"%>
<%@ taglib uri="; prefix="spring"%>
<link type="text/css" rel="stylesheet"
href="<spring:url value='resources/css/styles.css' />" />
<script type="text/javascript"
src="<spring:url value='resources/js/jquery-1.10.2.min.js' />"></script>
<script type="text/javascript" src="<spring:url value='resources/script.js'/>"</script>     

<ul id="button">
<c:forEach var="category" items="${categoryList}">
    <li><a href="#">${category.categoryName}</a></li>
</c:forEach>
</ul>

The folder structure in Eclipse:

myweb
  |
  |
  |
  |----Java Resources
  |             |
  |             |
  |             |-----src/main/resources
  |             |                |
  |             |                |
  |             |                |------js
  |             |                |       |
  |             |                |       |-----jquery-1.10.2.min.js
  |             |                |       |
  |             |                |       |
  |             |                |       |-----script.js
  |             |                |         
  |             |                |      
  |             |                |-----css
  |             |                |      |
  |             |                |      |-----style.css
  |             |                |      |
  |             |                |      |

Any tips would be appreciated!

The CSS and JavaScript is not take effect on my page. I googled online, people saying this is the magic, but not happening on my page.

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

This is the error:

Nov 02, 2013 9:19:29 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myweb/resources/css/styles.css] in DispatcherServlet with name 'dispatcher'
Nov 02, 2013 9:19:29 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myweb/resources/script.js] in DispatcherServlet with name 'dispatcher'
Nov 02, 2013 9:19:29 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myweb/resources/js/jquery-1.10.2.min.js] in DispatcherServlet with name 'dispatcher'

Here is the applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework/schema/beans"
xmlns:mvc="http://www.springframework/schema/mvc" xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xmlns:p="http://www.springframework/schema/p" xmlns:tx="http://www.springframework/schema/tx"
xmlns:context="http://www.springframework/schema/context"
xsi:schemaLocation="http://www.springframework/schema/beans 
http://www.springframework/schema/beans/spring-beans-3.2.xsd
http://www.springframework/schema/mvc
http://www.springframework/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework/schema/context 
http://www.springframework/schema/context/spring-context-3.2.xsd
http://www.springframework/schema/tx
http://www.springframework/schema/tx/spring-tx-3.2.xsd">

<context:ponent-scan base-package="org.peterhuang.myweb" />

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

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
</bean>

<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>

<!-- Hibernate Transaction Manager -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<mvc:annotation-driven />

<!-- Activates annotation based transaction management -->
<tx:annotation-driven />

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties" />
</bean>

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

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="org.peterhuang.myweb" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                ${jdbc.dialect}
            </prop>
            <prop key="hibernate.show_sql">
                ${hibernate.show_sql}
            </prop>
            <prop key="hibernate.format_sql">
                ${hibernate.format_sql}
            </prop>
        </props>
    </property>
</bean>

Here is the web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun./xml/ns/j2ee" xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun./xml/ns/j2ee    http://java.sun./xml/ns/j2ee/web-app_2_4.xsd">

<display-name>my web</display-name>

<servlet>
    <servlet-name>dispatcher</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>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<wele-file-list>
    <wele-file>/WEB-INF/jsp/wele.jsp</wele-file>
</wele-file-list>

This is the page got displaied:

<%@taglib uri="http://java.sun./jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework/tags" prefix="spring"%>
<link type="text/css" rel="stylesheet"
href="<spring:url value='resources/css/styles.css' />" />
<script type="text/javascript"
src="<spring:url value='resources/js/jquery-1.10.2.min.js' />"></script>
<script type="text/javascript" src="<spring:url value='resources/script.js'/>"</script>     

<ul id="button">
<c:forEach var="category" items="${categoryList}">
    <li><a href="#">${category.categoryName}</a></li>
</c:forEach>
</ul>

The folder structure in Eclipse:

myweb
  |
  |
  |
  |----Java Resources
  |             |
  |             |
  |             |-----src/main/resources
  |             |                |
  |             |                |
  |             |                |------js
  |             |                |       |
  |             |                |       |-----jquery-1.10.2.min.js
  |             |                |       |
  |             |                |       |
  |             |                |       |-----script.js
  |             |                |         
  |             |                |      
  |             |                |-----css
  |             |                |      |
  |             |                |      |-----style.css
  |             |                |      |
  |             |                |      |

Any tips would be appreciated!

Share Improve this question edited Jul 1, 2020 at 17:39 Donald Duck 8,89223 gold badges79 silver badges102 bronze badges asked Nov 3, 2013 at 1:42 Peter HuangPeter Huang 1,1885 gold badges13 silver badges39 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

Option 1

You need one more level for "resources." The src/main/resources folder is Maven's root location for resources that will be included in your classpath. Do this:

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

With this directory structure:

src/main/resources/resources
                       |
                       |------js
                       |
                       ...

Option 2

Or, if you'd rather move your resources to the web root, do this:

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

...with this directory structure:

src/main/webapp/resources
                       |
                       |------js
                       |
                       ...

You have at least two bugs:

First

in your jsp you used missed the js folder in the resources/script.js path!

Correct would be:

<spring:url value='resources/js/script.js'/>

Second (it is exactly what "kungfuters" has already written)

The 2. thing is that maven merge the folder: ´src/main/resources´ in the web app root folder.

Therefore you should create a new folder resources within src/main/resources and put the js/ and css/ folders there:

  • src/main/resources/resources/js/
  • src/main/resources/resources/css/

and modify <mvc:resources mapping="/resources/**" location="classpath:/resources" />

or resources/js/ and resources/css/ folder in src/main/webapp

  • src/main/webapp/resources/js/
  • src/main/webapp/resources/css/

and leave the spring configuration unchanged

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far