-2

I am getting the following error while adding CSS file to Spring MVC

Line 15 in XML document from ServletContext resource [/WEB-INF/welcome-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 15; columnNumber: 70; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:resources'.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
  • Welcome to stack overflow please read http://stackoverflow.com/help/how-to-ask and then update/improve your question. – M. Deinum Dec 09 '15 at 07:22
  • If you still need help, update your question including your xml configuration file. – a.ndrea Jan 04 '16 at 19:45

1 Answers1

0

the correct way to add the css using spring tags :

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

Using spring tags :

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

If your are using 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>

For further reading, click here.

JGCW
  • 1,509
  • 1
  • 13
  • 25
  • I referred all the sites and i worked out on the sample code u given but still iam getting the same problem which i mentioned above,and i tried changing all the spring jar version too. But still its not happening can u please help me to fix the issue.Its taking me a long time – Manoj Suravarapu Dec 19 '15 at 11:23
  • why dont you try a clean version of eclipse and a clean version of project. then it should work. – JGCW Dec 21 '15 at 06:26
  • can u please suggest me the cleaner versions please – Manoj Suravarapu Dec 21 '15 at 08:58
  • can u please suggest me cleaner versions please and iam getting different error like this Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.6 Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.6.when using with maven – Manoj Suravarapu Dec 21 '15 at 09:07
  • i tried with maven but while creating project itself i got the error which i mentioned aove – Manoj Suravarapu Dec 21 '15 at 09:08
  • which i mentioned above – Manoj Suravarapu Dec 21 '15 at 09:08
  • iam new to spring ,maven can u please make my work smoother – Manoj Suravarapu Dec 21 '15 at 09:08
  • i think things are corrupt with you..the software. Remove eclipse, delete all the save files delete the workspace directory..Do a restart, reinstall eclipse and maven – JGCW Dec 22 '15 at 04:15
  • i resolved the issues regarding which i mentioned above about mave n.Can u provide any source code for spring mvc example in maven .i will take it as reference to my things pleaseeee help mee. – Manoj Suravarapu Dec 22 '15 at 21:04
  • and i new to maven can u please tell me something about maven and its usage – Manoj Suravarapu Dec 22 '15 at 21:36
  • http://www.mkyong.com/spring/quick-start-maven-spring-example/ this page has examples for spring maven integration . It consists of the sourcecode as well. Maven is simply a built tool that allows you to add and maintain jarsin your project. its more like a config file for you entire java application. Read this for further undestanding. http://stackoverflow.com/questions/3589562/why-maven-what-are-the-benefits – JGCW Dec 23 '15 at 03:38