以前から、Webアプリケーションを
- Spring
- Hibernate
- Velocity
の組み合わせで作ってみたいと思っていました。
Spring2.5のマニュアルhttp://static.springframework.org/spring/docs/2.5.x/reference/mvc.html#mvc-coc
を見たときには感動しました。
これだ!と思ってSpring2.5のサンプルsamples/showcases/mvc-conventionの例題を動かしてみました。
規約(Convention)に基づいて作成すれば、コントローラのメソッドやビューを簡単に作成できます。
すぐにビューにVelocityを使えるように設定を変更しました。
マニュアルには、
!--
This bean sets up the Velocity environment for us based on a root path for templates.
Optionally, a properties file can be specified for more control over the Velocity
environment, but the defaults are pretty sane for file based template loading.
!--
View resolvers can also be configured with ResourceBundles or XML files. If you need
different view resolving based on Locale, you have to use the resource bundle resolver.
だれかVelocityで日本語をうまく表示した人はいないかとネットで検索しました。
キーワードにSpring MVC Velocityとすると
- http://www.chikkun.com/computer/java/study/springMVC1.html
が見つかりました。
<bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath">
<value>WEB-INF/vm/</value>
</property>
<property name="configLocation">
<value>/WEB-INF/velocity.properties</value>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="contentType">
<value>text/html; charset=Shift_JIS</value>
</property>
<property name="cache">
<value>false</value>
</property>
<property name="suffix">
<value>.vm</value>
</property>
<property name="exposeSpringMacroHelpers">
<value>true</value>
</property>
<property name="toolboxConfigLocation">
<value>/WEB-INF/toolbox.xml</value>
</property>
</bean>
}}
とありましたが、これだとフォームの文字入力で文字化けが発生してしまいます。
そこで、試行錯誤の結果以下のようにしました。
- vmファイルの文字コードはすべてUTF-8を使用する
- ブラウザーにもcontentTypeをUTF-8と通知する
- Velocityのプロパティでinput.encoding, output.encodingにUTF-8を指定する
- フィルターとしてCharacterEncodingFilterをweb.xmlに追加し、入力文字コードをUTF-8に変換する
としました。
SpringのapplicationContextに
<bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="WEB-INF/velocity/" />
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="suffix" value=".vm" />
<property name="dateToolAttribute">
<value>dateTool</value>
</property>
<property name="numberToolAttribute">
<value>numberTool</value>
</property>
</bean>
}}
を定義し、web.xmlに
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
}}
を追加します。
これでめでたく日本語が扱えるようになりました。