59

This question has been asked before but I did not solve my problem and I getting some weird functionality.

If I put my index.html file in the static directory like so:

enter image description here

I get the following error in my browser:

enter image description here

And in my console:

[THYMELEAF][http-nio-8080-exec-3] Exception processing template "login": 
Exception parsing document: template="login", line 6 - column 3
2015-08-11 16:09:07.922 ERROR 5756 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].
[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] 
in context with path [] threw exception [Request processing failed; nested 
exception is org.thymeleaf.exceptions.TemplateInputException: Exception 
parsing document: template="login", line 6 - column 3] with root cause

org.xml.sax.SAXParseException: The element type "meta" must be terminated by 
the matching end-tag "</meta>".

However if I move my index.html file into the templates directory I get the following error in my browser: enter image description here

enter image description here

I have added my view resolvers:

@Controller
@EnableWebMvc
public class WebController extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/results").setViewName("results");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/form").setViewName("form");
    }

    @RequestMapping(value="/", method = RequestMethod.GET)
    public String getHomePage(){
        return "index";
    }

    @RequestMapping(value="/form", method=RequestMethod.GET)
    public String showForm(Person person) {
        return "form";
    }

    @RequestMapping(value="/form", method=RequestMethod.POST)
    public String checkPersonInfo(@Valid Person person, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return "form";
        }
        return "redirect:/results";
    }

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("templates/");
        //resolver.setSuffix(".html");
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

WebSecurityConfig.java

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/", "/index").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
               .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<meta>
    <meta> charset="UTF-8">
    <title></title>
</head>
<body>

<h1>Welcome</h1>

<a href="../../login.html"><span>Click here to move to the next page</span></a>

</body>

</html>

At this point I do not know what is going on. Can anyone give me some advice?

Update

I missed a typo in index.html, but I am still getting the same errors

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta> charset="UTF-8">
    <title></title>
</head>
<body>

<h1>Welcome</h1>

<a href="../../login.html"><span>Click here to move to the next page</span></a>

</body>

</html>
Mike3355
  • 11,305
  • 24
  • 96
  • 184

22 Answers22

29

Check for the name of the

templates

folder. it should be templates not template(without s).

Mohit Singh
  • 5,977
  • 2
  • 24
  • 25
25

index.html should be inside templates, as I know. So, your second attempt looks correct.

But, as the error message says, index.html looks like having some errors. E.g. the in the third line, the meta tag should be actually head tag, I think.

Sanjay
  • 8,755
  • 7
  • 46
  • 62
23

In the console is telling you that is a conflict with login. I think that you should declare also in the index.html Thymeleaf. Something like:

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:th="http://www.thymeleaf.org" 
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
    
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>k</title> 
</head>
David_Garcia
  • 622
  • 7
  • 15
20

I am new to spring spent an hour trying to figure this out.

go to --- > application.properties

add these :

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
endlessCode
  • 1,255
  • 4
  • 21
  • 36
9

this can be resolved by copying the below code in application.properties

spring.thymeleaf.enabled=false
slfan
  • 8,950
  • 115
  • 65
  • 78
Shivu B Sasanur
  • 109
  • 1
  • 5
6

this make me success!

prefix: classpath:/templates/

check your application.yml

wonder
  • 61
  • 1
  • 1
5

If you are facing this issue and everything looks good, try invalidate cache/restart from your IDE. This will resolve the issue in most of the cases.

Deni Simon
  • 161
  • 2
  • 7
3

this error probably is occurred most of the time due to missing closing tag. and further you can the following dependency to resolve this issue while supporting legacy HTML formate.

as it your code charset="UTF-8"> here is no closing for meta tag.

<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>                                 
</dependency>
Muhammad
  • 31
  • 1
3

The error message might also occur, if the template name starts with a leading slash:

return "/index";

In the IDE the file was resolved successfully with a path with two slashes:

