spring boot を勉強中です。
作成したポートフォリオにて共通化できるViewが共通化できていないので共通化してみようと思いました。
以下にgithubのリンクを添付します。
ポートフォリオリンク
しかし、テンプレートを共通化する部分の実装で教本通りに実装してもエラーが出てしまうため質問させていただきます。
実装方法・エラーの内容
Thymeleaf Layout Dialectライブラリを使用、Configクラスでの実装でエラーが発生している。
下記コード下から4行目の**engine.setTemplateResolver(templateResolver());**にてtemplateResolver()メソッドなどない。と怒られる。
java
1package jsug; 2 3import jsug.domain.model.Cart; 4//import jsug.infra.cart.CachingCart; 5import net.sf.log4jdbc.sql.jdbcapi.DataSourceSpy; 6import nz.net.ultraq.thymeleaf.LayoutDialect; 7 8import org.springframework.beans.factory.annotation.Autowired; 9import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 10import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; 11import org.springframework.boot.context.properties.ConfigurationProperties; 12import org.springframework.cache.annotation.EnableCaching; 13import org.springframework.context.annotation.*; 14import org.springframework.web.context.WebApplicationContext; 15import org.thymeleaf.spring4.SpringTemplateEngine; 16import org.thymeleaf.templateresolver.ITemplateResolver; 17 18import javax.sql.DataSource; 19 20@Configuration 21@EnableCaching 22public class AppConfig { 23 @Autowired 24 DataSourceProperties dataSourceProperties; 25 DataSource dataSource; 26 27 @Bean(destroyMethod = "close") 28 @ConfigurationProperties(prefix = DataSourceProperties.PREFIX) 29 DataSource realDataSource() { 30 DataSourceBuilder factory = DataSourceBuilder 31 .create(this.dataSourceProperties.getClassLoader()) 32 .url(this.dataSourceProperties.getUrl()) 33 .username(this.dataSourceProperties.getUsername()) 34 .password(this.dataSourceProperties.getPassword()); 35 this.dataSource = factory.build(); 36 return this.dataSource; 37 } 38 39 @Primary 40 @Bean 41 DataSource dataSource() { 42 return new DataSourceSpy(this.dataSource); 43 } 44 45 @Bean 46 @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS) 47 Cart cart() { 48 return new Cart(); 49 } 50 51 @Bean 52 public SpringTemplateEngine templateEngine() { 53 SpringTemplateEngine engine = new SpringTemplateEngine(); 54 engine.addDialect(new LayoutDialect()); 55 engine.setTemplateResolver(templateResolver());//←templateResolver()メソッドなどない。と怒られる。 56 57 return engine; 58 59 } 60}
確認したこと
setTemplateResolver()は、import **org.thymeleaf.spring4.SpringTemplateEngine;**内にある。その宣言を見てみると。。。
java
1public void setTemplateResolver(final ITemplateResolver templateResolver) { 2 Validate.notNull(templateResolver, "Template Resolver cannot be null"); 3 checkNotInitialized(); 4 this.templateResolvers.clear(); 5 this.templateResolvers.add(templateResolver); 6 }
引数はfinal ITemplateResolverインタフェースのものだった。なので、ITemplateResolverインタフェースを見てみる。。
java
1/* 2 * ============================================================================= 3 * 4 * Copyright (c) 2011-2016, The THYMELEAF team (http://www.thymeleaf.org) 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 * ============================================================================= 19 */ 20package org.thymeleaf.templateresolver; 21 22import java.util.Map; 23 24import org.thymeleaf.IEngineConfiguration; 25import org.thymeleaf.templatemode.TemplateMode; 26import org.thymeleaf.templateresource.ITemplateResource; 27 28 //コメントアウト分省略 29 public Integer getOrder(); 30 31 32 33 //コメントアウト分省略 34 35 36 37 public TemplateResolution resolveTemplate( 38 final IEngineConfiguration configuration, 39 final String ownerTemplate, final String template, 40 final Map<String, Object> templateResolutionAttributes); 41 42} 43
わからないこと
setTemplateResolverの引数にはおそらくITemplateResolver型の変数を入れなくてはいけないのだろうと推測はするも、そこから先のアプローチがわかりません。
■バージョン情報
・Spring boot :1.3.3.RELEASE
・thymeleaf :3.0.9.RELEASE
・thymeleaf-layout-dialect :2.3.0
※上記3つのライブラリはgithubあがっているコードのpom.xmlには入っていません(レイアウト共通化実装途中であるため)