当前位置: 首页 > 知识库问答 >
问题:

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

弘柏
2023-03-14

这个问题以前被问过,但我没有解决我的问题,我得到了一些奇怪的功能。

如果我将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>".

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

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

在这一点上,我不知道是怎么回事。有人能给我一些建议吗?

我在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>

共有1个答案

翁凯定
2023-03-14

正如我所知,index.html应该在模板中。所以,你的第二次尝试看起来是正确的。

但是,正如错误消息所说,index.html似乎有一些错误。例如。在第三行中,meta标记实际上应该是head标记。

 类似资料: