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

时间:2015-08-11 14:19:54

标签: spring-boot thymeleaf

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

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

enter image description here

我的浏览器出现以下错误:

enter image description here

在我的控制台中:

[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>".

但是,如果我将index.html文件移动到templates目录中,我的浏览器会出现以下错误: enter image description here

enter image description here

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

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

此时我不知道发生了什么。谁能给我一些建议?

------ -------- UPDATE

我错过了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>

12 个答案:

答案 0 :(得分:15)

在控制台中告诉您这与登录有冲突。我认为你也应该在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>

答案 1 :(得分:14)

正如我所知,

index.html应位于templates内。所以,你的第二次尝试看起来是正确的。

但是,正如错误消息所示,index.html看起来有些错误。例如。我认为,在第三行中,meta标记应该是head标记。

答案 2 :(得分:7)

检查

的名称
  

模板

文件夹。它应该是模板而不是模板(没有s)。

答案 3 :(得分:4)

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

spring.thymeleaf.enabled=false

答案 4 :(得分:2)

这使我成功!

prefix: classpath:/templates/

检查您的application.yml

答案 5 :(得分:1)

由于缺少结束标记,大多数情况下可能会发生此错误。此外,您可以使用以下依赖项来解决此问题,同时支持旧版HTML格式化。

因为你的代码charset =“UTF-8”&gt;这里没有关闭元标记。

<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>                                 
</dependency>

答案 6 :(得分:1)

如果您遇到此问题但一切看起来都不错,请尝试从IDE中无效缓存/重启。这将解决大多数情况下的问题。

答案 7 :(得分:0)

对我来说,问题是因为区分大小写。我使用的是~{fragments/Base}而不是~{fragments/base}(文件名是base.html

我的开发环境是Windows,但是托管应用程序的服务器是Linux,所以在开发过程中没有看到此问题,因为Windows的路径不区分大小写。

答案 8 :(得分:0)

可能是由于某些异常(例如将NUMERIC解析为String或反之亦然)引起的。

请验证单元格值是否为null或处理异常并查看。

最好, 沙希德

答案 9 :(得分:0)

我刚来春天,就花了一个小时试图弄清楚这个问题。

转到---> application.properties

添加这些:

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

答案 10 :(得分:0)

我浪费了 2 个小时调试这个问题。

虽然我在正确的位置(在资源/模板/内)有模板文件,但我一直收到同样的错误。

原来是因为我在我的项目中创建了一些额外的包。例如,所有控制器文件都在“控制器”包中。

我对 Spring Initializr 自动生成的文件做了同样的事情。

我不明白为什么会发生这种情况,

但是当我将 ServletInitializer 文件和用 @SpringBootApplication 注释的文件移回项目的根目录时,错误消失了!

答案 11 :(得分:0)

如果模板名称以前导斜杠开头,也可能会出现错误消息:

return "/index";

在 IDE 中,文件被成功解析,路径带有两个斜杠:

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'

在生产系统上,模板被打包到 jar 中,带有两个斜杠的解析不起作用并导致相同的错误消息。

✅ 省略前导斜线:

return "index";
相关问题