CSS和JavaScript在我的页面上无效。
我在网上搜索了Google,有人说这是神奇的事情,但我的页面却没有发生。

<mvc:resources mapping="/resources/**" location="/resources/" />
这是错误:
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'
这是applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<context:component-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>

这是web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>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>

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

这是被移走的页面:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/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>
Eclipse中的文件夹结构:
myweb
  |
  |
  |
  |----Java Resources
  |             |
  |             |
  |             |-----src/main/resources
  |             |                |
  |             |                |
  |             |                |------js
  |             |                |       |
  |             |                |       |-----jquery-1.10.2.min.js
  |             |                |       |
  |             |                |       |
  |             |                |       |-----script.js
  |             |                |
  |             |                |
  |             |                |-----css
  |             |                |      |
  |             |                |      |-----style.css
  |             |                |      |
  |             |                |      |
任何提示将不胜感激!

最佳答案

选项1

您还需要一个“资源”级别。 src/main/resources文件夹是Maven的根目录,这些资源将包含在类路径中。做这个:

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

使用此目录结构:
src/main/resources/resources
                       |
                       |------js
                       |
                       ...

选项2

或者,如果您希望将资源移至网络根目录,请执行以下操作:
<mvc:resources mapping="/resources/**" location="/resources" />

...具有以下目录结构:
src/main/webapp/resources
                       |
                       |------js
                       |
                       ...

09-27 04:53