質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

2回答

2573閲覧

Spring bootにおけるテンプレートのレイアウト化

sanezane

総合スコア91

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

1クリップ

投稿2018/12/24 16:39

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には入っていません(レイアウト共通化実装途中であるため)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/12/24 22:55

・w・ メソッドじゃなく変数だな
sanezane

2018/12/25 00:32

ありがとうございます。回答に記載されているフラグメントにて一度試してみます。少々お待ち下さい。。。
guest

回答2

0

templateResolver() の定義は、Thymeleaf-Springプラグインで行っているので、似たような実装を使いたいのでしたら https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#the-springstandard-dialect の実装例にならい、

java

1@Bean 2public SpringResourceTemplateResolver templateResolver(){ 3 // SpringResourceTemplateResolver automatically integrates with Spring's own 4 // resource resolution infrastructure, which is highly recommended. 5 SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); 6 templateResolver.setApplicationContext(this.applicationContext); 7 templateResolver.setPrefix("/WEB-INF/templates/"); 8 templateResolver.setSuffix(".html"); 9 // HTML is the default value, added here for the sake of clarity. 10 templateResolver.setTemplateMode(TemplateMode.HTML); 11 // Template cache is true by default. Set to false if you want 12 // templates to be automatically updated when modified. 13 templateResolver.setCacheable(true); 14 return templateResolver; 15}

を定義し、適宜設定や拡張をすれば良いでしょう。

※ただしここでやれることはあまりないと思料いたします。

投稿2018/12/25 01:47

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

thymeleaf ならば 対応方法

  1. pom.xml に入れるだけで使える ※ xmns ちゃんと設定してね
  2. その外部ライブラリを使うより th:fragment / th:replace を使えばいい

なはずなんだが

投稿2018/12/24 22:50

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問