Roman
Roman

Reputation: 33

Russian text displaying as ??? in my FTL-page

I use Spring MVC and Freemarker 2.3.27 in my web application, when I try to display russian text, I have only "?????". I saved my ftl-page in utf-8 and added meta:

<!DOCTYPE html>
    <html >
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
              integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <!-- Optional theme -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
              integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
        <!-- Latest compiled and minified JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
                integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
        <title>Registration</title>
        <#assign contextPath = request.contextPath>
    </head>
    <body>

                <div class="panel-heading">
                    <h3 class="panel-title">Зарегистрируйтесь</h3>
                </div>
                <div class="panel-body">
                    <form method="post"  action="" name="saveUser">
                        <input name="${_csrf.parameterName}" value="${_csrf.token}" type="hidden">

                        <div class="form-group">
                            <@spring.bind "userForm.username"/>
                            <div class="alert-link">
                               <#if spring.status.error>
                                   <div class="alert alert-danger" role="alert">
                                      <#list spring.status.errorMessages as error>
                                         <li>${error}</li>
                                      </#list>
                                   </div>
                               </#if>
                            </div>
                            <label for="username">Username</label>
                            <input type="text" class="form-control" id="username" placeholder="Username" name="username">
                        </div>

                        ...
                    </form>
                </div>

<body/>
<html/>

Here is my WebConfig:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "aquaplant")
public class WebConfig extends WebMvcConfigurerAdapter {
.....
    @Bean
    public FreeMarkerConfigurer getFreeMarkerConfigurer() {
        FreeMarkerConfigurer configurer = new DefaultFreeMarkerConfigurer();
        configurer.setDefaultEncoding("UTF-8");
        configurer.setTemplateLoaderPaths("/", "/WEB-INF/views");
        return configurer;
    }
    @Bean(name = "messageSource")
    public ReloadableResourceBundleMessageSource getMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:validation");
        messageSource.setDefaultEncoding("UTF-8");
        //messageSource.setUseCodeAsDefaultMessage(true);
        return messageSource;
    }
}

In my entire program I use utf-8. It should work but it doesn't. Can anybody help me?

Upvotes: 1

Views: 277

Answers (1)

Roman
Roman

Reputation: 33

The problem was solved by creating such a filter:

public class CharsetFilter implements Filter {

    private String encoding;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        encoding = filterConfig.getInitParameter("requestEncoding");
        if (encoding == null) encoding = "UTF-8";
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (request.getCharacterEncoding()==null) {
            request.setCharacterEncoding(encoding);
        }
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}

This filter must be added to servletContext:

public class WebAppInitializer implements WebApplicationInitializer {
    public void onStartup(ServletContext servletContext) throws ServletException {
        CharsetFilter filter = new CharsetFilter();
        servletContext.addFilter("charsetFilter", filter).addMappingForUrlPatterns(null, false, "/*");
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(SpringConfig.class, WebConfig.class);
        context.setServletContext(servletContext);
        ServletRegistration.Dynamic dispatcher11 = servletContext.
                addServlet("dispatcher11", new DispatcherServlet(context));
        dispatcher11.setLoadOnStartup(1);
        dispatcher11.addMapping("/");
    }
}

Upvotes: 2

Related Questions