M
M
MaxLich2018-08-29 22:17:51
Java
MaxLich, 2018-08-29 22:17:51

Why does the Cyrillic alphabet from the HTML form come to the program in the form of krakozyabry?

Hello.
I am making a pet project in the form of a CRUD web application - an English-Russian dictionary.
On the back - Java.
On the front is pure html generated by freemarker.
I am using spring mvc.
I enter the data into the form - the text in Russian, I check what comes to the controller, and there are all sorts of bugs.
Here is a screenshot.
5b86ef7ae9ca2956421663.png
This is what is displayed in IDEA during the debug. The breakpoint is just on the controller that intercepts this request.
5b86f0daf2c5b783183013.png
All pages are in utf-8. Additionally, I set this in the spring configuration:

@Bean
    public ViewResolver getViewResolver() {
        FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver();
        freeMarkerViewResolver.setOrder(1);
        freeMarkerViewResolver.setPrefix("");
        freeMarkerViewResolver.setSuffix(".ftl");
        freeMarkerViewResolver.setContentType("text/html; charset=UTF-8");
        return freeMarkerViewResolver;
    }

    @Bean
    public FreeMarkerConfigurer getFreeMarkerConfigurer() {
        FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
        freeMarkerConfigurer.setTemplateLoaderPaths("/", "/WEB-INF/views/");
        freeMarkerConfigurer.setDefaultEncoding("UTF-8");
        return freeMarkerConfigurer;
    }

The page itself, from where I send the data:
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Добавление нового слова</title>
</head>
<body>

<h1>Добавление нового слова</h1>
<form name="record" action="/my-web-dictionary/dictionary-add-word/" method="post" accept-charset="UTF-8">
    Слово:<br>
    <input title="Слово" type="text" name="word" style="width: 418px;"><br>
    Значение:<br>
    <textarea title="Значение слова" rows="10" cols="50" name="definition"></textarea><br>
    <input type="submit" value="OK">
</form>

<br>
<a href="/my-web-dictionary/">Back</a>

</body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxLich, 2018-08-30
@MaxLich

Moskus comment helped . It was necessary to add code to WebAppInitializer. As a result, this class looks like this:

public class WebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(SpringConfig.class, WebConfig.class);
        context.setServletContext(servletContext);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);

        FilterRegistration.Dynamic filterRegistration = servletContext
                .addFilter("characterEncodingFilter", characterEncodingFilter);
        filterRegistration.addMappingForUrlPatterns(null, false, "/*");
    }
}

Added the following piece:
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);

        FilterRegistration.Dynamic filterRegistration = servletContext
                .addFilter("characterEncodingFilter", characterEncodingFilter);
        filterRegistration.addMappingForUrlPatterns(null, false, "/*");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question