Spring Boot MVC,不返回我的视图 [英] Spring Boot MVC, not returning my view

查看:126
本文介绍了Spring Boot MVC,不返回我的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Spring Boot MVC和视图有疑问. 当我进入localhost:9090时,页面写有索引",而不是像视图一样的index.html页面. 你能告诉我问题是什么吗? 预先感谢.

I have problem with Spring Boot MVC and views. When I go on localhost:9090, I've got page with "index" written and not my index.html page like view. Can you tell my what the problem is ? Thanks in advance.

pom.xml

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

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-rest-hal-browser</artifactId>
    </dependency>
</dependencies>

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

MainController

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class MainController {

    @RequestMapping(method = RequestMethod.GET)
    public String index() {
        return "index";
    }
}

Application.yml src/main/resources/application.yml

Application.yml src/main/resources/application.yml

spring:
    data:
        rest:
            basePath: /api
    mvc:
        view:
            prefix: /webapp/
            suffix: .html

server:
    port: 9000

我的index.html 位于:src/main/webapp/index.html

My index.html is in : src/main/webapp/index.html

也许没用,但是: Application.java

@SpringBootApplication
public class Application {

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

更新: 我将index.html放在src/static/index.html中 好的,但是我也有同样的问题:

UPDATE: I put index.html in src/static/index.html Ok but I have same problem with :

我添加了pom :

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

在map.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
 <p>this is the map</p>
</body>

MapController

@Controller
@RequestMapping("/map")
public class MapController {

    @RequestMapping(method = RequestMethod.GET)
    public String index() {
        return "map";
    }
}

Map.html在wepapp中. 当我转到localhost:9090/map时,我在chrome上遇到以下错误:

Map.html is in wepapp. When I go to localhost:9090/map, I've the following error on chrome :

This application has no explicit mapping for /error, so you are seeing this as a fallback.

在月食中,我遇到了这个错误:

And in eclipse I've got this error :

Error resolving template "map", template might not exist or might not be accessible by any of the configured Template Resolvers

地图也许应该在webapps/模板中?我真的不知道.

Map maybe should be in webapps/templates? I really dont know.

解决方案:谢谢大家! 控制器

@Controller
@RequestMapping("/map")
public class MapController {

    @RequestMapping(method = RequestMethod.GET)
    public String index() {
        return "map";
    }
}

并且视图必须位于src/main/resources/templates/map.html中,而不能位于wepapp/templates/map.html中 map.html

And the views must be in src/main/resources/templates/map.html and NOT in wepapp/templates/map.html map.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Bienvenue sur le portail historique intéractif - Amysto</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

    <div id="main">
        <div class="row">
            <p>Il faut mettre la map ici</p>
        </div>
    </div>
</body>

<script src="./foundation/js/vendor/jquery.js"></script>
<script src="./js/menu.js"></script>


</html>

我不能投票,所以我让你们做:)

I can't upvote so I let you do guys :)

推荐答案

写有"index"而不是index.html的页面是正常的,这是因为@RestController使您以Json格式返回了内容. 改为执行此操作:

The page with "index" written and not your index.html is normal since you are returning your content in Json format thanks to @RestController. Do this instead :

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

//@RestController
@Controller
@RequestMapping("/")
public class MainController {

    @RequestMapping(method = RequestMethod.GET)
    public String index() {
        return "index";
    }
}

确保在您的Maven中导入了Thymeleaf依赖项.

Make sure you imported the Thymeleaf dependency in your maven.

这篇关于Spring Boot MVC,不返回我的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