将Spring与Thymeleaf一起使用时,我的所有西里尔字符在页面上显示为?????
。
使用
@RequestMapping(value = "/login", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
正如在此处所建议的那样:https://stackoverflow.com/a/11866822/1479414而这里:https://stackoverflow.com/a/12023816/1479414并没有帮助。
如何解决这个问题?
答案 0 :(得分:17)
答案可以找到here:
应为characterEncoding
和templateResolver
明确设置属性ThymeleafViewResolver
:
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
...
<property name="characterEncoding" value="UTF-8"/>
...
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
...
<property name="characterEncoding" value="UTF-8"/>
...
</bean>
答案 1 :(得分:2)
为我工作。 Java配置
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.projectName.controller")
public class MVCConfig implements WebMvcConfigurer, ApplicationContextAware {
@Autowired
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws
BeansException {
this.applicationContext = applicationContext;
}
@Bean
public ViewResolver viewResolver(){
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
thymeleafViewResolver.setTemplateEngine(templateEngine());
thymeleafViewResolver.setCharacterEncoding("UTF-8");
return thymeleafViewResolver;
}
@Bean
public TemplateEngine templateEngine(){
SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
springTemplateEngine.setEnableSpringELCompiler(true);
springTemplateEngine.setTemplateResolver(templateResolver());
return springTemplateEngine;
}
@Bean
public ITemplateResolver templateResolver(){
SpringResourceTemplateResolver springResourceTemplateResolver = new
SpringResourceTemplateResolver();
springResourceTemplateResolver.setApplicationContext(applicationContext);
springResourceTemplateResolver.setPrefix("/WEB-INF/views/");
springResourceTemplateResolver.setTemplateMode(TemplateMode.HTML);
springResourceTemplateResolver.setSuffix(".html");
springResourceTemplateResolver.setCharacterEncoding("UTF-8");
return springResourceTemplateResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
答案 2 :(得分:1)
在配置中
public class SpringConfig implements WebMvcConfigurer {
private final ApplicationContext applicationContext;
@Autowired
public SpringConfig(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html");
templateResolver.setCharacterEncoding("UTF-8");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding("UTF-8");
registry.viewResolver(resolver);
}
}
在 ServletInitializer 中
public class MySpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
return new Filter[] { filter };
}
}
答案 3 :(得分:0)
如果您在视图解析程序的Bean配置中添加了<property name="characterEncoding" value="UTF-8"/>
,但仍然没有以正确的格式显示静态文本,则问题出在properties / resource_bundle文件中。
尝试使用native2ascii工具对UTF-8或非英语字符进行编码。 (它包含在java_home / bin文件夹中。
答案 4 :(得分:-2)
我认为在thymeleaf html页面中你正在使用th:带有html元素的文本,th:text它只显示普通文本,
如果你想在你的百里香html页面上使用特殊章程,那么只需要改变例如
个:utext = “$ {yourcontroller_var}”
或
个:utext = “#{properties_var}”
例如
<div th:utext="${user.name}"> Test! </div> // for your controller variable
并且
<div th:utext="#{message.username}"> UserName! </div> // for your properties variable
在thymeleaf html页面上使用特殊字符时,没有其他任何配置。
希望你能解决问题