gpt4 book ai didi

java - Uncaught Error : Could not load **** at XMLHttpRequest. xhr.onreadystatechange

转载 作者:行者123 更新时间:2023-11-30 06:47:28 24 4
gpt4 key购买 nike

我正在开发一个 springboot 项目,该项目使用天气 api 并使用 React 在浏览器上显示数据,无论如何,我似乎缺少一些配置,或者我可能需要在项目中移动文件,错误在浏览器显示 js/css 文件不可访问:

GET http://localhost:8080/demo/resources/css/neo.css browser.min.js:4 GET http://localhost:8080/demo/resources/js/WeatherManager.js 404 () browser.min.js:4 Uncaught Error: Could not load http://localhost:8080/demo/resources/js/WeatherManager.js at XMLHttpRequest.xhr.onreadystatechange (browser.min.js:4)

* WebConfig *

@Configuration
@ComponentScan
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}

/**
* We need to define 3 things to implement
* 1- Define message resource
* 2- Define Local resolver internationalization
* 3- Override interceptor
*/
@Bean
public MessageSource messageSource(){
ResourceBundleMessageSource messageSource=new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}

@Bean
public LocaleResolver localeResolver(){
SessionLocaleResolver resolver =new SessionLocaleResolver();
resolver.setDefaultLocale(Locale.ENGLISH);
return resolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
//you can add more resources here
registry.addResourceHandler("/css/**").addResourceLocations("/resources/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/resources/js/");
}

@Override
public void addInterceptors(InterceptorRegistry registry)
{
LocaleChangeInterceptor changeInterceptor=new LocaleChangeInterceptor();
changeInterceptor.setParamName("language");
registry.addInterceptor(changeInterceptor);
}


}

* WebAppInitializer *

public class WebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("*.html");
dispatcher.addMapping("*.pdf");
//Enable JSON response
dispatcher.addMapping("*.json");
dispatcher.addMapping("*.jsx");

}

private WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context =new AnnotationConfigWebApplicationContext();
context.register(WebConfig.class);
return context;
}

* 演示应用程序 *

@SpringBootApplication
@EnableAutoConfiguration
@EnableAsync
public class DemoApplication extends AsyncConfigurerSupport {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("WeatherService-");
executor.initialize();
return executor;
}
}

JS/CSS files live under /resources/css/ /resources/js/

JSP pages live under WEB-INF/jsp

** Weather.jsp 页面 **

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="resources/css/neo.css">

<title>Weather </title>
</head>
<body>
<div id="main" class="container">

</div>
<script type="text/babel" src="resources/js/WeatherManager.js"></script>
</body>
</html>

我有源代码
github:[https://github.com/saifmasadeh/WeatherBoard][1]

最佳答案

您应该将其添加到您的问题中:

<script type="text/babel" src="resources/js/WeatherManager.js"></script> 
<link rel="stylesheet" type="text/css" href="resources/css/neo.css">

这就是问题所在。您通过错误的 URL 导入。

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
//you can add more resources here
registry.addResourceHandler("/css/**").addResourceLocations("/resources/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/resources/js/");
}

上面的方法表示,当用户尝试访问路径为 /js/** 的 URL 时,会在 /resources/js/ 中查找。

基本上,如果用户请求localhost/context-root/js/script.jsSpring 会将其视为 localhost/context-root/resources/js/script.js

(事实并非如此,但它很好地解释了这个想法。)

因此,当您尝试导入脚本文件 resources/js/WeatherManager.js 时,资源处理程序不知道在哪里查找。它不知道路径 resources/**

的任何内容

您想要做的是这样导入:

    <script type="text/babel" src="/js/WeatherManager.js"></script> 

这映射到资源处理程序的 "/js/** 并在 /resources/js/ 中查找 WeatherManager.js。需要对 CSS 文件执行相同的操作。

有关其工作原理的另一个示例,view my answer here.

(此外,如果这不起作用,您可能需要使用 classpath:/resources/(js|css) 作为资源位置。)

关于java - Uncaught Error : Could not load **** at XMLHttpRequest. xhr.onreadystatechange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43452479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com