Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers

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>
like image 727
Mike3355 Avatar asked Aug 11 '15 14:08

Mike3355


3 Answers

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.

like image 84
Sanjay Avatar answered Nov 01 '22 02:11

Sanjay


Check for the name of the

templates

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

like image 20
Mohit Singh Avatar answered Nov 01 '22 01:11

Mohit Singh


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>
like image 22
David_Garcia Avatar answered Nov 01 '22 01:11

David_Garcia