How to set content type in Freemarker views when using Spring MVC?

I am using Sping MVC with freemarker views. I have configured FreeMarkerViewResolver to allow views, and it still works, but now I have problems with the encoding. All my submissions are UTF-8 encoded HTML 5 pages, and I also added <meta charset="UTF-8" /> to the HTML page, but the characters still print with the wrong encoding. I checked the HTTP headers using curl and found this:

 k@jules :~$ curl -I http://localhost:8080/testweb/test.view HTTP/1.1 200 OK Content-Type: */*;charset=ISO-8859-1 

But when I request some non-existent resource (which generates a Tomcat error), I get the following:

 k@jules :~$ curl -I http://localhost:8080/testweb/nothere.html HTTP/1.1 200 OK Content-Type: text/html;charset=utf-8 

So, Tomcat itself returns the correct content type, but Spring MVC Freemarker views do not.

For JSP, I can set the Content-Type to the JSP header, but where can I set it for the freemarker template? I think I need to do this somewhere in the Spring bean configuration, but I cannot find the right place.

+4
source share
2 answers

The view resolver (should be in your dispatcher-servlet.xml ) has a contentType property for this:

 <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="prefix" value=""/> <property name="suffix" value=".ftl"/> <property name="contentType" value="text/html;charset=UTF-8"/> </bean> 
+8
source

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); } /** * Filter all incoming requests with character encoding UTF-8 * @param 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).

 /** * FreeMarker Configurer will help configure different settings of * the FreeMarker template engine. * * @return an object of the FreeMarkerConfigurer class. */ @Bean public FreeMarkerConfigurer freemarkerConfig() { FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); freeMarkerConfigurer.setTemplateLoaderPath("/templates/"); freeMarkerConfigurer.setDefaultEncoding("UTF-8"); return freeMarkerConfigurer; } /** * The View resolver to use when resolving FreeMarker views. * * @return the View Resolver Object used to resolve FreeMarker views. */ @Bean public FreeMarkerViewResolver viewResolver() { FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver(); viewResolver.setPrefix(""); viewResolver.setSuffix(".ftl"); viewResolver.setCache(false); //Set to true during production viewResolver.setContentType("text/html;charset=UTF-8"); return viewResolver; } 

Hope this helps you :)

+5
source

All Articles