gpt4 book ai didi

Spring Boot MVC,没有返回我的观点

转载 作者:行者123 更新时间:2023-12-02 06:10:15 25 4
gpt4 key购买 nike

我对 Spring Boot MVC 和 View 有问题。当我访问 localhost:9090 时,我得到的页面写有“index”,而不是像我的 index.html 页面那样的 View 。你能告诉我问题是什么吗?提前致谢。

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>

主 Controller

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>

map Controller

@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

map 也许应该在 webapps/templates 中?我真的不知道。

解决方案:谢谢大家! Controller

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

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

View 必须位于 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>

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

最佳答案

有几件事需要注意:

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

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

包含模板库

如果您要返回 View ,您可能需要一个模板库,例如 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 用于提供静态内容,但在这种情况下,您不必提供 Controller ,您只需转到:http://localhost:8080/map.html .

关于Spring Boot MVC,没有返回我的观点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40765007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com