2016-05-13 63 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 /模板

相關問題