Java Spring Boot将ELEAF字符编码转换为UTF-8

Java Spring Boot将ELEAF字符编码转换为UTF-8,java,spring,utf-8,spring-boot,thymeleaf,Java,Spring,Utf 8,Spring Boot,Thymeleaf,我想知道为什么thymeleaf没有正确地编码我的页面。这是我的标记 <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <meta charset="utf-8"/> <head> <meta charset="UTF-8" /> <meta http-equiv="Content-Type: text/

我想知道为什么thymeleaf没有正确地编码我的页面。这是我的标记

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <meta charset="utf-8"/>
    <head>
  <meta charset="UTF-8" />
  <meta http-equiv="Content-Type: text/html;charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta name="description" content="" />
  <meta name="author" content="" />
  <title>Gowlook CMS</title>
  <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,400italic,700,800" rel="stylesheet" type="text/css" />
  <link href="http://fonts.googleapis.com/css?family=Raleway:100" rel="stylesheet" type="text/css" />
  <link href="http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,700" rel="stylesheet" type="text/css" />

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" />
  <link rel="stylesheet" href="/fonts/font-awesome-4.4.0/css/font-awesome.min.css" />

  <!-- Custom styles for this template -->
  <link rel="stylesheet" href="/css/bootstrap.css" />
</head>
    <body>
    <div th:replace="header :: head-nav"></div>
    <div id="wrapper">
        <div class="container-fluid">
            <form action="/category/save" method="POST" th:object="${catFeatGroupForm}">
                <input type="hidden" th:field="*{categoryId}" th:value="*{categoryId}"/>
                <div th:each="catFeatGroup,status : *{catFeatGroupList}" class="form-group">
                    <label>Position</label><input type="number" th:field="*{catFeatGroupList[__${status.index}__].position}"  th:value="${catFeatGroup.position}" />
                    <label>Name</label> <input th:field="*{catFeatGroupList[__${status.index}__].name}" th:value="${catFeatGroup.name}" />
                    <input type="hidden" th:field="*{catFeatGroupList[__${status.index}__].id}" th:value="${catFeatGroup.id}"/>
                    <a th:href="|/category/group/features/${catFeatGroup.id}|">Edit Group Features</a>
                </div>
                <button type="submit" class="btn btn-default">Submit</button>
            </form>
        </div>
    </div>
    <div th:replace="footer :: foot"></div>
    </body>
    </html>
还有我的Application.java

@SpringBootApplication
@EnableJpaRepositories(basePackages = {"com.app.comparator.repository",
        "com.app.comparator.repository.product"
})
@EntityScan(basePackages =
        {
                "com.app.comparator.domain",
                "com.app.comparator.domain.product",
                "com.app.comparator.domain.feature"
        })
@ComponentScan(basePackages={"com.app"})
@EnableAutoConfiguration(exclude = { SolrAutoConfiguration.class })
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class Application extends WebSecurityConfigurerAdapter {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).banner(new Banner() {
            @Override
            public void printBanner(Environment environment, Class<?> aClass, PrintStream printStream) {
                printStream.append(stringBanner());
            };
        }).run();
    }



    @Override
    protected void configure(HttpSecurity http) throws Exception {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceEncoding(true);
        http.addFilterBefore(filter,CsrfFilter.class);
        http.csrf().disable();
    }
@springboot应用程序
@EnableJpaRepositories(basePackages={“com.app.comparator.repository”,
“com.app.comparator.repository.product”
})
@EntityScan(基本软件包)=
{
“com.app.comparator.domain”,
“com.app.comparator.domain.product”,
“com.app.comparator.domain.feature”
})
@ComponentScan(basePackages={“com.app”})
@EnableAutoConfiguration(exclude={SolrAutoConfiguration.class})
@EnableGlobalMethodSecurity(Prespenabled=true)
@启用Web安全性
公共类应用程序扩展WebSecurity配置适配器{
公共静态void main(字符串[]args){
新的SpringApplicationBuilder(Application.class).banner(新的banner(){
@凌驾
public void printBanner(环境,类aClass,PrintStream PrintStream){
append(stringBanner());
};
}).run();
}
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
CharacterEncodingFilter=新的CharacterEncodingFilter();
设置编码(“UTF-8”);
filter.setForceEncoding(true);
http.addFilterBefore(filter,CsrfFilter.class);
http.csrf().disable();
}
…其他一些属性

spring.jpa.show-sql=true
spring.jpa.generate-ddl=true

spring.jpa.hibernate.ddl-auto=update

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
security.enable-csrf=false
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5


security.basic.enabled=true


# HTTP encoding (HttpEncodingProperties)
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
下面是正在呈现的内容


视图解析器的字符编码应设置为UTF-8:

@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setTemplateEngine(templateEngine());
    resolver.setCharacterEncoding("UTF-8");
    return resolver;
}

ViewResolver负责将响应提交到http,因此将其编码设置为utf-8应该可以正常工作。

那么什么是“templateEngine()”?@JelleBlaauw它是一个Spring bean。它是Thymeleaf使用的模板引擎。