具有控制器请求映射的spring静态资源

具有控制器请求映射的spring静态资源,spring,jsp,spring-mvc,Spring,Jsp,Spring Mvc,我有一个SpringMVC项目:以下是我的文件 web.xml <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <

我有一个SpringMVC项目:以下是我的文件

web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>

    <link href="resources/css/bootstrap.css" rel="stylesheet" type="text/css" />

    </head></html>

我的webaps主机带有URL
localhost:8080/projectName/test/home
但它不会加载我的静态资源,即bootstrap.css。它应该可以工作,因为我已经将静态资源放在了正确的位置
webapp/resources/css/bootstrap.css

问:为什么我的静态资源是来自控制器路径的路由。。为什么显示404未找到。??
localhost:8080/projectName/test/resources/css/bootstrap.css


为什么spring要查找静态资源时控制器路径“test”会附加URL。

我让它运行了。。My home.jsp必须包含以下静态资源:

资源的“./”前缀使控制器路由尊重静态资源

<mvc:resources mapping="/resources/**" location="/resources/" />
引用静态资源。
除此之外,还有另一种解决此问题的方法,即像下面这样配置base标记

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <base href="<%=basePath %>">
    <title></title>
    <link href="resources/css/bootstrap.css" rel="stylesheet" type="text/css" />
</head>
....

....

两者都可以正常工作,希望您能提供帮助:)

为了避免url问题,请使用JSTL的


注意:第一个例子是在使用之前在jsp顶部导入JSTL标记库。 比如:



尝试访问:localhost:8080/projectName/resources/css/bootstrap.css您正在使用css资源的相对路径。尝试使用@Bob.Zmy注释中提到的绝对路径。静态资源位于正确的位置。我的意思是,当我尝试访问时,localhost:8080/projectName/resources/css/bootstrap.css会返回我的css。但我的问题是,当我的应用程序加载时,我的控制器无法使用静态资源。。。。它在:localhost:8080/projectName/test/resources/css/bootstrap.css、、中搜索,然后很明显它将搜索404,因为资源不在这个位置下。不,这太愚蠢了!你已经准备好使用绝对路径了!简单使用“/”前缀。
<mvc:resources mapping="/resources/**" location="/resources/" />
<link href="<%= request.getContextPath() %>/resources/css/bootstrap.css" rel="stylesheet" type="text/css" />
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <base href="<%=basePath %>">
    <title></title>
    <link href="resources/css/bootstrap.css" rel="stylesheet" type="text/css" />
</head>
....
<link href='<c:url value="/resources/css/bootstrap.css"/>'
      rel="stylesheet" type="text/css" />
<link href='${pageContext.request.contextPath}/resources/css/bootstrap.css'
      rel="stylesheet" type="text/css" />
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>