简体   繁体   English

无法为我的Spring Maven项目映射资源

[英]Cannot map resources for my Spring Maven project

I can't map my static resources like css and js files in my Spring application built in Maven. 我无法在我在Maven中构建的Spring应用程序中映射我的静态资源,如css和js文件。

I already followed the guidelines offered related to this question but still it fetches me 404 status for my resources. 我已经遵循了与此问题相关的指南,但它仍然为我的资源提取了404状态。

I have below directory list 我有以下目录列表

/src/main/webapp/
\
 WEB-INF
        \
         views
         applicationContext.xml
         dispatcherServlet.xml
         web.xml
 resources
         \
          themes
               \
                bootstrap
                        \
                         css (bootstrap.min.css is inside)
                         js  (bootstrap.min.js is inside)

I have the mvc:resource mapping in my dispatcher-servlet.xml 我的dispatcher-servlet.xml中有mvc:resource映射

<mvc:resources mapping="/resources/**" location="/resources/themes/bootstrap/"/>

Below is my LoginPage.jsp file. 下面是我的LoginPage.jsp文件。

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html>
    <head>
    <spring:url value="/resources/css/bootstrap.min.css" var="loginCss" />
    <spring:url value="/resources/js/bootstrap.min.js" var="loginJs" />

    <link href="${loginCss}" rel="stylesheet" />
    <script src="${loginJs}"></script>
...
</html>

After inspection it is fetching to below URL with 404 status: 检查后,它将获取404状态下面的URL:

http://localhost:8080/{project-name}/resources/css/bootstrap.min.css 
http://localhost:8080/{project-name}/resources/css/bootstrap.min.js 

Check the servlet-mapping on web.xml. 检查web.xml上的servlet-mapping。

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

Edit: 编辑:

Add the below servlet-mapping to your web.xml 将以下servlet映射添加到web.xml

  <servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <url-pattern>/resources/**</url-pattern>
  </servlet-mapping>

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

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