[[FrontPage]] #contents 2008/03/01からのアクセス回数 &counter; * Spring-MVCのビューにVelocityを使いたい [#r723b4bc] 以前から、Webアプリケーションを - Spring - Hibernate - Velocity の組み合わせで作ってみたいと思っていました。 Spring2.5のマニュアル[[Convention over configuration>http://static.springframework.org/spring/docs/2.5.x/reference/mvc.html#mvc-coc]] を見たときには感動しました。 これだ!と思ってSpring2.5のサンプルsamples/showcases/mvc-conventionの例題を動かしてみました。 規約(Convention)に基づいて作成すれば、コントローラのメソッドやビューを簡単に作成できます。 すぐにビューにVelocityを使えるように設定を変更しました。 マニュアルには、 #pre{{ <!-- 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とすると - [[ずっこけChikkunのお部屋>http://www.chikkun.com/computer/java/study/springMVC1.html]] が見つかりました。 #pre{{ <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に #pre{{ <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に #pre{{ <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> }} を追加します。 これでめでたく日本語が扱えるようになりました。 * コメント [#cf8a595c] この記事は、 #vote(おもしろかった[25],そうでもない[1],わかりづらい[6]) #vote(おもしろかった[25],そうでもない[1],わかりづらい[7]) 皆様のご意見、ご希望をお待ちしております。 - 引用元のサイト名が間違ってます。「わかりずらい」ではなく「わかりづらい」です。 -- [[通りすがり]] &new{2013-07-18 (木) 15:59:00}; - ご指摘ありがとうございます。ずっこけChikkunのお部屋ですね。訂正してお詫びします。 -- [[竹本 浩]] &new{2013-07-18 (木) 20:10:11}; #comment_kcaptcha