Spring Boot Thymeleaf字符编码为UTF-8

时间:2015-11-17 22:42:15

标签: java spring utf-8 spring-boot 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>

我的应用程序属性

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

和我的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();
    }

..一些其他属性

这是正在呈现的内容

enter image description here

1 个答案:

答案 0 :(得分:2)

您的查看解析程序的字符编码应设置为UTF-8:

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

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