Spring Boot无法正确呈现Thymeleaf视图

时间:2016-01-15 00:01:19

标签: spring spring-mvc spring-security spring-boot thymeleaf

我正在关注此Spring Security guide,我已经到达了名为创建不安全的Web应用程序的部分。在该部分的末尾,陈述了:

  

此时,您可以跳转到使应用程序可执行   并且无需登录任何内容即可运行应用程序。

     

创建基本简单Web应用程序后,您可以添加安全性   它

我尝试按照使应用程序可执行中描述的步骤,以便能够创建应用程序的不安全版本。但是,视图处理不当。
例如,如果我导航到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说我应该添加jasper和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);
    }

}

2 个答案:

答案 0 :(得分:2)

您不需要tomcat中的javax.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 - 当你得到像这样的whitelabel错误时,去eclipse(或STS,无论你使用什么)并查看控制台上的输出。它通常会转储一个完整的堆栈跟踪,其中包含更明确的错误消息,您可以查明问题所在。

答案 1 :(得分:1)

您应该添加@Configuration注释,以便在加载配置时考虑您的MvcConfig类

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