简体   繁体   English

Spring MVC中的资源

[英]resource in spring mvc

I know that there are other topics about adding resources(CSS, Javascript) in Spring MVC, but in my case that doesn't work. 我知道在Spring MVC中还有其他有关添加资源(CSS,Javascript)的主题,但是在我的情况下不起作用。 I have this error when adding: HTTP Status 404 The requested resource is not available . 添加时出现此错误: HTTP Status 404 The requested resource is not available

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

My application works properly until this point. 到目前为止,我的应用程序可以正常工作。

In my index.jsp I've got: 在我的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"

I had a similar problem and I think I solved it by adding a default mapping in web.xml 我有一个类似的问题,我想我通过在web.xml中添加默认映射来解决了

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

Problem solved. 问题解决了。

In DispatcherServlet I added : 在DispatcherServlet中,我添加了:

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

At this point 'HTTP Status 404 The requested resource is not available.' 此时,“ HTTP状态404请求的资源不可用”。 has disappeared, but my resources were not available at all. 已经消失了,但是我的资源根本不可用。 So i found another way to solve the problem, configuring the base tag like 所以我找到了另一种解决问题的方法,像

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

And now it works properly. 现在它可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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