【问题标题】:Stylesheets and javascript not read in Spring applicationSpring 应用程序中未读取样式表和 javascript
【发布时间】:2017-09-04 13:27:48
【问题描述】:

如果我在没有 Spring 的情况下打开 html 文件,css 和 js 文件会正确加载。但是当我将它作为 Spring 应用程序运行时,css 和 js 文件不起作用,输出只是一个裸 html 页面。

我尝试使用 thymeleaf 和普通链接添加路径,如下所示。两种尝试都没有区别,也没有错误消息。

环顾四周,并被建议将文件移动到静态文件夹。那也无济于事。请指教。谢谢你。

项目结构

添加 js 和 css 文件的链接。自动完成会建议正确的文件,我认为这些文件表明文件位于正确的位置。

<!--<link href='http://fonts.googleapis.com/css?family=Titillium+Web:400,300,600' rel='stylesheet' type='text/css'/>-->
<!--<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"/>-->
<!--<link rel="stylesheet" href="../static/bootstrap/css/bootstrap.min.css"/>-->
<!--<link rel="stylesheet" href="../static/css/style.css"/>-->

<link th:href="@{http://fonts.googleapis.com/css?family=Titillium+Web:400,300,600}" rel='stylesheet' type='text/css'/>
<link th:href="@{https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css}" rel="stylesheet" type="text/css"/>
<link th:href="@{../static/bootstrap/css/bootstrap.min.css}"/>
<link th:href="@{../static/css/style.css}"/>

<script th:src="@{../static/bootstrap/js/bootstrap.min.js}" type="javascript"></script>
<script th:src="@{../static/js/jquery.min.js}" type="javascript"></script>
<script th:src="@{../static/js/index.js}" type="javascript"></script>
<!--<script src='../static/js/jquery.min.js'></script>-->
<!--<script src="../static/js/index.js"></script>-->

gradle 文件

group 'com.fantasy'
version '1.0-SNAPSHOT'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'gs-securing-web'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    testCompile("junit:junit")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile("org.springframework.security:spring-security-test")
    compile("org.springframework.boot:spring-boot-starter-security")
}

MvcConfig.java

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/hello").setViewName("hello");
        registry.addViewController("/login").setViewName("login");
    }

}

WebSecurityConfig.java

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/", "/index").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}

【问题讨论】:

    标签: javascript css spring-mvc thymeleaf


    【解决方案1】:

    删除链接中的“../static”,它将起作用。您的文件被映射到 URL。因此,当您访问服务器时,您不会像访问文件系统结构那样访问它。 Spring-Boot 默认定义了所需的 ResourceMapping。

    【讨论】:

    • 试过 健全的结果。
    • 你自定义了Spring Boot的默认配置吗?
    • 不,我将它们保留为默认值。
    • 嗯。你定制了 Spring Security 吗?默认情况下,Spring 将“静态”文件夹中的所有文件映射到 URL-Root...
    • 也没有对 Spring Security 进行任何更改。添加了 gradle 文件和 java 文件,以防它们可能有助于识别问题。
    猜你喜欢
    • 1970-01-01
    • 2019-08-10
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多