簡體   English   中英

Spring Boot Thymeleaf 字符編碼為 UTF-8

[英]Spring Boot Thymeleaf Character Encoding to UTF-8

我想知道為什么 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();
    }

..一些其他屬性

這是正在渲染的內容

在此處輸入圖片說明

您的視圖解析器的字符編碼應設置為 UTF-8:

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

ViewResolver 負責將響應提交給 http,因此將其編碼設置為 utf-8 應該可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM