【问题标题】:Uncaught Error: Could not load **** at XMLHttpRequest.xhr.onreadystatechange未捕获的错误:无法在 XMLHttpRequest.xhr.onreadystatechange 加载 ****
【发布时间】:2017-04-17 13:30:11
【问题描述】:

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

获取http://localhost:8080/demo/resources/css/neo.css browser.min.js:4 GEThttp://localhost:8080/demo/resources/js/WeatherManager.js404() browser.min.js:4 未捕获的错误:无法加载http://localhost:8080/demo/resources/js/WeatherManager.js 在 XMLHttpRequest.xhr.onreadystatechange (browser.min.js:4)

* 网络配置 *

@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 文件位于 /resources/css/ /resources/js/

JSP 页面位于 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]

【问题讨论】:

  • 查看代码在哪里? HTML/JSP 等?您如何尝试导入脚本文件?看起来不对。
  • 我在github上添加了源代码链接>>github.com/saifmasadeh/WeatherBoard
  • 是的,我找到了。您也应该将其添加到问题中,这样问题就完成了。
  • 感谢您的反馈:) 我在问题中添加了jsp页面代码,目录信息

标签: java spring reactjs spring-boot


【解决方案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.js Spring 会将其视为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) 作为您的资源位置。)

【讨论】:

  • 您的建议帮助我解决了这个问题,但我不得不将脚本导入部分更新为:** ** ,我想知道这是否与我将 application.properties 文件中的 contextPath 更新为 ** server.contextPath=/demo **
  • 啊...静态内容文件的路径有时需要反复试验。 /js/script.js 可能正在查看 localhost/js/script.js。您可以使用 js/script.js 而不使用前面的正斜杠,具体取决于页面的路径。或者,我使用自定义 JSP 标记来解析脚本路径以避免此问题。
猜你喜欢
  • 2017-06-26
  • 2019-10-07
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 2012-10-01
相关资源
最近更新 更多