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