2016-01-15 47 views
0

我按照此Spring Security guide,我已經到達名爲的部分創建一個不安全的Web應用程序。在該節結束時,這是說:Spring Boot無法正確呈現Thymeleaf視圖

At this point, you could jump ahead to Make the application executable and run the application without having to login to anything.

With the base simple web application created, you can add security to it.

我試圖按照使應用程序的可執行,才能夠創建應用程序的不安全的版本描述的步驟。但是,視圖處理不當。
例如,如果我定位到http://localhost:8080/home我得到這個錯誤:

Whitelabel Error Page 

This application has no explicit mapping for /error, so you are seeing this as a fallback. 

Thu Jan 14 20:49:56 ART 2016 
There was an unexpected error (type=Not Found, status=404). 
No message available 

I found this question說我要補充Thymeleaf的依賴,and this other one說我要補充碧玉和jslt,但他們都沒有工作。然後,我發現this issue,說我應該將我的資源從src/main/resources/templates複製到src/main/resources/static
這樣做會稍微改變一下:導航到http://localhost:8080/home.html會呈現html,但視圖不會被處理,因此不會生成鏈接。

這是我pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.github.juanmougan.samples</groupId> 
    <artifactId>spring-security</artifactId> 
    <!-- <packaging>jar</packaging> --> 
    <version>1.0-SNAPSHOT</version> 
    <name>spring-security</name> 
    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.1.RELEASE</version> 
    </parent> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-thymeleaf</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.tomcat.embed</groupId> 
      <artifactId>tomcat-embed-jasper</artifactId> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
     </dependency> 
    </dependencies> 

    <properties> 
     <java.version>1.8</java.version> 
    </properties> 


    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

    <repositories> 
     <repository> 
      <id>spring-releases</id> 
      <name>Spring Releases</name> 
      <url>https://repo.spring.io/libs-release</url> 
     </repository> 
    </repositories> 
    <pluginRepositories> 
     <pluginRepository> 
      <id>spring-releases</id> 
      <name>Spring Releases</name> 
      <url>https://repo.spring.io/libs-release</url> 
     </pluginRepository> 
    </pluginRepositories> 
</project> 

我的MVC配置類:

package com.github.juanmougan.samples; 

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

/** 
* Configures Spring MVC and sets up view controllers to expose the templates. 
* 
* @author juanma 
* 
*/ 
public class MvcConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addViewController("/home").setViewName("home"); 
     registry.addViewController("/").setViewName("home"); 
     registry.addViewController("/hello").setViewName("hello"); 
     registry.addViewController("/login").setViewName("login"); 
    } 

} 

而且模板:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" 
     xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> 
    <head> 
     <title>Hello World!</title> 
    </head> 
    <body> 
     <h1>Hello world!</h1> 
    </body> 
</html> 

爲什麼模板沒有被正確處理任何想法?
在此先感謝

編輯:添加缺少的Application類。

package com.github.juanmougan.samples; 

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

@SpringBootApplication 
public class Application { 

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

} 
+0

澄清問題是,您的家庭或「/」頁面沒有被正確渲染,而不是你得到「白色標籤錯誤頁面」的事實? – Aeseir

+0

家庭(http:// localhost:8080 /)和其他頁面都不像http:// localhost:8080/hello – jmm

+0

您是如何設置應用程序上下文配置的? – Aeseir

回答

1

您應該添加註釋@Configuration,所以你MvcConfig類將被考慮在內,同時加載配置

@Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter{ 
... 
} 
+0

雖然這是真的,但OP應該使用'spring-boot-starter-web',如果他們想要MVC的東西。讓春季啓動做它的自動配置魔術,並且只在必要時手動配置。 –

+2

這個問題並非如此。我也推薦使用'spring-boot-started-web',但問題OP的根源是他的映射不被Spring加載。你的回答也很好,但這不是OP在他的文章 – wcislo

+0

中建議的方法謝謝。除了這個註釋缺失問題(一個錯誤的複製粘貼),我犯了一個非常愚蠢的錯誤:不知何故,我把'MvcConfig'放在test文件夾中,而不是主要的 – jmm

2

你並不需要在tomcatjavax.servletpom.xml,而不是帶他們出去和將它們替換爲

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-web</artifactId> 
</dependency> 

這將引入嵌入式tomcat實例並自動配置MVC你的東西。

然後添加一個@Controller註解類,併成立了@RequestMapping

@Controller 
public class IndexController { 

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

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

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

} 

此外,作爲#protip - 當你得到一個白色標籤的錯誤那樣,去到eclipse(或STS,你使用什麼)並查看控制檯上的輸出。它通常會轉儲一個完整的堆棧跟蹤,其中有更明確的錯誤消息,您可以確定問題出在哪裏。

+0

而且,這只是一個意見,但freemarker比thymeleaf。我嘗試了百里香,因爲它也是一些教程的一部分,它有一個令人討厭的語法來包含其他模板,並且浪費時間編寫虛擬html,以便在開發期間將其呈現在瀏覽器中,並查看它的外觀。實際上,Spring引導應用程序的部署速度很快(尤其是使用[DevTools](https://spring.io/blog/2015/06/17/devtools-in-spring-boot-1-3)),這是毫無意義的功能!它也使你的模板代碼難以閱讀,因爲隨處可見隨機無關的東西。 –

+0

感謝您的回答和評論。我會檢查freemarker,我不認爲Spring Boot是如此之好,但我只是我想了解一下Spring Security,我決定按照這個指南一步一步來做最少量的配置問題 – jmm

+0

我剛剛讀了額外的提示,我決定發佈一個問題,因爲要麼從Eclipse運行應用程序,要麼從控制檯不會在日誌中顯示任何錯誤 – jmm