I also had a problem with displaying UTF-8 characters (special characters like æ. Ø and å etc.) when using the spring frame and freemarker template.
What I did was.
1. Make sure your .ftl page is encoded using utf-8. It is important that a page that is not encoded in UTF-8 can display incorrect numbers, even if you have all the other requirements. Check your IDE settings to see what encoding your files are set to by default. However, I think that today Eclipse and NetBeans install all UTF-8 encoded files as standard. You must ensure that it encodes UTF-8 without specification.
2. Include the meta tag in the template file to set the encoding. In the template file (.ftl) that contains the <head> , set <meta> with the charset="UTF-8" attribute. This is if you are using HTML 5. If you are using xhtml or HTML 4, your meta tag should look like this:
- HTML 5
<meta charset="UTF-8" /> - HTML 4 / XHTML
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
3. Ensure that you set the character encoding filter in the deployment descriptor file . You should filter all incoming / outgoing requests through the character encoding filter. This filter is specified in the deployment descriptor (web.xml / or equivalent JavaApplicationInitializer).
WebApplicationInitializer (Java file)
@Override public void onStartup(ServletContext servletContext) throws ServletException { registerCharacterEncodingFilter(servletContext); } private void registerCharacterEncodingFilter(ServletContext servletContext) { CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter(); encodingFilter.setEncoding("UTF-8"); encodingFilter.setForceEncoding(true); FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("characterEncodingFilter", encodingFilter); characterEncodingFilter.addMappingForUrlPatterns(null, false, "/*"); }
web.xml
<filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
4. Set the FreeMarker character encoding in the configurator and view the resolution . You also need to make all your FreeMarker files standard encoded using UTF-8, this is done by setting their properties in UTF-8 in FreeMarkerConfigurer and FreeMarkerViewResolver. This is set in your spring application context file (I will only show the Java equivalent, since it is the same in the XML file).
@Bean public FreeMarkerConfigurer freemarkerConfig() { FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); freeMarkerConfigurer.setTemplateLoaderPath("/templates/"); freeMarkerConfigurer.setDefaultEncoding("UTF-8"); return freeMarkerConfigurer; } @Bean public FreeMarkerViewResolver viewResolver() { FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver(); viewResolver.setPrefix(""); viewResolver.setSuffix(".ftl"); viewResolver.setCache(false);
Hope this helps you :)