解析模板“登录”时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问

Posted

技术标签:

【中文标题】解析模板“登录”时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问【英文标题】:Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers 【发布时间】:2018-07-12 04:54:10 【问题描述】:

我正在尝试制作一个 Spring Boot 应用程序,但是,我无法加载第一页。似乎(基于提出的许多类似问题)此错误可能有多种原因,但是,似乎没有一个原因可以解决导致此特定错误的问题。我觉得在 SO 上对这个问题有一些见解不会有什么坏处吗?

目前的错误是:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers

控制器方法

@Controller
public class UserController 
    @Autowired
    private UserService userService;

    @Autowired
    private SecurityService securityService;

    @Autowired
    private UserValidator userValidator;

    @Autowired
    UserRepository userRepository;
  @RequestMapping(value = "/registration", method = RequestMethod.GET)
    public String registration(Model model) 
        model.addAttribute("userForm", new User());

        return "registration";
    

    @RequestMapping(value = "/registration", method = RequestMethod.POST)
    public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult, Model model) 
        userValidator.validate(userForm, bindingResult);

        if (bindingResult.hasErrors()) 
            return "registration";
        

        userService.save(userForm);

        securityService.autologin(userForm.getUsername(), userForm.getPasswordConfirm());

        return "redirect:/welcome";
    

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model, String error, String logout) 
        if (error != null)
            model.addAttribute("error", "Your username and password is invalid.");

        if (logout != null)
            model.addAttribute("message", "You have been logged out successfully.");

        return "login";
    

    @RequestMapping(value = "/", "/welcome", method = RequestMethod.GET)
    public String welcome(Model model) 
        return "welcome";
    

网络配置文件

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter 
    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() 
        return new BCryptPasswordEncoder();
    

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

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception 
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
    

Application.properties

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/arearatingdb?useSSL=false
spring.datasource.username = root
spring.datasource.password = root


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

spring.mvc.view.prefix: /
spring.mvc.view.suffix: .jsp
spring.messages.basename=validation

我的目录设置如下:

【问题讨论】:

【参考方案1】:

请更新 spring.thymeleaf.enabled=false 在 application.properties 中

【讨论】:

以上是关于解析模板“登录”时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问的主要内容,如果未能解决你的问题,请参考以下文章

解析模板 [] 时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问

解析模板时出错 [/],模板可能不存在或可能无法被任何已配置的模板解析器访问

解析模板“欢迎”时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问

解析模板 [registration] 时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问

解析模板“home”时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问

解析模板“/index.html”时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问