
前提・実現したいこと
※imageフォルダを「webapp/image」に移動したら解決しました。
Spring 5 + Spring Security + Thymeleaf + BootstrapでECサイトを作成しています。
ローカルに置いてあるcssファイルを読み込みたいのですが、未だにうまく反映されません。
発生している問題・エラーメッセージ
Failed to load resource: the server responded with a status of 404 () style.css
Chrome上でソースを見てリンクを押すと404エラーが発生します。パスがあっていないことはわかるのですが、Spring上でどのようにpathを指定すればいいのかわかりません。
該当のソースコード
- Web Security Config
Spring
1 @Override 2 public void configure(WebSecurity web) throws Exception { 3 // セキュリティ設定を無視するリクエスト設定 4 // 静的リソースに対するアクセスはセキュリティ設定を無視する 5 6 web.ignoring().antMatchers("/resources/**", "/resource/**", "/css/**", "/js/**", "/images/**", "/vendor/**", "/fonts/**"); 7 } 8 9 @Override 10 protected void configure(AuthenticationManagerBuilder auth) throws Exception { 11 auth.eraseCredentials(true) 12 .userDetailsService(userDetailsService) 13 .passwordEncoder(passwordEncoder()); 14 //add our users for in memory authentication 15 16 } 17 18 @Override 19 protected void configure(HttpSecurity http) throws Exception { 20 http.authorizeRequests() 21 .antMatchers("/").permitAll() 22 .anyRequest().authenticated() 23 .and() 24 .formLogin() 25 .loginPage("/index") 26 .permitAll() 27 .usernameParameter("userName") 28 .passwordParameter("password") 29 .loginProcessingUrl("/authenticate") 30 .defaultSuccessUrl("/result") 31 .failureUrl("/login/failure") 32 ; 33 }
- Web MVC Config
Spring
1 @Bean 2 public SpringResourceTemplateResolver templateResolver() { 3 SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); 4 //ビューを保存するフォルダ名を指定する 5 templateResolver.setPrefix("WEB-INF/templates/"); 6 //ビューの拡張子を指定する 7 templateResolver.setSuffix(".html"); 8 //テンプレートモードをHTMLに指定する 9 //templateResolver.setTemplateMode(TemplateMode.HTML); 10 templateResolver.setTemplateMode("HTML5"); 11 //テンプレート読み込み時の文字コードを指定する 12 templateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name()); 13 return templateResolver; 14 } 15 16 @Bean 17 public SpringTemplateEngine templateEngine() { 18 SpringTemplateEngine engine = new SpringTemplateEngine(); 19 //engine.addDialect(new SpringSecurityDialect()); 20 engine.setTemplateResolver(templateResolver()); 21 return engine; 22 } 23 24 @Bean 25 public ThymeleafViewResolver thymeleafViewResolver() { 26 ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); 27 viewResolver.setTemplateEngine(templateEngine()); 28 //ビューを書き出す際の文字コードを指定する 29 viewResolver.setCharacterEncoding("UTF-8"); 30 viewResolver.setOrder(1); 31 return viewResolver; 32 } 33 34 /** 35 * CSSなどの静的コンテンツを利用するための設定を記述する。 36 */ 37 public void addResourceHandlers(ResourceHandlerRegistry registry) { 38 39 // Register resource handler for CSS and JS 40 registry.addResourceHandler("resource/**").addResourceLocations("classpath:/static") 41 .setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS).cachePublic()); 42 43 // Register resource handler for images 44 registry.addResourceHandler("/images/**").addResourceLocations("/WEB-INF/images/") 45 .setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS).cachePublic()); 46 }
- ビュー(Thymeleafで読み込み)
HTML
1<!doctype html> 2<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="utf-8"/> 5 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/> 6 <meta name="description" content=""/> 7 <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors"/> 8 <meta name="generator" content="Jekyll v3.8.5"/> 9 10 <title>Navbar Template · Bootstrap</title> 11 12 <!-- Bootstrap CSS --> 13 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" 14 integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> 15 16 <!--<link rel="stylesheet" th:src="@{/css/style.css}" type="text/css"/>--> 17 18 <link href="/css/style.css" th:href="@{/css/style.css}" rel="stylesheet">
HTML
1<div class="container"> 2 <div class="row"> 3 <!-- Carousal --> 4 <div class="col-md-8"> 5 <div class="panel panel-default"> 6 <div class="panel-body"> 7 <!--カルーセルのタグ。いじるのはオススメしない。--> 8 <div id="carousel-example" class="carousel slide" data-ride="carousel"> 9 10 <!--ここで好きな枚数のスライドを作れる。imgタグのscr内に好きな画像を入れよう。--> 11 <div class="carousel-inner"> 12 <!--後ろにactiveをつけたものが最初に表示されるよ。--> 13 <div class="carousel-item active"> 14 <img class="d-block w-100" src="http://lorempixel.com/output/sports-q-c-1600-500-1.jpg" 15 alt="Picture1"> 16 <div class="carousel-caption"> 17 <h4>First Thumbnail</h4> 18 <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots 19 in a 20 piece of classical Latin literature from 45 BC, making it over 2000 years 21 old. </p> 22 </div> 23 </div> 24 <div class="carousel-item"> 25 <img class="d-block w-100" src="http://lorempixel.com/output/sports-q-c-1600-500-3.jpg" 26 alt="Picture2"> 27 </div> 28 <div class="carousel-item"> 29 <img class="d-block w-100" src="http://lorempixel.com/output/sports-q-c-1600-500-2.jpg" 30 alt="Picture3"> 31 </div> 32 </div> 33 34 <!--これはスライドの「進むボタン」と「戻るボタン」。いらないなら無くていい。--> 35 <a class="carousel-control-prev" href="#carousel-example" role="button" data-slide="prev"> 36 <span class="carousel-control-prev-icon" aria-hidden="true"></span> 37 <span class="sr-only">Previous</span> 38 </a> 39 <a class="carousel-control-next" href="#carousel-example" role="button" data-slide="next"> 40 <span class="carousel-control-next-icon" aria-hidden="true"></span> 41 <span class="sr-only">Next</span> 42 </a> 43 44 <!--こちらはスライドの枚数や現在地がわかるあれ。いらないならn(ry--> 45 46 <ol class="carousel-indicators"> 47 <li data-target="#carousel-example" data-slide-to="0" class="active"></li> 48 <li data-target="#carousel-example" data-slide-to="1"></li> 49 <li data-target="#carousel-example" data-slide-to="2"></li> 50 </ol> 51 </div> 52 </div> 53 </div> 54 </div> 55 <div class="col-sm-2"> 56 <div class="logo"> 57 <img th:src="@{/images/logo.png}"/> 58 </div> 59 </div> 60 </div> 61</div>
試したこと
以下のStackFlow のサイトの方法で解決を試みましたが、完璧に詰んでいます。
初歩的な質問で申し訳ないですが、どこが間違えているのか検討がつきません。
1)https://stackoverflow.com/questions/50792837/thymeleaf-3-spring-5-load-css
2)https://stackoverflow.com/questions/54848419/cant-load-my-css-when-using-spring-security-and-thymeleaf
3)https://stackoverflow.com/questions/54844592/unable-to-add-css-file-in-spring-and-thymeleaf
4)https://stackoverflow.com/questions/29562471/springboot-with-thymeleaf-css-not-found
5) https://stackoverflow.com/questions/26283670/spring-mvc-issue-with-loading-of-resources-images-css
補足情報(FW/ツールのバージョンなど)
ちなみに、Spring Bootは使用していません。
使用しているIDE:IntelliJ
ご教授よろしくお願いします。