2008/03/01からのアクセス回数 16083
以前から、Webアプリケーションを
の組み合わせで作ってみたいと思っていました。
Spring2.5のマニュアルConvention over configuration を見たときには感動しました。
これだ!と思って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. --> <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="/WEB-INF/velocity/"/> </bean> <!-- 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. --> <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> <property name="cache" value="true"/> <property name="prefix" value=""/> <property name="suffix" value=".vm"/> </bean>
とありますが、ブラウザーの文字コードをShift-JISに設定しないと日本語は文字化けして しまいます。
だれかVelocityで日本語をうまく表示した人はいないかとネットで検索しました。 キーワードにSpring MVC Velocityとすると
<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>
とありましたが、これだとフォームの文字入力で文字化けが発生してしまいます。
そこで、試行錯誤の結果以下のようにしました。
としました。 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>
を追加します。 これでめでたく日本語が扱えるようになりました。
この記事は、
皆様のご意見、ご希望をお待ちしております。