LoginSignup
8
2

More than 1 year has passed since last update.

Spring MVC × MayaaテンプレートでHello World

Last updated at Posted at 2021-12-14

Spring MVC × Mayaaテンプレート

MayaaテンプレートはSeasar2の拡張機能として2000年台に登場したHTMLテンプレートエンジンです。
プログラマーとデザイナーの作業分担を意識した思想があり、主にMayaaファイルとHTMLファイルに分けられて画面が実装されます。

Seasar2としては2016年9月にEOLを迎えていますが、Mayaaテンプレート自体のサポートは2021年12月現在も継続しています。
元々Seasar2の一部として登場していますが、テンプレート自体はフレームワークに依存していないため、その他のMVCフレームワークでも動作させることが可能みたいです。

Servlet APIのバージョンも4.0まで対応しているみたいなので、比較的新しいバージョンのAPサーバー上でも動作が可能です。
JavaについてもLTSバージョンに対応しているので、Springとの相性としても問題はなさそうです。

Spring MVC側での設定実装

Mayaaテンプレートを動作させるためにSpringのJava Configrationを実装していきます。
基本は以前投稿したこちらと同じような形の設定方法となりますが、一部をMayaaテンプレート用にしていきます。

まず、テンプレートエンジンのライブラリから提供されるServletをWebアプリケーションに登録する必要があるため、MayaaServletを登録していきます。
こちらはDispatcherServletの初期化などを行うAbstractAnnotationConfigDispatcherServletInitializerを拡張することで登録することができるので、以下のように登録します。

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

import org.seasar.mayaa.impl.MayaaServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    // 省略

    private void addStaticHtmlFilesHandlingServlet(ServletContext servletContext) {
        // MayaaServletを登録
        ServletRegistration.Dynamic servlet = servletContext.addServlet("HtmlsServlet", new MayaaServlet());
        servlet.setLoadOnStartup(1);
        servlet.addMapping("*.html");
    }

}

MayaaServlet以外としては、ViewResolverの設定を別途行う必要があります。
こちらはSpring側から標準提供されているWebMvcConfigurationSupportを拡張する形で以下のようにBean登録をしていきます。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
class WebMvcConfig extends WebMvcConfigurationSupport {

    private static final String CHARACTER_ENCODING = "UTF-8"; // 適宜変更が必要
    private static final String VIEWS = "/WEB-INF/view"; // 適宜変更が必要

    // 省略

    @Override
    protected void configureViewResolvers(ViewResolverRegistry registry) {
        // ViewResolverを登録
        InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
        resolver.setViewClass(InternalResourceView.class);
        resolver.setPrefix(VIEWS);
        resolver.setSuffix(".html");
        resolver.setContentType(CHARACTER_ENCODING);
        registry.viewResolver(resolver);
    }

}

Spring側に対してMayaaテンプレート上で設定する必要がある箇所としては以上となります。
あとは通常のSpring同様でアプリケーションの機能上で必要な設定をしていけばOKです。

実際にMayaaテンプレートを実装してアプリケーションを起動

Spring側の設定が終わったら、リクエストを受け付けるためのSpringのコントローラークラスとMayaaテンプレートを実装してみましょう。
今回はHello Worldレベルの実装で紹介させていただきます。

Controller

Hello Worldレベルの実装なので、今回はリクエストを受け付けてModelにタイトルの値を詰めて画面にレンダリングするだけにします。

@Controller
public class TestController {

    @RequestMapping(("/hello"))
    public ModelAndView hello(ModelMap model) {
        ModelAndView mv = new ModelAndView("/hello");
        model.addAttribute("title", "Hello World!!");
        return mv;
    }

}

HTMLファイル

このファイルがテンプレートになっていて、デザイナーが画面のデザインを考えて実装するものになります。
各要素ごとに付与するid属性がMayaaファイルと紐づく仕組みとなっており、表示される値や属性が切り替わる仕組みとなっています。
タイトルを表示するテンプレートなので、こちらもシンプルに書いています。

hello.html
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <p id="title">ダミータイトル</p>
</body>
</html>

Mayaaファイル

プログラマーが実装するファイルで、サーバーサイド側からレンダリングされる値を埋め込むことができます。
m:id属性がHTML側のテンプレートと紐づくことで、テンプレート上に値が差し込まれる仕組みとなっています。
基本的な構文はHTMLと同じですが、Mayaaから提供のテンプレートを組み込見ながら実装していきます。

hello.mayaa
<?xml version="1.0" encoding="UTF-8"?>
<m:mayaa xmlns:m="http://mayaa.seasar.org">
    <m:write m:id="title" value="${title }" />
</m:mayaa>

ここまで実装したらAPサーバーを起動して、設定しているパス(今回の場合は/hello)にアクセスすると画面が表示されると思います。

localhost_8094_mayaa_hello - Google Chrome 2021-12.png

まとめ

今回はSpringとMayaaテンプレートという珍しい組み合わせでの実装を試してみました。
設定実装部分や動作するまでに試行錯誤はありましたが、無事動作ができてよかったです。

また、今回は実装していませんがMayaaファイルにはJSPのタグを実装することが可能です。
カスタマイズされたタグについても使用できるため、過去にはJSPから移行する検討をされていたこともあるそうです。

参考

8
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
2