3

CSS 和 JavaScript 没有在我的页面上生效。我在网上搜索,人们说这是魔法,但没有发生在我的页面上。

<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
  |             |                |      |
  |             |                |      |

任何提示将不胜感激!

4

2 回答 2

10

选项1

对于“资源”,您需要再上一层。该src/main/resources文件夹是 Maven 的根位置,用于存放将包含在类路径中的资源。做这个:

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

使用此目录结构:

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

选项 2

或者,如果您希望将资源移动到 Web 根目录,请执行以下操作:

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

...使用此目录结构:

src/main/webapp/resources
                       |
                       |------js
                       |
                       ...
于 2013-11-03T01:50:05.757 回答
3

你至少有两个错误:

第一的

在您使用的jsp中,您错过了resources/script.js路径中的js文件夹!

正确的是:

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

第二(正是“功夫”已经写过的)

2.事情是maven合并文件夹:'src/main/resources'在网络应用程序根文件夹中。

因此,您应该在其中创建一个新文件夹并将and文件夹resources放在那里:src/main/resourcesjs/css/

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

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

resources/js/resources/css/文件夹src/main/webapp

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

并保持弹簧配置不变

于 2013-11-03T11:22:43.747 回答