简体   繁体   中英

Spring MVC integration js and css files

I'm new with Spring Framework, this is my first app using it, so, I have the next problem.. And I'm sorry for my really bad english hehe..

I need to include in my jsp some js and css files. I'm working with spring mvc. I have a NotClassFound exception.. This is all my code:

In my web.xml

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>Resource Servlet</servlet-name>
    <servlet-class>org.springframework.js.resource.ResouceServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Resource Servlet</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

In servlet-context (I've tried with mvc resource with and without "classpath")

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:default-servlet-handler />
<!-- <mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/"/> -->
<mvc:resources mapping="/resources/**" location="/resources/" />

in the jsp file

<%@ page import="org.springframework.js.resource.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="<c:url value='/resources/medico.css'/>" />
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/zapdent.js"></script>
<%-- <script type="text/javascript" src="<c:url value="/resources/spring/Spring-Dojo.js" />"> </script> --%>
</head>

In 'webapp/resources' is where I have my js and ccs files.. and I think my principal problem is with the org.springframework.js library.. I'm mapping it in the web.xml, but when I run the project I have this:

SEVERE: Servlet /springsecurity threw load() exception
java.lang.ClassNotFoundException: org.springframework.js.resource.ResouceServlet
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1295)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1147)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:520)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:501)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:120)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1105)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1041)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4944)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5230)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

I have like a week trying to figure out this problem, but I dont know how to do it.. Please, I need help!

Looking at web.xml it seems that org.springframework.js.resource.ResouceServlet should be there, but make sure it exists in the classpath. You could also try following this link and perform these steps:

https://stackoverflow.com/a/28170853/2748793

Hope this helps!

You don't need ResourceServlet. The mvc:resources tag alone will do the trick. the javadoc is explicit and clear: It's deprecated in favor of the resource tag. Don't use them both

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM