春天的资源mvc

时间:2015-05-15 15:54:02

标签: java javascript spring spring-mvc

我知道在Spring MVC中还有其他关于添加资源(CSS,Javascript)的主题,但在我的情况下并不起作用。添加时出现此错误:HTTP Status 404 The requested resource is not available

mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/"

我的应用程序正常运行,直至此时。

在我的index.jsp中,我得到了:

link type="text/css" rel="stylesheet" href="c:url value="/resources/css/test.css" 
script type="text/javascript" src="c:url value="/resources/js/carousel.js"

2 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,我想我是通过在web.xml中添加默认映射来解决它的。

  <servlet-mapping>
    <servlet-name>dispatherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

答案 1 :(得分:0)

问题解决了。

在DispatcherServlet中我添加了:

<mvc:default-servlet-handler />
<mvc:annotation-driven />

此时&#39; HTTP状态404请求的资源不可用。&#39;已经消失,但我的资源根本无法获得。所以我找到了解决问题的另一种方法,配置基本标签,如

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath %>">
<title>index</title>
<link href="resources/css/test.css" rel="stylesheet" type="text/css" />
</head>
...

现在它正常运作。