spring mvc、css 和 javascript 无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19748980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:41:25  来源:igfitidea点击:

spring mvc, css and javascript is not working properly

javascriptcssspring

提问by sh977218

the css and javascript is not take effect on my page. I google online, people saying this is the magic, but not happening on my page.

css 和 javascript 在我的页面上没有生效。我在网上谷歌,人们说这是魔法,但没有发生在我的页面上。

<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

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

here is the web.xml

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

this is the page got displaied:

这是显示的页面:

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

the folder structure in eclipse:

eclipse中的文件夹结构:

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

any tips would be appreciated!! thanks in advanced!

任何提示将不胜感激!先谢谢了!

回答by MattSenter

Option 1

选项1

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

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

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

With this directory structure:

使用此目录结构:

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

Option 2

选项 2

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

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

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

...with this directory structure:

...使用此目录结构:

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

回答by Ralph

You have at least two bugs:

你至少有两个错误:

First

第一的

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

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

Correct would be:

正确的应该是:

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

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

第二(这正是“kungfuters”已经写的)

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

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

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

因此,你应该创建一个新的文件夹resourcessrc/main/resources,并把js/css/文件夹有:

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

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

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

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

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

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

and leave the spring configuration unchanged

并保持弹簧配置不变