我按照此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);
}
}
澄清問題是,您的家庭或「/」頁面沒有被正確渲染,而不是你得到「白色標籤錯誤頁面」的事實? – Aeseir
家庭(http:// localhost:8080 /)和其他頁面都不像http:// localhost:8080/hello – jmm
您是如何設置應用程序上下文配置的? – Aeseir