Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Thymeleaf Character Encoding to UTF-8

I was wondering why is thymeleaf not encoding my pages properly. So here's my markup

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

And my application properties

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

and my 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 properties

and here's what's being rendered

enter image description here

like image 693
user962206 Avatar asked Nov 17 '15 22:11

user962206


1 Answers

Your view resolver's character encoding should be set to UTF-8:

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

ViewResolver is responsible for committing the response to http, so setting its encoding to utf-8 should work fine.

like image 169
Nikhil Sahu Avatar answered Sep 24 '22 15:09

Nikhil Sahu