CSS 和 JS 未加载 - net:ERR_ABORTED 和 404() Java Spring Boot

Posted

技术标签:

【中文标题】CSS 和 JS 未加载 - net:ERR_ABORTED 和 404() Java Spring Boot【英文标题】:CSS and JS not loading - net:ERR_ABORTED and 404() Java Spring Boot 【发布时间】:2018-10-02 22:50:59 【问题描述】:

我已经解决这个问题几天了,并且一直在尝试不同的事情。在启动我的应用程序并将其加载到localhost 时,我收到了net::ERR_ABORTED404() 错误。 index.html 页面加载,但没有 CSS 或 JS 文件。有任何想法吗?我尝试了不同的相对路径和完整路径、一些不同的配置等。

index.html:

    link th:href="@/nav.css" type=text/css rel="stylesheet
    link th:href="@/name.css" type=text/css rel="stylesheet"
    script th:src="@/nav.js" type=text/javascript></script

MvcConfig:

    @Configuration
    @EnableWebMvc
    public class MvcConfig extends WebMvcConfigurerAdapter 

@Bean
public SpringResourceTemplateResolver templateResolver() 
    SpringResourceTemplateResolver templateResolver = new 
SpringResourceTemplateResolver();
    templateResolver.setCacheable(false);
    templateResolver.setPrefix("classpath:/templates/");
    templateResolver.setSuffix(".html");
    return templateResolver;


@Bean
public SpringTemplateEngine templateEngine() 
    SpringTemplateEngine springTemplateEngine = new 
SpringTemplateEngine();
    springTemplateEngine.addTemplateResolver(templateResolver());
    return springTemplateEngine;


@Bean
public ThymeleafViewResolver viewResolver() 
    String[] excludedViews = new String[] 
            "/resources/static/*";
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine());
    viewResolver.setOrder(1);
    viewResolver.setExcludedViewNames(excludedViews);
    return viewResolver;


@Override
public void configureDefaultServletHandling(
        DefaultServletHandlerConfigurer configurer) 
    configurer.enable();


