【发布时间】: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