形成丢失的波兰人物

时间:2016-01-22 13:58:19

标签: java mysql spring character-encoding thymeleaf

我试图将“ą,ę,ć,ł”等波兰语字符添加到我的MySQL数据库中。我已经完成的步骤:

  1. 在我的MySQL数据库中设置“方法比较铭文”到utf8_unicode_ci
  2. 为所有varchar字段Method comparing inscription设置为utf8_unicode_ci
  3. application.properties集合中:
  4. spring.datasource.url: jdbc:mysql://localhost:3306/database?characterEncoding=UTF-8
    spring.mandatory-file-encoding=UTF-8
    spring.http.encoding.charset=UTF-8
    spring.http.encoding.enabled=true
    spring.http.encoding.force=true
    spring.thymeleaf.mode=HTML5
    spring.thymeleaf.encoding=UTF-8
    spring.thymeleaf.content-type=text/html
    spring.datasource.sqlScriptEncoding=UTF-8
    
    1. 要确保在所有HTML文件中添加<head></head>括号<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    2. 像这样设置配置文件:
    3. @Configuration
      public class Config extends WebSecurityConfigurerAdapter {
      
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              CharacterEncodingFilter filter = new CharacterEncodingFilter();
              filter.setEncoding("UTF-8");
              filter.setForceEncoding(true);
          }
      }
      

      当我尝试在控制器中添加一些波兰语单词时,例如player.setName("ĆŁĘ") - 这没关系。正确插入了数据库中的名称。

      但是当我从Thymeleaf手中接过控制器的name时,它会返回ÄÅÄ而不是CŁĘ。我的表格服务员Thymeleaf看起来像这样:

      <form action="#" th:action="@{/editPlayer}" th:object="${player}" method="post">
          <div class="info">Name:</div>
          <div class="error" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"/>
          <input type="text" th:field="*{name}" placeholder="Name" th:class="${#fields.hasErrors('name')}? 'error'"/><br/>
      
          <input type="button" id="cancel" class="button2 button-cancel" value="CANCEL"/>
          <input type="submit" class="button button-submit" value="SUBMIT"/>
      </form>
      

      我不知道还需要做些什么来从Thymeleaf那里获得正确的角色。

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。它采取了一些步骤:

  1. 向我的@EnableWebSecurity班级
  2. 添加注释Config
  3. http.addFilterBefore(filter, CsrfFilter.class)
  4. 下写下filter.setForceEncoding(true)
  5. 创建课程ApplicationSecurityInitializer
  6. public class ApplicationSecurityInitializer extends AbstractSecurityWebApplicationInitializer {
    
        @Override
        protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
            super.beforeSpringSecurityFilterChain(servletContext);
            FilterRegistration.Dynamic characterEncodingFilter;
            characterEncodingFilter = servletContext.addFilter("encodingFilter",
                    new CharacterEncodingFilter());
            characterEncodingFilter.setInitParameter("encoding", "UTF-8");
            characterEncodingFilter.setInitParameter("forceEncoding", "true");
            characterEncodingFilter.addMappingForUrlPatterns(null, false, "/*");
        }
    }
    
    1. 深吸一口气,保持冷静:)