@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) 
    registry.addResourceHandler("/resource/**").addResourceLocations("WEB-
INF/resources/");


索引控制器:

@Controller
public class IndexController 

    @RequestMapping(value =  "", "/", "/index")
    public String showIndex() 

        return "index";
    

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.site</groupId>
    <artifactId>newsite</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>newsite</name>
    <description>website update 1</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

安全配置:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter 

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception 
        httpSecurity
                .authorizeRequests()
                .antMatchers("/")
                .permitAll();

        // Disabling csrf tokens and x-frame-options to be able to run h2 console (localhost:8080/console)
        httpSecurity.csrf().disable();
        httpSecurity.headers().frameOptions().disable();
    

    @Override
    public void configure(WebSecurity web) throws Exception 
        web
                .ignoring()
                .antMatchers("/resources/**");
    

我的设置是:

-src
--main
---java
----com.site
-----configuration
-----controllers
----application(main)

---resources
----static
-----css
------nav.css
------name.css
-----js
------nav.js
----templates
-----index.html

堆栈跟踪是:

/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bin/java -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=54683 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=127.0.0.1 -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=54684:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/tools.jar:/Users/myname/Documents/WebSite/updated/target/classes:/Users/myname/.m2/repository/org/springframework/boot/spring-boot-starter-security/2.0.1.RELEASE/spring-boot-starter-security-2.0.1.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/boot/spring-boot-starter/2.0.1.RELEASE/spring-boot-starter-2.0.1.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.0.1.RELEASE/spring-boot-starter-logging-2.0.1.RELEASE.jar:/Users/myname/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Users/myname/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/myname/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.10.0/log4j-to-slf4j-2.10.0.jar:/Users/myname/.m2/repository/org/apache/logging/log4j/log4j-api/2.10.0/log4j-api-2.10.0.jar:/Users/myname/.m2/repository/org/slf4j/jul-to-slf4j/1.7.25/jul-to-slf4j-1.7.25.jar:/Users/myname/.m2/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar:/Users/myname/.m2/repository/org/yaml/snakeyaml/1.19/snakeyaml-1.19.jar:/Users/myname/.m2/repository/org/springframework/spring-aop/5.0.5.RELEASE/spring-aop-5.0.5.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/spring-beans/5.0.5.RELEASE/spring-beans-5.0.5.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/security/spring-security-config/5.0.4.RELEASE/spring-security-config-5.0.4.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/spring-context/5.0.5.RELEASE/spring-context-5.0.5.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/security/spring-security-web/5.0.4.RELEASE/spring-security-web-5.0.4.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/spring-expression/5.0.5.RELEASE/spring-expression-5.0.5.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/boot/spring-boot-starter-thymeleaf/2.0.1.RELEASE/spring-boot-starter-thymeleaf-2.0.1.RELEASE.jar:/Users/myname/.m2/repository/org/thymeleaf/thymeleaf-spring5/3.0.9.RELEASE/thymeleaf-spring5-3.0.9.RELEASE.jar:/Users/myname/.m2/repository/org/thymeleaf/thymeleaf/3.0.9.RELEASE/thymeleaf-3.0.9.RELEASE.jar:/Users/myname/.m2/repository/org/attoparser/attoparser/2.0.4.RELEASE/attoparser-2.0.4.RELEASE.jar:/Users/myname/.m2/repository/org/unbescape/unbescape/1.1.5.RELEASE/unbescape-1.1.5.RELEASE.jar:/Users/myname/.m2/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar:/Users/myname/.m2/repository/org/thymeleaf/extras/thymeleaf-extras-java8time/3.0.1.RELEASE/thymeleaf-extras-java8time-3.0.1.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.0.1.RELEASE/spring-boot-starter-web-2.0.1.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.0.1.RELEASE/spring-boot-starter-json-2.0.1.RELEASE.jar:/Users/myname/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.9.5/jackson-databind-2.9.5.jar:/Users/myname/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0.jar:/Users/myname/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.9.5/jackson-core-2.9.5.jar:/Users/myname/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.9.5/jackson-datatype-jdk8-2.9.5.jar:/Users/myname/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.9.5/jackson-datatype-jsr310-2.9.5.jar:/Users/myname/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.9.5/jackson-module-parameter-names-2.9.5.jar:/Users/myname/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.0.1.RELEASE/spring-boot-starter-tomcat-2.0.1.RELEASE.jar:/Users/myname/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.29/tomcat-embed-core-8.5.29.jar:/Users/myname/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/8.5.29/tomcat-embed-el-8.5.29.jar:/Users/myname/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/8.5.29/tomcat-embed-websocket-8.5.29.jar:/Users/myname/.m2/repository/org/hibernate/validator/hibernate-validator/6.0.9.Final/hibernate-validator-6.0.9.Final.jar:/Users/myname/.m2/repository/javax/validation/validation-api/2.0.1.Final/validation-api-2.0.1.Final.jar:/Users/myname/.m2/repository/org/jboss/logging/jboss-logging/3.3.2.Final/jboss-logging-3.3.2.Final.jar:/Users/myname/.m2/repository/com/fasterxml/classmate/1.3.4/classmate-1.3.4.jar:/Users/myname/.m2/repository/org/springframework/spring-web/5.0.5.RELEASE/spring-web-5.0.5.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/spring-webmvc/5.0.5.RELEASE/spring-webmvc-5.0.5.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/boot/spring-boot-devtools/2.0.1.RELEASE/spring-boot-devtools-2.0.1.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/boot/spring-boot/2.0.1.RELEASE/spring-boot-2.0.1.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.0.1.RELEASE/spring-boot-autoconfigure-2.0.1.RELEASE.jar:/Users/myname/.m2/repository/com/h2database/h2/1.4.197/h2-1.4.197.jar:/Users/myname/.m2/repository/org/springframework/spring-core/5.0.5.RELEASE/spring-core-5.0.5.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/spring-jcl/5.0.5.RELEASE/spring-jcl-5.0.5.RELEASE.jar:/Users/myname/.m2/repository/org/springframework/security/spring-security-core/5.0.4.RELEASE/spring-security-core-5.0.4.RELEASE.jar com.zachthayer.newsite.NewsiteApplication
18:54:27.157 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
18:54:27.166 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/, /spring-boot/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter/target/classes/]
18:54:27.166 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/Users/myname/Documents/WebSite/updated/target/classes/]

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)

