2016-05-13 65 views
1

我搜索了很多,但没有找到我的问题的答案,所以我在这里发布我的问题。请看看我的错误解决办法。无法运行带Thymeleaf支持的Spring Boot WebMVC

我已经使用Spring Tool Suite(STS)创建了带有thymeleaf支持的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文件资源/模板文件夹。我仍然得到那个错误。我在网上搜索了很多,但没有得到任何线索。请有人帮助我。

Directory Structure

+0

删除您的Web配置类。 Spring Boot已经为你配置。对于I18N支持,在您的'application.properties'中添加'spring.messages.basename =/WEB-INF/i18/thymeleafResource'。简而言之,框架不在框架内。 –

+0

我已经删除了WebConfig.java文件,现在我也不想使用任何资源,所以我没有配置资源包。在此之后,我试图运行我仍然得到同样的问题。 – Mandy

+0

添加目录结构。此外,错误页面可能意味着完全不同,请检查日志中是否有错误(并将堆栈跟踪/错误添加到您的问题中)。 –

回答

1

实际上,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"; 
    } 
} 
+0

不是它不工作。它带我到没有上下文根路径的登录页面。 – Mandy

+0

@Mandy 我在LoginController Annotation中犯了一个错误。将“/”更改为“/ login”...而“没有上下文根路径”是什么意思?上面的设置应该保护任何请求,并且应该重定向到“/ login”下的登录页面。成功登录后,它应该重定向到“/”。 – shinchillahh

+0

这里我们需要更多的东西来让我做实际的项目。我也会感谢M. Deinum,没有他的指导,我不会完成我的任务。谢谢Deinum。 – Mandy

1

春季启动已经配置Thymeleaf你,所以无需手动配置。删除所有Thymeleaf相关配置,同时删除@EnableWebMvc,因为这会干扰Spring Boot自动配置。 @ComponentScan也是多余的。

Spring Boot还为您注册了MessageSource,因此不需要配置它。不知道你做了什么servlet注册,但这是你唯一需要的。

另外我建议删除你的控制器并使用你可以在WebConfig类中配置的视图控制器。为您节省一个控制器。

@Configuration 
public class WebConfig extends WebMvcConfigurerAdapter { 

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

    public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addViewController("/").setViewName("index"); 
    } 
} 

为了让自动配置的消息来源领取您的自定义捆绑添加以下src/main/resources/application.properties

spring.messages.basename=/WEB-INF/i18/thymeleafResource 

我也建议干脆让SpringThymeLeafApplication延长SpringBootServletInitializer

@SpringBootApplication 
public class SpringThymeLeafApplication extends SpringBootServletInitializer { 

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

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

另外,还要确保你的模板是src/main/resources/templates,而不是在其他src/main/resources/resources/templates那些不会被发现。

+0

我已经完成了所有你提到的事情。我已经删除了所有类似webconfig.java以及任何与thymeleaf有关的东西。只有安全配置和引导配置。我也创建了模板中的error.html页面。不是显示错误页面而是控制器页面。 – Mandy

+0

感谢Deinum没有你的帮助,我不会做更多。作为你的回答,shinchillahh的回答让我走得更远。在这里,我只能接受一个答案,但为你效劳。 – Mandy

0

当您添加thymeleaf依赖项时,Spring引导会执行所有自动配置。那么你应该做以下事情。

  1. 删除您的WebConfig上的所有thymeleaf配置。java的
  2. 请确保您有以下依赖你的pom.xml如果你正在使用Maven,否则检查弹簧网站相当于如果你正在使用的gradle产出:

    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 
    
  3. 第三你要确保你扫描您的控制器,增加您的SpringThymeLeafApplication.java如下:

    @ComponentScan(basePackages = “your.path.to.controllers”)

  4. 最后你有你的.html文件添加至r esources /模板

相关问题