Spring MVC集成了js和css文件

时间:2015-03-11 19:12:08

标签: javascript css spring jsp spring-mvc

我是Spring Framework的新手,这是我第一个使用它的应用程序,所以,我有下一个问题..而且我很抱歉我的英文非常糟糕嘿嘿......

我需要在jsp中包含一些js和css文件。我正在使用spring mvc。我有一个NotClassFound异常..这是我的所有代码:

在我的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>

在servlet-context中(我尝试过使用和不使用“classpath”的mvc资源)

<!-- 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/" />

在jsp文件中

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

在'webapp / resources'中我有我的js和ccs文件..我认为我的主要问题是org.springframework.js库..我将它映射到web.xml中,但是当我运行了这个项目:

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)

我有一周时间试图找出这个问题,但我不知道该怎么做..请,我需要帮助!

2 个答案:

答案 0 :(得分:0)

查看web.xml似乎应该存在org.springframework.js.resource.ResouceServlet,但要确保它存在于类路径中。您也可以尝试按照此链接执行以下步骤:

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

希望这有帮助!

答案 1 :(得分:0)

您不需要ResourceServlet。单独使用mvc:resources标签就可以了。 javadoc明确而明确:不赞成使用资源标签。不要同时使用它们