【问题标题】:Unable to load static file in Spring4Spring4无法加载静态文件
【发布时间】:2017-05-18 09:17:20
【问题描述】:

我正在尝试创建一个示例 Spring MVC 项目,但没有加载静态文件。我收到下面提到的错误。

http://localhost:8080/BPMEI/static/css/bootstrap.css 加载资源失败:服务器响应状态为 404(未找到)

如果有人可以帮助我解决此问题,我将不胜感激。

Project Structure

配置代码

 package com.dgsl.bpm.configuration;


@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.dgsl.bpm")
public class WebConfiguration extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/");
    }

}

初始化代码

package com.dgsl.bpm.configuration;

public class WebInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(WebConfiguration.class);
        ctx.setServletContext(container);
        ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);

    }

}

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"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Student Enrollment Form</title>
    <link href="<c:url value='/static/css/bootstrap.css' />" rel="stylesheet"></link>
</head>
<body>

【问题讨论】:

  • 你的jsp文件在哪里?在WEB-INFWEB-INF/{some_other_folder} 中?
  • JSP 文件位于 'WEB-INF\views\welcome.jsp'
  • 你可以试试&lt;c:url value='../../static/css/bootstrap.css' /&gt;
  • 我进行了更改,但仍然无法正常工作,但错误已更改,如下所述 'localhost/:10 GET localhost:8080/static/css/bootstrap.css'
  • 跟着我的回答老兄。

标签: java spring


【解决方案1】:

如下所示更改您的 addResourceHandlers 定义并尝试:-

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

【讨论】:

    【解决方案2】:

    可能是你做错了映射。

     registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    

    此代码应该可以工作,我在本地机器上对其进行了测试。 addResourceLocations("/static/") 最后一个斜杠是必填 在sping-core-4.2.5-release.jar StringUtils.java中有如下代码

    public static String applyRelativePath(String path, String relativePath) {
            int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR);
            if (separatorIndex != -1) {
                String newPath = path.substring(0, separatorIndex);
                if (!relativePath.startsWith(FOLDER_SEPARATOR)) {
                    newPath += FOLDER_SEPARATOR;
                }
                return newPath + relativePath;
            }
            else {
                return relativePath;
            }
    }
    

    如果不以斜线结尾,此方法将返回 relativePath

    仅供参考,当我查看您附加的图片时,我在您的项目中找到了“bootstrap.min.css”。不要忘记使用正确的文件名。希望我的建议能帮到你

    【讨论】:

    • 更改了代码 -'registry.addResourceHandler("/static/**").addResourceLocations("/static/");'还是不行
    【解决方案3】:

    致F2:如果你让它自己下载而不是我下载css文件并告诉Spring它们在哪里,你必须让js文件或其他东西发布到不受你控制的网络。建筑物所有者的目的是如何在Spring MVC项目中加载静态文件。我认为问题出在addResourceLocations的代码上,应该是addResourceLocations ("classpath:/static/");

    【讨论】:

    • 哦,对不起,不是F2,是F1
    【解决方案4】:

    请在您的 url 中使用上下文路径:

    <c:url value='${pageContext.request.contextPath}/static/css/bootstrap.css' />
    

    这将为您提供 url 值:

    BPMEI/static/css/bootstrap.css
    

    让我知道这是否有效。

    【讨论】:

      【解决方案5】:

      改变 addResourceHandlers 像

      @Override
      public void addResourceHandlers(final ResourceHandlerRegistry registry) {
          registry.addResourceHandler("/**").addResourceLocations("/");
      }
      

      同时更改 css 文件名 'bootstrap.min.css'

      【讨论】:

        猜你喜欢
        • 2015-03-10
        • 2014-07-12
        • 2018-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-26
        • 2021-05-05
        相关资源
        最近更新 更多