我试图将“ą,ę,ć,ł”等波兰语字符添加到我的MySQL数据库中。我已经完成的步骤:
utf8_unicode_ci
。varchar
字段Method comparing inscription
设置为utf8_unicode_ci
application.properties
集合中: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
<head></head>
括号<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
@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那里获得正确的角色。
答案 0 :(得分:1)
我找到了解决方案。它采取了一些步骤:
@EnableWebSecurity
班级Config
http.addFilterBefore(filter, CsrfFilter.class)
filter.setForceEncoding(true)
ApplicationSecurityInitializer
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, "/*");
}
}