2015-11-17 103 views
1

我想知道为什么百里香没有正确编码我的页面。因此,这里是我的标记Spring Boot Thymeleaf字符编码为UTF-8

<!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(); 
    } 

..some otherp性能

,这里是什么正在呈现

enter image description here

回答

1

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

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

的ViewResolver负责提交到HTTP响应,所以它的编码设置为UTF-8应该可以正常工作。