Spring MVC,CSS和JavaScript无法正常工作

彼得·黄

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Tiles中的图像/ CSS /样式和包含的页面中的Spring MVC无法正常工作

来自分类Dev

Spring MVC:Apache Tile无法正常工作

来自分类Dev

Spring4 MVC Controller Pageable无法正常工作

来自分类Dev

Spring MVC VersionResourceResolver / ContentVersionStrategy在JSP中无法正常工作

来自分类Dev

Thymeleaf(Java Spring):无法使mvc.uri正常工作

来自分类Dev

无法使带有注释的Spring MVC Portlet正常工作

来自分类Dev

Spring MVC控制器的TDD测试无法正常工作

来自分类Dev

无法使 Spring MVC 工作

来自分类Dev

Spring @JsonIgnore无法正常工作

来自分类Dev

Spring @Transactional无法正常工作

来自分类Dev

带有SPring MVC控制器的Spring AOP无法正常工作

来自分类Dev

Camunda Spring交易集成无法正常工作

来自分类Dev

AOP Spring @AfterReturning无法正常工作

来自分类Dev

Spring FilterChainProxy与filterSecurityInterceptor无法正常工作?

来自分类Dev

Spring Security hasAnyRole无法正常工作

来自分类Dev

Spring Security UserDetailsService无法正常工作

来自分类Dev

React Spring Transition无法正常工作

来自分类Dev

Spring Crowd集成无法正常工作

来自分类Dev

Spring XML View Resolver无法正常工作

来自分类Dev

Camel Spring DSL无法正常工作

来自分类Dev

Spring @Autowired无法正常工作并返回null

来自分类Dev

Java Spring + Hibernate LocalSessionFactoryBean无法正常工作

来自分类Dev

Camunda Spring交易集成无法正常工作

来自分类Dev

ViewResolver在Spring 4中无法正常工作

来自分类Dev

Spring Security UserDetailsService无法正常工作

来自分类Dev

Spring BeanUtils.copyProperties无法正常工作

来自分类Dev

Spring Controller 请求映射无法正常工作

来自分类Dev

使用@Valid会引发异常,并且在基本的Spring 3.0 MVC程序中无法正常工作

来自分类Dev

Spring 4 MVC验证无法正常工作-BindingResult hasErrors为false

Related 相关文章

  1. 1

    Tiles中的图像/ CSS /样式和包含的页面中的Spring MVC无法正常工作

  2. 2

    Spring MVC:Apache Tile无法正常工作

  3. 3

    Spring4 MVC Controller Pageable无法正常工作

  4. 4

    Spring MVC VersionResourceResolver / ContentVersionStrategy在JSP中无法正常工作

  5. 5

    Thymeleaf(Java Spring):无法使mvc.uri正常工作

  6. 6

    无法使带有注释的Spring MVC Portlet正常工作

  7. 7

    Spring MVC控制器的TDD测试无法正常工作

  8. 8

    无法使 Spring MVC 工作

  9. 9

    Spring @JsonIgnore无法正常工作

  10. 10

    Spring @Transactional无法正常工作

  11. 11

    带有SPring MVC控制器的Spring AOP无法正常工作

  12. 12

    Camunda Spring交易集成无法正常工作

  13. 13

    AOP Spring @AfterReturning无法正常工作

  14. 14

    Spring FilterChainProxy与filterSecurityInterceptor无法正常工作?

  15. 15

    Spring Security hasAnyRole无法正常工作

  16. 16

    Spring Security UserDetailsService无法正常工作

  17. 17

    React Spring Transition无法正常工作

  18. 18

    Spring Crowd集成无法正常工作

  19. 19

    Spring XML View Resolver无法正常工作

  20. 20

    Camel Spring DSL无法正常工作

  21. 21

    Spring @Autowired无法正常工作并返回null

  22. 22

    Java Spring + Hibernate LocalSessionFactoryBean无法正常工作

  23. 23

    Camunda Spring交易集成无法正常工作

  24. 24

    ViewResolver在Spring 4中无法正常工作

  25. 25

    Spring Security UserDetailsService无法正常工作

  26. 26

    Spring BeanUtils.copyProperties无法正常工作

  27. 27

    Spring Controller 请求映射无法正常工作

  28. 28

    使用@Valid会引发异常,并且在基本的Spring 3.0 MVC程序中无法正常工作

  29. 29

    Spring 4 MVC验证无法正常工作-BindingResult hasErrors为false

热门标签

归档