无法将Spring mvc与css集成

时间:2015-12-09 07:16:17

标签: spring-mvc

CSS 文件添加到 Spring MVC

时出现以下错误
  

来自ServletContext资源的XML文档中的第15行   [/WEB-INF/welcome-servlet.xml]无效;嵌套异常是   org.xml.sax.SAXParseException; lineNumber:15; columnNumber:70;   cvc-complex-type.2.4.c:匹配的通配符是严格的,但没有   可以在元素'mvc:resources'找到声明。

1 个答案:

答案 0 :(得分:0)

使用spring标签添加css的正确方法:

spring-web-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.mkyong.web" />

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

    <mvc:resources mapping="/resources/**" location="/resources/theme1/"  
    cache-period="31556926"/>

    <mvc:annotation-driven />

</beans>

使用弹簧标签:

  <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <spring:url value="/resources/css/main.css" var="mainCss" />
    <spring:url value="/resources/js/jquery.1.10.2.min.js" var="jqueryJs" />
    <spring:url value="/resources/js/main.js" var="mainJs" />

    <link href="${mainCss}" rel="stylesheet" />
    <script src="${jqueryJs}"></script>
    <script src="${mainJs}"></script>
</head>
<body>
<h1>1. Test CSS</h1>

<h2>2. Test JS</h2>
<div id="msg"></div>

</body>
</html>

如果您使用的是JSTL

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
    <script src="<c:url value="/resources/js/jquery.1.10.2.min.js" />"></script>
    <script src="<c:url value="/resources/js/main.js" />"></script>
</head>
<body>
<h1>1. Test CSS</h1>

<h2>2. Test JS</h2>
<div id="msg"></div>

</body>
</html>

如需进一步阅读,请点击here