getResource(templates//index.html)
 Delegating to parent classloader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@2399ee45
 --> Returning 'file:/Users/andreas/git/my-project/frontend/out/production/resources/templates//index.html'

On the productive system, where the template is packed into a jar, the resolution with two slashes does not work and leads to the same error message.

✅ Omit the leading slash:

return "index";
andy
  • 1,035
  • 1
  • 13
  • 35
  • 1
    This is a great answer. More on why the consecutive slashes work in IDE but fail via JAR: [https://bilalkaun.com/2022/04/20/why-consecutive-slashes-fail-in-jar-files/](https://bilalkaun.com/2022/04/20/why-consecutive-slashes-fail-in-jar-files/) – BKaun Apr 20 '22 at 20:27
2

For me the issue was because of Case sensitivity. I was using ~{fragments/Base} instead of ~{fragments/base} (The name of the file was base.html)

My development environment was windows but the server hosting the application was Linux so I was not seeing this issue during development since windows' paths are not case sensitive.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

Adding spring.thymeleaf.mode=HTML5 in the application.properties worked for me. You could try that as well.

JW Geertsma
  • 857
  • 3
  • 13
  • 19
1

I also faced TemplateResolver view error , Adding the spring.thymeleaf.mode=HTML5 in the application.properties worked for me. In case of build created in STS and running for Websphere 9 ..

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
shri
  • 11
  • 1
1

Check the html file is available in src/main/resources/templates folder

Maninder
  • 1,539
  • 1
  • 10
  • 12
1

Try adding @RestController as well, I was facing this same problem, i added both @RestController @Controller, it worked find

Nitish K
  • 51
  • 1
  • 1
  • 6
1

I tried all the solutions here and none of them seemed to be working for me So I tried changing the return statement a little bit and it worked! Seems like the issue with thymleaf not being able to recognize the template file, adding ".html" in the return statement seemed to fix this

 @RequestMapping(value="/", method = RequestMethod.GET)
    public String getHomePage(){
        return "index.html";
    }

Vivek
  • 193
  • 3
  • 3
  • 9
0

It May be due to some exceptions like (Parsing NUMERIC to String or vise versa).

Please verify cell values either are null or do handle Exception and see.

Best, Shahid

Shahid Hussain Abbasi
  • 2,508
  • 16
  • 10
0

I wasted 2 hours debugging this issue.

Althought I had the template file in the right location (within resources/templates/), I kept getting the same error.

It turns out it was because I had created some extra packages in my project. For instance, all controller files were in 'controller' package.

I did the same thing for the files which were automatically generated by Spring Initializr.

I don't understand exactly why this happens,

but when I moved the ServletInitializer file and the one annotated with @SpringBootApplication back to the root of the project, the error went away !

0

For me, including these in the pom.xml CAUSES the exception. Removing it from the pom.xml resolves the issue. (Honestly, I don't know how that happen)

<build>
    <resources>
        <resource>
           <directory>src/main/resources</directory>
           <targetPath>${project.build.outputDirectory}</targetPath>
           <includes>
               <include>application.properties</include>
           </includes>
        </resource>
    </resources>
</build>
0

In my case I had everything else right as suggested above but still it was complaining that "template might not exist or might not be accessible by any of the configured Template Resolvers". On comparing my project with some other sample projects which were working fine, I figured out I was missing

<configuration>
    <addResources>true</addResources>
</configuration>

in spring-boot-maven-plugin. Adding which worked for me. So my plugins section now looks like

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    </plugins>

I am not sure why I needed to add tag to get thymeleaf working though.

nagamanojv
  • 301
  • 5
  • 16
0

Whenever I meet this error I check if my target view folder is inside /src/man/.../resources/templates and if I am not making any typo.

Issa
  • 161
  • 1
  • 9
0

i think you have forgot to end the tag as <meta .... /> or using </meta

RK Shrestha
  • 159
  • 1
  • 3
0

I resolved this error by removing the "/" from the path of html file.

before: return "/shopView/index.html";

after: return "shopView/index.html";

Sachin
  • 21
  • 1
  • 4