java 带有 Thymeleaf 的 utf8 字符集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36397203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:23:48  来源:igfitidea点击:

utf8 charset with Thymeleaf

javaspringspring-mvcutf-8thymeleaf

提问by Andremoniy

When using Spring with Thymeleaf all my Cyrillic characters are shown as ?????on pages.

将 Spring 与 Thymeleaf 一起使用时,我的所有西里尔文字符都显示为?????页面上。

Using

使用

@RequestMapping(value = "/login", method = RequestMethod.GET, produces = "text/html; charset=utf-8")

@RequestMapping(value = "/login", method = RequestMethod.GET, produces = "text/html; charset=utf-8")

as it was suggested here: https://stackoverflow.com/a/11866822/1479414and here: https://stackoverflow.com/a/12023816/1479414doesn't help.

正如这里建议的那样:https: //stackoverflow.com/a/11866822/1479414和这里:https: //stackoverflow.com/a/12023816/1479414没有帮助。

How to solve this issue?

如何解决这个问题?

回答by Andremoniy

The answer can be found here:

答案可以在这里找到:

Property characterEncodingshould be explicitly set for templateResolverand ThymeleafViewResolver:

属性characterEncoding应明确设置为templateResolverThymeleafViewResolver

<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>

回答by Мумин Норматов

working for me. java config

对我来说有效。配置文件

@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/");
   }
}

回答by ranjan

If you have added <property name="characterEncoding" value="UTF-8"/>in the bean configuration for view resolver and still text are not shown in correct format then the problem is in the properties/resource_bundle files.

如果您<property name="characterEncoding" value="UTF-8"/>为视图解析器添加了 bean 配置,但仍然没有以正确的格式显示文本,则问题出在 properties/resource_bundle 文件中。

Try encoding UTF-8 or non-English characters with native2ascii tool. (its included in the java_home/bin folder.

尝试使用 native2ascii 工具对 UTF-8 或非英语字符进行编码。(它包含在 java_home/bin 文件夹中。

回答by Ravi Kavaiya

I think in thymeleaf html page you are using th:text with html element, th:text it just display normal text,

我认为在 thymeleaf html 页面中,您使用的是 th:text 和 html 元素,th:text 它只显示普通文本,

if you want use special charter with your thymeleaf html page so just need to change for example

如果您想在 thymeleaf html 页面中使用特殊章程,则只需更改例如

th:utext="${yourcontroller_var}"

th:utext="${yourcontroller_var}"

or

或者

th:utext="#{properties_var}"

th:utext="#{properties_var}"

For example

例如

<div th:utext="${user.name}"> Test! </div> // for your controller variable 

And

<div th:utext="#{message.username}"> UserName! </div> // for your properties variable 

Nothing do other configuration for using special character with thymeleaf html page.

没有对 thymeleaf html 页面使用特殊字符进行其他配置。

Hope you will fix your problem

希望你能解决你的问题