Wednesday, December 11, 2013

Spring MVCでThymeleafを使う

Thymeleafについて

ThymeleafはJSPに変わるような、主にHTML向けのテンプレートエンジン。JSPよりも高性能らしい。大きな特徴は、HTMLとしても妥当なマークアップでテンプレートを記述できること。同様のアプローチはSeasarファミリーにMayaaというのがあった。Mayaaはテンプレートのほかに、データとテンプレートのマッピング用のXMLを用意する必要があったのがイマイチだった。Thymeleafは普通にテンプレートファイルは一つで大丈夫。

ここではSTSのSpring MVCのテンプレートで作成したプロジェクトのテンプレートエンジンを、Thymeleafに変更する。

Mavenで依存を解決

pom.xmlのdependencyに以下を追加。

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>2.0.16</version>
</dependency>
<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf-spring3</artifactId>
  <version>2.0.16</version>
</dependency>
で、以下の(あるいは他の)コマンドを実行。
$ mvn install

Springの設定

Springの設定ファイルでテンプレートエンジンをJSPからThymeleafに変更。

<!-- 
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <beans:property name="prefix" value="/WEB-INF/views/" />
  <beans:property name="suffix" value=".jsp" />
</beans:bean>
-->

<beans:bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
  <beans:property name="prefix" value="/WEB-INF/views/" />
  <beans:property name="suffix" value=".html" />
  <beans:property name="characterEncoding" value="UTF-8" />
  <beans:property name="templateMode" value="HTML5" />
  <beans:property name="cacheable" value="false"/>
</beans:bean>
<beans:bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
  <beans:property name="templateResolver" ref="templateResolver" />
</beans:bean>
<beans:bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
  <beans:property name="characterEncoding" value="UTF-8" />
  <beans:property name="templateEngine" ref="templateEngine" />
</beans:bean>   

注意点としては、開発用にテンプレートのキャッシュを無効にしてある(デフォルトでは有効)。プロダクションにでプロイするときにはこの設定を有効にするべきでしょう。<beans:property name="cacheable" value="false"/>という箇所。

テンプレートを記述

あとはWEB-INF/views以下にテンプレートをThymeleafの書式で書いていくだけ。Spring MVCのコントローラからの呼び出し方法についてはJSPと全く同様。 ただし、テンプレートのHTMLには以下のような宣言が必要。Thymeleaf単体で利用するのとは違い、thymeleafに'-spring3'というサフィックスがつくのに注意。

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring3-3.dtd">

<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>

1 comment:

Unknown said...

Nowadays, with the industry increasing in a great number of complex renovations, pieces are generally all-around appropriate clones of your first. For what reason merely shut down, one may wonder. For the reason that whole very idea of looks after is almost always to set up an easily affordable item, a merchandise that may be appreciated as a result of all of check out fans, not just by abundant very few. Knowning that translates that gold and silver such as rare metal or maybe platinum are generally changed through the very best quality stainless. Aside from the fact that, all with written with the greatest treatment replica watches, in order to make each and every glance, believe plus work for instance the real counterpart. The truth is numerous website pages selling running watches as they are trading well. The key reason why would likely these offer therefore very well should superior quality seemed to be a problem? As to why may consumers constantly return to invest in far more when displeased with these? The difficulty with simple truth is which usually unless you can easily drop some decent 500 us dollars replica watches uk, or maybe even tons of upon an device, wristwatches might possibly be the most effective replacement for your needs. As well as in all honesty, although I'd that will sort of cash to waste http://www.attrinity.com, I needed turn out to be unwilling to carry out them. Precisely why commit twenty as well as $ 100 or so circumstances much more upon a factor that is actually certainly economical? A well used buddy connected with mine out of institution most recently paid for themselves a watch, considering the things much better look at proceeds better by using other folks? Absolutely, my pal Charles is affluent. Your dog has been bragging a lot along with his cutting edge obtain, around the truth that it will be such a reduced piece and so forth, we thought i would make fun of your pet along with I just bought a watch to get myself personally, since i simply cannot spend the money for original.