【问题标题】:Spring Boot MVC, not returning my viewSpring Boot MVC,不返回我的视图
【发布时间】:2016-11-23 12:48:17
【问题描述】:

我对 Spring Boot MVC 和视图有疑问。 当我进入 localhost:9090 时,我的页面写有“索引”,而不是我的 index.html 页面,如视图。 你能告诉我问题是什么吗? 提前致谢。

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>

主控制器

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

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

server:
    port: 9000

我的 index.html 位于: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 好的,但我有同样的问题:

我在我的 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>

地图控制器

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

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

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

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

在 Eclipse 中我遇到了这个错误:

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

地图可能应该在 webapps/templates 中?我真的不知道。

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

@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 ma​​p.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>

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

【问题讨论】:

  • /webapp/ 不会从 /src/main/webapp 加载模板,而是从 /src/main/resources/webapp`...
  • 您应该接受答案,而不是粘贴有问题的解决方案。不要发布有问题的答案。

标签: spring maven view spring-boot


【解决方案1】:

有几件事要看:

确保您使用的是@Controller 而不是@RestController

如果您使用@RestController,您的方法的返回值将被序列化为 JSON 并返回,而不是解析为视图。

包含一个模板库

如果您要返回视图,您可能需要一个模板库,例如 Thymeleaf、Velocity 等。如果你想使用 Thymeleaf,你可以使用:

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

确保您的模板位于正确的位置

添加模板的默认位置是src/main/resources/templates,请确保您的模板位于此处。 src/main/resources/publicsrc/main/resources/static 位置用于提供静态内容,但在这种情况下,您不必提供一个控制器,你可以去:http://localhost:8080/map.html

【讨论】:

  • 谢谢!我将我的 html 放在 wepapp/templates 中,它是 resources/templates !非常感谢。
  • 谢谢。我不知道“@ResponseBody”会被序列化为 JSON。
  • 谢谢。我使用的是@RestController 而不是@Controller。
【解决方案2】:

写有“index”而不是 index.html 的页面是正常的,因为感谢@RestController,您以 Json 格式返回内容。 改为这样做:

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 依赖项。

【讨论】:

  • index.html in wepapp with what's what's write,给我一个错误:解析模板“索引”时出错,模板可能不存在或可能无法被任何配置的模板解析器访问
  • 我期待这个错误。您是否导入了 thymeleaf 依赖项?渲染 html 内容仅适用于 thymeaf。你的 webapp 是一个 RESP 应用程序。
  • 我把它放在我的 pom.xml 中。 org.springframework.bootspring-boot-starter-thymeleaf 这在我的 index.html thymeleaf.org ">
【解决方案3】:

静态资源,如 HTML、JavaScript 或 CSS,可以很容易地从 Spring Boot 应用程序中提供,只需将它们放到源代码中的正确位置即可。默认情况下,Spring Boot 从“/static”(或“/public”)的类路径中的资源中提供静态内容。 index.html 资源很特别,因为如果它存在,它被用作“欢迎页面”,这意味着它将作为根资源提供。添加 maven 依赖 Thymeleaf

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

所以创建这个文件:

src/main/resources/static/index.html

在正确的位置,您可以将其他静态文件放入您的 webapp 文件夹中

【讨论】:

  • 静态文件夹下 index.html 没问题。但我对另一个观点有同样的问题。我会更新我的帖子
  • 感谢 Tymeleaf。但我现在又犯了一个错误。我更新了我的帖子
【解决方案4】:

这是使用 Spring boot mvc 返回视图的完整答案。 谢谢大家。

控制器

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

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

src/main/resources/templates/map.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

    <div id="main">
        <div class="row">
            <p>my map</p>
        </div>
    </div>
</body>
</html>

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>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>



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

【讨论】:

    猜你喜欢
    • 2010-11-15
    • 2016-01-08
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多