无法运行具有Thymeleaf支持的Spring Boot WebMVC

曼迪

我进行了很多搜索,但没有找到问题的答案,因此,我在此处发布问题。请查看并向我提出我误解的解决方案。

我已经使用Spring Tool Suite(STS)创建了带有百里香支持的spring boot web mvc项目。当我运行它时,请给我“白色标签错误页面”页面。这意味着找不到映射。

工作量:

WebConfig.java

package com.springthymeleaf.config;

import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

@Configuration
@ComponentScan("com.springthymeleaf")
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter  {

    @Bean
    ServletRegistrationBean servletRegistration(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.addUrlMappings("/console/*");
        return registrationBean;
    }

    //start Thymeleaf specific configuration
    @Bean(name ="templateResolver") 
    public ServletContextTemplateResolver getTemplateResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
//      templateResolver.setPrefix("/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("XHTML");
    return templateResolver;
    }
    @Bean(name ="templateEngine")       
    public SpringTemplateEngine getTemplateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(getTemplateResolver());
    return templateEngine;
    }
    @Bean(name="viewResolver")
    public ThymeleafViewResolver getViewResolver(){
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); 
        viewResolver.setTemplateEngine(getTemplateEngine());
    return viewResolver;
    }
    //end Thymeleaf specific configuration
    @Bean(name ="messageSource")
    public MessageSource getMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("/WEB-INF/i18/thymeleafResource");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
}

SecurityConfiguration.java

package com.springthymeleaf.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequests().antMatchers("/").permitAll();
    }
}

ServletInitializer.java

package com.springthymeleaf;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringThymeLeafApplication.class);
    }

}

SpringThymeLeafApplication.java

package com.springthymeleaf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringThymeLeafApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringThymeLeafApplication.class, args);
    }
}

IndexController.java

package com.springthymeleaf.controllers;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index(){
        return "index";
    }
}

我已经index.html在resources / templates文件夹中创建了文件。仍然我得到那个错误。我在网上进行了很多搜索,但没有得到任何线索。请有人帮我。

目录结构

新奇拉

实际上,Spring Boot是开箱即用地配置Thymeleaf的。它应与以下设置一起使用:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .formLogin().loginPage("/login").defaultSuccessUrl("/").permitAll() // http://docs.spring.io/spring-security/site/docs/4.0.3.RELEASE/reference/htmlsingle/#jc-form
                .and()
            .logout().permitAll(); // http://docs.spring.io/spring-security/site/docs/4.0.3.RELEASE/reference/htmlsingle/#jc-logout
    }

    @Override
    public void configure(WebSecurity web) throws Exception
    {
        web
            .ignoring()
                .antMatchers("/resources/**"/*, ... */);
    }
}

