无法在 Thymeleaf 支持下运行 Spring Boot WebMVC [英] Unable to run Spring Boot WebMVC with Thymeleaf support

查看:32
本文介绍了无法在 Thymeleaf 支持下运行 Spring Boot WebMVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多,但没有找到我的问题的答案,所以我在这里发布我的问题.请查看并建议我错误的解决方案.

I have search a lot but I did not find answer to my question, So I am posting my question here. Please look and suggest me the solution where I am mistaken.

我使用 Spring Tool Suite(STS) 创建了具有 thymeleaf 支持的 spring boot web mvc 项目.当我运行它时,给我白标错误页面"页面.这意味着未找到映射.

I have created spring boot web mvc project with thymeleaf support using Spring Tool Suite(STS). When I run it give me "Whitelabel Error Page" page. Which means mapping not found.

努力:

WebConfig.java

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

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

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

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

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 文件.我仍然收到那个错误.我在网上搜索了很多,但没有得到任何线索.请有人帮助我.

I have created index.html file in resources/templates folder. Still I am getting that error. I have searched a lot on web, but did not get clue. Please somebody help me.

推荐答案

实际上 Spring Boot 开箱即用地配置了 Thymeleaf.它应该适用于以下设置:

Actually Spring Boot configures Thymeleaf out of the box. It should work with the following setup:

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

这篇关于无法在 Thymeleaf 支持下运行 Spring Boot WebMVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