【问题标题】:Spring Boot mapping action to view(.html) not workingSpring Boot 映射操作到视图(.html)不起作用
【发布时间】:2017-08-08 17:39:49
【问题描述】:

我有一个 Spring Boot 基础应用程序

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/main/resources/public 文件夹中的基本 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>

当我执行 localhost:8080 时,我希望重定向到 index.html,但我收到 Whitelabel 错误页面

当我添加 thymeleaf 依赖项并在 .html 中添加 xmlns:th="http://www.thymeleaf.org" 时,它可以工作

如何解决?还有什么是 Spring Boot 中的默认视图解析器?我是否必须使用百里香叶?

另外,我是否需要在 application.properties 中添加一些内容

【问题讨论】:

    标签: spring maven spring-boot


    【解决方案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);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-14
      • 1970-01-01
      • 2015-11-01
      • 2021-06-24
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      相关资源
      最近更新 更多