2018-04-22 18:54:27.804  INFO 63101 --- [  restartedMain] c.site.newsite.NewsiteApplication  : Starting NewsiteApplication on Nikis-MBP with PID 63101 (/Users/myname/Documents/WebSite/updated/target/classes started by myname in /Users/myname/Documents/WebSite/updated)
2018-04-22 18:54:27.806  INFO 63101 --- [  restartedMain] c.site.newsite.NewsiteApplication  : No active profile set, falling back to default profiles: default
2018-04-22 18:54:27.914  INFO 63101 --- [  restartedMain] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1f1a27ec: startup date [Sun Apr 22 18:54:27 CDT 2018]; root of context hierarchy
2018-04-22 18:54:29.384  INFO 63101 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-04-22 18:54:29.404  INFO 63101 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-04-22 18:54:29.404  INFO 63101 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.29
2018-04-22 18:54:29.408  INFO 63101 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/nikitaschultz/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2018-04-22 18:54:29.470  INFO 63101 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-04-22 18:54:29.471  INFO 63101 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1567 ms
2018-04-22 18:54:29.587  INFO 63101 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-04-22 18:54:29.588  INFO 63101 --- [ost-startStop-1] .s.DelegatingFilterProxyRegistrationBean : Mapping filter: 'springSecurityFilterChain' to: [/*]
2018-04-22 18:54:29.588  INFO 63101 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-04-22 18:54:29.589  INFO 63101 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet webServlet mapped to [/h2-console/*]
2018-04-22 18:54:29.755  INFO 63101 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[ || / || /index]" onto public java.lang.String com.site.newsite.controllers.IndexController.showIndex()
2018-04-22 18:54:29.764  INFO 63101 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error]" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-04-22 18:54:29.765  INFO 63101 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error],produces=[text/html]" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-22 18:54:29.802  INFO 63101 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/resource/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-22 18:54:29.806  INFO 63101 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
2018-04-22 18:54:29.858  INFO 63101 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1f1a27ec: startup date [Sun Apr 22 18:54:27 CDT 2018]; root of context hierarchy
2018-04-22 18:54:30.218  INFO 63101 --- [  restartedMain] .s.s.UserDetailsServiceAutoConfiguration : 

Using generated security password: bdb0d712-a401-4236-a43a-8640d098eb4b

2018-04-22 18:54:30.344  INFO 63101 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: org.springframework.security.web.util.matcher.AnyRequestMatcher@1, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@20535127, org.springframework.security.web.context.SecurityContextPersistenceFilter@1f64bc72, org.springframework.security.web.header.HeaderWriterFilter@573f5c9f, org.springframework.security.web.authentication.logout.LogoutFilter@43b8332, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@68e2a192, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@6434b609, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@970737, org.springframework.security.web.session.SessionManagementFilter@189056ec, org.springframework.security.web.access.ExceptionTranslationFilter@253cd51a, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@2c2a1f6c]
2018-04-22 18:54:30.450  INFO 63101 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2018-04-22 18:54:30.493  INFO 63101 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-04-22 18:54:30.546  INFO 63101 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-04-22 18:54:30.552  INFO 63101 --- [  restartedMain] c.site.newsite.NewsiteApplication  : Started NewsiteApplication in 3.361 seconds (JVM running for 4.846)
2018-04-22 18:54:34.975  INFO 63101 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-04-22 18:54:34.976  INFO 63101 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-04-22 18:54:34.991  INFO 63101 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms

我已经尝试过添加@EnableWebMvc 并将其删除 - 只是来回查看什么会起作用。还尝试添加 Web-Inf 文件夹,但没有运气。任何帮助将不胜感激,因为我不确定我错过了什么!提前致谢。

【问题讨论】:

【参考方案1】:

您是否尝试在 application.properties 中指定静态文件夹?像这样:

spring.mvc.static-path-pattern=/static/**

【讨论】:

【参考方案2】:

我认为您的安全配置不允许加载静态资源。您可以按照以下步骤操作:

    configure(WebSecurity web) 覆盖添加到您的SecurityConfig 类中:

    @Override
    public void configure(WebSecurity web) throws Exception 
        web
                .ignoring()
                .antMatchers("/resources/**");
    
    

    将所有静态资源放在webapp/resources/.中。

【讨论】:

我已更改安全配置以添加这些更改,并按照您的建议移动了静态资源,但仍然看到相同的错误:( 你能分享一下你的静态文件的文件夹结构吗? 当然,很抱歉它在那里,但自从添加网络应用程序后我还没有更新。现在它是 webapp -> 资源 -> 然后是一个静态文件夹和一个模板文件夹。静态文件夹有一个 css 文件夹,其中有一个文件和 2 个 css 文件。然后模板文件夹有 index.html 文件。抱歉,格式不正确,但希望我解释正确【参考方案3】:

我也遇到了这个问题,因此花了很多时间。我这样解决了这个问题:

我的目录层次结构:

-resources
--static.css
---bg.css

--templates.catalog
---catalog.html

链接:

href="/css/bg.css"

【讨论】:

以上是关于CSS 和 JS 未加载 - net:ERR_ABORTED 和 404() Java Spring Boot的主要内容,如果未能解决你的问题,请参考以下文章

Magento 2.4.3 安装后未加载 css 和 js。在 url 'pub' 中丢失

在apache tomcat上部署war文件时未加载CSS和JS文件

Laravel 8 + nginx - 来自公共/未加载的 app.css 和 app.js 资源 - 未找到 404

CSS 和 JS 未加载 - net:ERR_ABORTED 和 404() Java Spring Boot

未加载 VueJs 组件节点模块 css/js

函数未从 CSS 模态中的 js 数据加载所有项目