Tomcat 8、Spring、Thymeleaf 2 Java 配置显示空白页

Tomcat8, Spring4, Thymeleaf2 Java configuration showing blank page

我非常接近这个工作,但我缺少一些简单的东西。

当我的 RestController returns 一个字符串 "index" 它应该转到 index.html 但我得到一个空白页面,上面写着 "index" 。一定是我的配置关闭了,但我不知道怎么办。

我把我的 index.html 放到 WEB-INF\templates 中,它只包含一段 "Hello World"。我考虑过使用 Spring Boot,但我想解决这个问题,所以我暂时跳过它。另外,我认为我已经非常接近这个工作了。

我的applicationContext.xml代表什么:

@Configuration
@ComponentScan({"com.spring.poc.web"})
@EnableWebMvc
public class SpringWebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }

    @Bean
      public ViewResolver viewResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setCacheable(false);
        templateResolver.setPrefix("/WEB-INF/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");

        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setCharacterEncoding("UTF-8");
        viewResolver.setOrder(1);
        viewResolver.setTemplateEngine(templateEngine);

        return viewResolver;
      }
}

我的 spring @Services 在哪里:

@Configuration
@ComponentScan({"com.nox.poc.service"})
public class SpringRootConfig {

}

什么代表我的web.xml

    public class WebXmlInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { SpringRootConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { SpringWebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/"};
    }

}

我的控制器:

@RestController
public class PocRestController {



    @RequestMapping(value = "/")
    public String getMainPage(ModelMap map) {

        return "index";
    }   

}

我的pom.xml(我正在使用 Eclipse Mars):

    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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.spring.poc</groupId>
  <artifactId>pocApp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

    <properties>
        <spring.version>4.2.2.RELEASE</spring.version>
        <jackson.version>2.6.3</jackson.version>
        <faces.version>2.2.12</faces.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- jackson allows spring RestController to bind whatever we send back as JSON -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.3</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

当我在 Eclipse 中启动 tomcat 8 时一切正常,外部日志文件中没有任何内容。但是,当我点击本地主机时:8080\pocApp\

我得到一个空白页,上面写着 "index"。很明显我缺少一些配置,但我还没有找到答案。它必须是小东西...

所以我遗漏了一些基本的东西。从更多谷歌搜索中找到答案。遇到这个:

how to use spring4 @RestController to return a jsp page?

将我的注释从@RestController 更改为@Controller 成功了。现在一切都说得通了!