【发布时间】: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 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>
我不能投票,所以我让你们投票:)
【问题讨论】:
-
/webapp/ 不会从/src/main/webapp加载模板,而是从 /src/main/resources/webapp`... -
您应该接受答案,而不是粘贴有问题的解决方案。不要发布有问题的答案。
标签: spring maven view spring-boot