FreemarkerResultをちょっといじってみる

WebWork2のFreemarkerResultが使いにくい。何が使いにくいかって言うと、テンプレートの文字コードを自由に設定できないのだ。ソースを見ると、WebWorkのコンフィグのロケールになるようだ。そうすると、WebWorkのコンフィグでUTF-8にすると、テンプレートもUTF-8にしなければならない。もし、最初にMS932などでテンプレートを大量に作ってしまうとあとあと面倒なことになる。
テンプレートファイルを指定する段階でロケールも一意に決まるはずなので融通を利かせたいものだ。そこで、設定で自由に文字コードを変更できるFreemarkerResultを作ってみる。

package foo.bar.hoge;
..
..
public class MyFreemarkerResult
  extends com.opensymphony.webwork.views.freemarker.FreemarkerResult {

  /** テンプレートのエンコーディング */
  private String templateEncoding;

  public void setTemplateEncoding(String templateEncoding) {
    this.templateEncoding = templateEncoding;
  }
  public String getTemplateEncoding() {
    return	templateEncoding;
  }
  ..
  ..
  ..
  public void doExecute(String location, ActionInvocation invocation)
    throws IOException, TemplateException {
  ..
  ..
  ..
  // この行を直す。
  //Template template = configuration.getTemplate(location, deduceLocale());
  Template template = (templateEncoding != null)
          ? configuration.getTemplate(location, locale, templateEncoding)
          : configuration.getTemplate(location, locale);
  ..
  ..
  }
}

なんか、省略だらけだ。で、xwork.xmlには以下のような定義を。


  
    MS932
  

アクションの定義には、

  
    /WEB-INF/pages/test.ftl
  

これでOK。