Javascript 无法使用spring加载外部脚本js文件
结构是: src -主要 --资源 ---vue.html ---example.js vue.html是:Javascript 无法使用spring加载外部脚本js文件,javascript,java,spring,vue.js,Javascript,Java,Spring,Vue.js,结构是: src -主要 --资源 ---vue.html ---example.js vue.html是: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vu
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
<script src="./example.js"></script>
<script>
alert("I'm active");
</script>
我去,但我不能去
它给
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
但在intellj idea中,当我右键单击html并说“在浏览器中打开”时,它会转到
我的剧本很好用。这是**example.js**
alert("This script will load first");
那么,是什么阻止我的js工作
这是安全配置:
@Configuration
@EnableWebSecurity
//@EnableWebMvc
public class WebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
String[] resources = new String[]{
"/",
"/home",
"/css/**",
"/icons/**",
"/images/**",
"/resources/**",
"/static/**",
"/js/**"
};
http.csrf().disable();
http.authorizeRequests()
.antMatchers(resources).permitAll()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/authenticateTheUser")
.defaultSuccessUrl("/dashboard")
.permitAll();
}
我也尝试过这些:
<script src="./example.js"></script>
<script src="../example.js"></script>
<script src="/example.js"></script>
<script src="templates/example.js"></script>
尝试以下代码加载静态文件
@Configuration
//@EnableWebMvc
public class MvcConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
String[] resources = new String[]{
"/",
"/home",
"/css/**",
"/icons/**",
"/images/**",
"/resources/**",
"/static/**",
"/js/**"
};
http.csrf().disable();
http.authorizeRequests()
.antMatchers(resources).permitAll()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/authenticateTheUser")
.defaultSuccessUrl("/dashboard")
.permitAll();
}
}
并在html页面中像这样调用它
<script src="http://localhost:8080/myproject/resources/vau.js"></script>
希望这能解决问题。但是
@EnableWebMvc
禁用了MVC自动配置?您可以不使用它,因为spring会自动添加它。它还提供了未捕获的SyntaxError:Unexpected token'您也可以保留它,这是用于静态资源的。我已经更新了代码,您可以扩展并实现这两个功能
<script src="http://localhost:8080/myproject/resources/vau.js"></script>