2017-12-01 6 views
0

私はしばらくこのことに苦労していましたが、まだ理解できません。ThymeleafとSpringのスタイルシートをリンクする際のトラブル

私は外部CSSファイルを動作させることができません。ブラウザは常に200成功メッセージを返しますが、ファイルはロードされません。

は、私はさまざまな方法を試してみたが、これはそれが現時点でどのように見えるかです:

HTMLファイルにリンク:

<!DOCTYPE HTML> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head> 
    <title>Tourverwalter</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <link rel="stylesheet" type="text/css" th:href="@{/assets/css/myStyle.css}" /> 
</head> 

CSSファイル:

body { 
    background-color: lightblue; 
    text-align: center; 
} 
.button { 
    background-color: #4CAF50; /* Green */ 
    border: none; 
    color: white; 
    padding: 15px 32px; 
    text-align: center; 
    text-decoration: none; 
    display: inline-block; 
    font-size: 16px; 
    width: 20%; 
    cursor: pointer; 
} 

header{ 
    vertical-align: top; 
} 

.container { 
    width: 75%; 
    height: 30px; 
    padding: 10px; 
} 

.left { 
    width: 40%; 
    height:30px; 
    float: left; 
} 

.right { 
    margin-left: 60%; 
    height: 30px; 
} 

WebMcvConfigファイル:

package ese4.configuration; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

@Configuration 
public class WebMvcConfig extends WebMvcConfigurerAdapter { 

    @Bean 
    public BCryptPasswordEncoder passwordEncoder() { 
     BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); 
     return bCryptPasswordEncoder; 
    } 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
      registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/"); 

    } 

} 

とフォルダ階層:

src/main/resources/static/css

そして、あなたがオーバーライドaddResourceHandlersメソッドを削除することができます Folder Hierarchy

+0

あなたは春のブートを使用していますか? – bphilipnyc

+0

はい私は春のブートを使用しています –

答えて

0

あなたは春のブートを使用している場合、私はあなたがあなたの構造を変更することをお勧めします。 Spring Boot will use the static folder to serve your static content

次にあなたがあなたのリンクを更新します:脇

<link rel="stylesheet" type="text/css" th:href="@{/css/myStyle.css}" /> 

:あなたも、あなたが春ブーツをロードせずに、ブラウザでファイルを開くときに、あなたができるように、CSSにhrefパスを含めることになるでしょうCSSの効果はまだ分かります。これは、Thymeleafを使用することに大きな利点があります。あなたのUI担当者は、フォーマットされたページを見るためにJavaやSpringについて知る必要がないからです。

<link rel="stylesheet" type="text/css" href="../static/css/myStyle.css" th:href="@{/css/myStyle.css}" /> 

最後に、あなたの他の方法は、のように簡略化することができます。

@Bean 
public PasswordEncoder passwordEncoder() { 
    return new BCryptPasswordEncoder(); 
} 
+0

最初にすべてのお返事ありがとう問題はまだ動作しません...それはまだCSSが成功したように見えますが、読み込まれません。それでも、警告として:リソースはスタイルシートとして解釈されますが、MIMEタイプtext/htmlで転送されます。他にも何か考えがありますか? –

+0

完全なスタックトレースを投稿できますか? – bphilipnyc

+0

また、ファイル名が正確にmyStyle.cssという名前になっていることを確認してください。 – bphilipnyc

関連する問題