2017-03-17 68 views
0

我有一个春天启动基本应用春季启动映射操作来查看档案(.html)不工作

package com.meenakshi.fileupload; 

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



@SpringBootApplication 
public class Example { 



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

} 

我控制器

package com.meenakshi.fileupload.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 



@Controller 
public class IndexController { 

    @RequestMapping("/") 
    public String index() { 
     System.out.print("REDIRECTED BY MEENAKSHI"); 

     return "index"; 
    } 

} 

我的index.html在src一个基本的HTML文件/主/资源/公用文件夹

我的pom.xml的是

<?xml version="1.0" encoding="UTF-8"?> 
<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.example</groupId> 
    <artifactId>myproject</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.5.RELEASE</version> 
    </parent> 



    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <!-- Additional lines to be added here... --> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 


    </dependencies> 


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

</project> 

当我做本地主机:8080我希望重定向到index.html,但我越来越白标签错误页面当我添加百里香依赖并添加xmlns:th =“http://www.thymeleaf.org”中.html它的工作原理

如何解决它?另外什么是Spring Boot中的默认视图解析器?我是否强制需要使用百里香?

而且,我需要添加一些application.properties中

回答

1

有几个方法,你可以暴露你的index.html。

我最喜欢的方式是做如下:

@Configuration 
public class WebMvcConfiguration extends WebMvcConfigurerAdapter { 

    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addViewController("/").setViewName("index.html"); 
     registry.setOrder(Ordered.HIGHEST_PRECEDENCE); 
    } 

} 

它会自动抓取您的index.html并创建一个默认视图控制器来为它服务。

另一种方式是做像你这样,但返回ModelAndView像下面,但它适应Thymeleaf/JSP更多然后一个SPA:

@Controller 
@RequestMapping("/") 
public class DefaultController { 

    @GetMapping("/") 
    public ModelAndView index() { 
     IndexModel indexModel = new IndexModel(); 
     return new ModelAndView("index", "index", indexModel); 
    } 

}