Apache MVCとApache Tilesの統合
1概要
Apache
Tiles
は、純粋にCompositeデザインパターンに基づいて構築された無料のオープンソースのテンプレートフレームワークです。
複合設計パターンは、オブジェクトをツリー構造に構成して全体の階層を表す一種の構造パターンであり、このパターンは個々のオブジェクトとオブジェクトの構成を一様に扱います。つまり、Tilesでは、Tilesと呼ばれるサブビューの構成を組み合わせてページが構築されます。
このフレームワークの他のフレームワークに対する利点は次のとおりです。
-
再利用性
-
設定が簡単
-
低パフォーマンスのオーバーヘッド
この記事では、Apache TilesとSpring MVCの統合に焦点を当てます。
2依存関係の設定
ここでの最初のステップは、必要なhttps://search.maven.org/classic/#search%7C1%7Cg%3A%22org.apache.tiles%22%20AND%20a%3A%22tiles-jsp%を追加することです
pom.xml
の22[依存関係]:
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>3.0.8</version>
</dependency>
3タイルレイアウトファイル
テンプレート定義を定義する必要があります。具体的には各ページごとに、その特定ページのテンプレート定義を上書きします。
<tiles-definitions>
<definition name="template-def"
template="/WEB-INF/views/tiles/layouts/defaultLayout.jsp">
<put-attribute name="title" value=""/>
<put-attribute name="header"
value="/WEB-INF/views/tiles/templates/defaultHeader.jsp"/>
<put-attribute name="menu"
value="/WEB-INF/views/tiles/templates/defaultMenu.jsp"/>
<put-attribute name="body" value=""/>
<put-attribute name="footer"
value="/WEB-INF/views/tiles/templates/defaultFooter.jsp"/>
</definition>
<definition name="home" extends="template-def">
<put-attribute name="title" value="Welcome"/>
<put-attribute name="body"
value="/WEB-INF/views/pages/home.jsp"/>
</definition>
</tiles-definitions>
4
ApplicationConfiguration
と他のクラス
構成の一部として、
ApplicationInitializer
、
ApplicationController
、および
ApplicationConfiguration
という3つの特定のJavaクラスを作成します。
-
ApplicationInitializer
は必要なものを初期化してチェックします
ApplicationConfiguration
クラスで指定された構成
**
ApplicationConfiguration
クラスには、以下の設定が含まれています。
Spring MVCとApache Tilesフレームワークの統合
**
ApplicationController
クラスは
tiles.xml
ファイルと同期して動作し、
着信要求に基づいて必要なページにリダイレクトします。
実行中の各クラスを見てみましょう。
@Controller
@RequestMapping("/")
public class TilesController {
@RequestMapping(
value = { "/"},
method = RequestMethod.GET)
public String homePage(ModelMap model) {
return "home";
}
@RequestMapping(
value = { "/apachetiles"},
method = RequestMethod.GET)
public String productsPage(ModelMap model) {
return "apachetiles";
}
@RequestMapping(
value = { "/springmvc"},
method = RequestMethod.GET)
public String contactUsPage(ModelMap model) {
return "springmvc";
}
}
public class WebInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(TilesApplicationConfiguration.class);
container.addListener(new ContextLoaderListener(ctx));
ServletRegistration.Dynamic servlet = container.addServlet(
"dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
Spring MVCアプリケーションでタイルを設定する際に重要な役割を果たす2つの重要なクラスがあります。それらは
TilesConfigurer
と
TilesViewResolver
です。
-
TilesConfigurer
は、TilesフレームワークをSpringとリンクするのに役立ちます。
tiles構成ファイルへのパスを提供することによってフレームワーク
**
TilesViewResolver
はSpring APIが提供するアダプタクラスの一つです。
タイルビューを解決する
最後に、
ApplicationConfiguration
クラスでは、統合を実現するために
TilesConfigurer
クラスと
TilesViewResolver
クラスを使用しました。
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.baeldung.spring.controller.tiles")
public class TilesApplicationConfiguration implements WebMvcConfigurer {
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tilesConfigurer = new TilesConfigurer();
tilesConfigurer.setDefinitions(
new String[]{ "/WEB-INF/views/** ** /tiles.xml" });
tilesConfigurer.setCheckRefresh(true);
return tilesConfigurer;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
TilesViewResolver viewResolver = new TilesViewResolver();
registry.viewResolver(viewResolver);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/** ** ")
.addResourceLocations("/static/");
}
}
5タイルテンプレートファイル
これまで、Apache Tilesフレームワークの設定、およびアプリケーション全体で使用されるテンプレートと特定のタイルの定義の設定は完了しました。
このステップでは、
tiles.xml
で定義されている特定のテンプレートファイルを作成する必要があります。
特定のページを構築するためのベースとして使用できるレイアウトの断片を見つけてください。
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><tiles:getAsString name="title"/></title>
<link href="<c:url value='/static/css/app.css'/>"
rel="stylesheet">
</link>
</head>
<body>
<div class="flex-container">
<tiles:insertAttribute name="header"/>
<tiles:insertAttribute name="menu"/>
<article class="article">
<tiles:insertAttribute name="body"/>
</article>
<tiles:insertAttribute name="footer"/>
</div>
</body>
</html>
6. 結論
これでSpring MVCとApache Tilesの統合は完了です。
完全な実装はhttps://github.com/eugenp/tutorials/tree/master/spring-mvc-simple[次のgithubプロジェクト]にあります。