解析模板"index"时出错,模板可能不存在,或者任何已配置的模板解析器都可能无法访问

Dre*_*208 22 thymeleaf spring-boot

之前已经问过这个问题,但我没有解决我的问题而且我得到了一些奇怪的功能.

如果我将index.html文件放在静态目录中,如下所示:

在此输入图像描述

我在浏览器中收到以下错误:

在此输入图像描述

在我的控制台中:

[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>".
Run Code Online (Sandbox Code Playgroud)

但是,如果我将index.html文件移动到templates目录中,我的浏览器中会出现以下错误: 在此输入图像描述

在此输入图像描述

我添加了我的视图解析器:

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

}
Run Code Online (Sandbox Code Playgroud)

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

在这一点上,我不知道发生了什么.谁能给我一些建议?

------更新--------

我错过了index.html中的拼写错误,但我仍然遇到同样的错误

<!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>
Run Code Online (Sandbox Code Playgroud)

San*_*jay 16

index.html应该在里面templates,据我所知.所以,你的第二次尝试是正确的.

但是,正如错误消息所示,index.html看起来有一些错误.例如,在第三行中,meta标签应该是实际head标签,我想.


小智 14

在控制台告诉你这是与登录冲突.我认为你也应该在index.html百万美元中声明.就像是:

    <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>
Run Code Online (Sandbox Code Playgroud)


Moh*_*ngh 11

检查的名称

模板

夹.它应该是模板而不是模板(没有s).


obu*_*oon 10

我是春天的新手,花了一个小时试图解决这个问题。

去 --- > application.properties

添加这些:

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
Run Code Online (Sandbox Code Playgroud)


小智 8

这可以通过在 application.properties 中复制以下代码来解决

spring.thymeleaf.enabled=false
Run Code Online (Sandbox Code Playgroud)


小智 6

这让我成功!

prefix: classpath:/templates/
Run Code Online (Sandbox Code Playgroud)

检查您的 application.yml


小智 5

如果您遇到此问题并且一切看起来都很好,请尝试从 IDE 使缓存无效/重新启动。这将解决大多数情况下的问题。