@Controller
public class LoginController
{
    @RequestMapping("/login")
    static String login(Model model)
    {
        return "login";
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

类'ModelAndViewDefiningException'的Spring Webmvc NoClassDefFoundError

来自分类Dev

如何生成Spring WebMVC CRUD API

来自分类Dev

分隔Spring-WS和Spring-Webmvc

来自分类Dev

Spring WebMVC 5 - 基于注解的拦截器

来自分类Dev

将spring-data-rest-repository添加到spring-data-rest-webmvc

来自分类Dev

ComponentScan.basePackageClasses与ComponentScan.basePackages来注册单个Spring webMVC控制器?

来自分类Dev

获取spring-data-rest-webmvc 2.3.0.BUILD_SNAPSHOT的Maven构建错误

来自分类Dev

与Spring-Data-Rest-WebMVC冲突的JSP页面的视图分辨率?

来自分类Dev

在Spring WebMVC应用程序中获取WebContent的相对路径

来自分类Dev

我正在尝试结合 Spring WebMVC 和 Apache CXF,但找不到该服务。为什么?

来自分类Dev

使用Shiro和Spring WebMVC(Java8,Spring 4.x)从WebApp中注销所有仍登录的用户

来自分类Dev

无法使用 Spring Boot thymeleaf 加载图像

来自分类Dev

Spring-Data-Rest:当升级到spring-data-rest-webmvc 2.0.0时,Spring Data Book示例失败

来自分类Dev

Spring WebMVC的标记库:form:checkboxes可以按预期工作,而对form:checkbox的迭代不起作用

来自分类Dev

具有cors支持和选项要求的Spring Boot执行器

来自分类Dev

无法运行spring-boot-test

来自分类Dev

Spring Boot无法自动接线并运行

来自分类Dev

无法运行 Spring Boot 简单 REST 服务

来自分类Dev

AlreadyBuiltException运行具有Spring Security的Spring Boot应用程序

来自分类Dev

无法通过Spring Boot和Thymeleaf进行验证

来自分类Dev

IntelliJ中的Spring Boot + thymeleaf:无法解析vars

来自分类Dev

Spring Boot,Thymeleaf和@Controller

来自分类Dev

Spring Boot-Thymeleaf模板

来自分类Dev

Spring Boot Thymeleaf相对网址

来自分类Dev

Spring Boot-Thymeleaf与JQuery?

来自分类Dev

Spring Boot,使用Thymeleaf发行

来自分类Dev

Spring Boot 和 Thymeleaf 的问题

来自分类Dev

Spring Boot Thymeleaf Ajax 调用

来自分类Dev

具有事务配置的Spring Boot

Related 相关文章

  1. 1

    类'ModelAndViewDefiningException'的Spring Webmvc NoClassDefFoundError

  2. 2

    如何生成Spring WebMVC CRUD API

  3. 3

    分隔Spring-WS和Spring-Webmvc

  4. 4

    Spring WebMVC 5 - 基于注解的拦截器

  5. 5

    将spring-data-rest-repository添加到spring-data-rest-webmvc

  6. 6

    ComponentScan.basePackageClasses与ComponentScan.basePackages来注册单个Spring webMVC控制器?

  7. 7

    获取spring-data-rest-webmvc 2.3.0.BUILD_SNAPSHOT的Maven构建错误

  8. 8

    与Spring-Data-Rest-WebMVC冲突的JSP页面的视图分辨率?

  9. 9

    在Spring WebMVC应用程序中获取WebContent的相对路径

  10. 10

    我正在尝试结合 Spring WebMVC 和 Apache CXF,但找不到该服务。为什么?

  11. 11

    使用Shiro和Spring WebMVC(Java8,Spring 4.x)从WebApp中注销所有仍登录的用户

  12. 12

    无法使用 Spring Boot thymeleaf 加载图像

  13. 13

    Spring-Data-Rest:当升级到spring-data-rest-webmvc 2.0.0时,Spring Data Book示例失败

  14. 14

    Spring WebMVC的标记库:form:checkboxes可以按预期工作,而对form:checkbox的迭代不起作用

  15. 15

    具有cors支持和选项要求的Spring Boot执行器

  16. 16

    无法运行spring-boot-test

  17. 17

    Spring Boot无法自动接线并运行

  18. 18

    无法运行 Spring Boot 简单 REST 服务

  19. 19

    AlreadyBuiltException运行具有Spring Security的Spring Boot应用程序

  20. 20

    无法通过Spring Boot和Thymeleaf进行验证

  21. 21

    IntelliJ中的Spring Boot + thymeleaf:无法解析vars

  22. 22

    Spring Boot,Thymeleaf和@Controller

  23. 23

    Spring Boot-Thymeleaf模板

  24. 24

    Spring Boot Thymeleaf相对网址

  25. 25

    Spring Boot-Thymeleaf与JQuery?

  26. 26

    Spring Boot,使用Thymeleaf发行

  27. 27

    Spring Boot 和 Thymeleaf 的问题

  28. 28

    Spring Boot Thymeleaf Ajax 调用

  29. 29

    具有事务配置的Spring Boot

热门标签

归档