テンプレート(html)自体の文字コードはUTF-8になっています。
設定したこと
①Serversのweb.xmlと、プロジェクト自体のWEB-INF/web.xmlに以下を追記
lang
1 <filter> 2 <filter-name>characterEncodingFilter</filter-name> 3 <filter-class> 4 org.springframework.web.filter.CharacterEncodingFilter 5 </filter-class> 6 <init-param> 7 <param-name>encoding</param-name> 8 <param-value>UTF-8</param-value> 9 </init-param> 10 <init-param> 11 <param-name>forceEncoding</param-name> 12 <param-value>true</param-value> 13 </init-param> 14 </filter> 15 <filter-mapping> 16 <filter-name>characterEncodingFilter</filter-name> 17 <url-pattern>/*</url-pattern> 18 </filter-mapping>
②Serversのserver.xmlのConnectorタグに以下を追記
lang
1URIEncoding="UTF-8" useBodyEncodingForURI="true"
③HTMLファイルのHTMLタグをlang="en"からlang="ja"に変更
④ <beans:bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">に以下を追記
lang
1<beans:property name="characterEncoding" value="utf-8" /> 2<beans:property name="cacheable" value="false" />
****************************
そのうえで試したこと
lang
1@RequestMapping(value = "/top", method = RequestMethod.GET) 2public String top(Locale locale, Model model) { 3 ~省略~ 4 model.addAttribute("test", "あああ" ); →top.hmtl内のthymeleafで出力した日本語が「???」のまま 5 return "top"; →top.hmtl内の直書きした日本語も「???」のまま 6}
アノテーションに「produces="text/plain;charset=UTF-8"」または「produces="text/html;charset=UTF-8"」を追加しても変化なし
lang
1@RequestMapping(value = "/top", method = RequestMethod.GET, produces="text/plain;charset=UTF-8") 2public String top(Locale locale, Model model) { 3 ~省略~ 4 model.addAttribute("test", "あああ" ); →top.hmtl内のthymeleafで出力した日本語が「???」のまま 5 return "top"; →top.hmtl内の直書きした日本語も「???」のまま 6}
以下のようにして直接文字列を返した場合は文字化けしない。
lang
1@ResponseBody 2@RequestMapping(value = "/top", method = RequestMethod.GET, produces="text/html;charset=UTF-8") 3public String top(Locale locale, Model model) { 4 ~省略~ 5 return "あああ"; →「あああ」が正常に画面に表示される 6}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。