bak*_*ouz 2 spring view maven spring-boot
我对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>
Run Code Online (Sandbox Code Playgroud)
主控制器
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";
}
}
Run Code Online (Sandbox Code Playgroud)
Application.yml src /主目录/资源/application.yml
spring:
data:
rest:
basePath: /api
mvc:
view:
prefix: /webapp/
suffix: .html
server:
port: 9000
Run Code Online (Sandbox Code Playgroud)
我的index.html位于:src / main / webapp / index.html
也许没用,但是: Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
更新: 我将index.html放在src / static / index.html中,但是我有以下同样的问题:
我在pom中添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
MapController
@Controller
@RequestMapping("/map")
public class MapController {
@RequestMapping(method = RequestMethod.GET)
public String index() {
return "map";
}
}
Run Code Online (Sandbox Code Playgroud)
Map.html在wepapp中。当我转到localhost:9090 / map时,我在chrome上遇到以下错误:
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Run Code Online (Sandbox Code Playgroud)
在日食中,我有此错误:
Error resolving template "map", template might not exist or might not be accessible by any of the configured Template Resolvers
Run Code Online (Sandbox Code Playgroud)
地图也许应该在webapps /模板中?我真的不知道。
解决方案:谢谢大家! 控制者
@Controller
@RequestMapping("/map")
public class MapController {
@RequestMapping(method = RequestMethod.GET)
public String index() {
return "map";
}
}
Run Code Online (Sandbox Code Playgroud)
并且视图必须位于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>
Run Code Online (Sandbox Code Playgroud)
我无法投票,所以我让你们做:)
有几件事情需要看:
@Controller是代替@RestController.如果您正在使用@RestController,您的方法的返回值将被序列化为 JSON 并返回,而不是被解析为视图。
如果您要返回一个视图,您可能需要一个模板库,例如 Thymeleaf、Velocity、...。如果你想使用 Thymeleaf,你可以使用:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
添加模板的默认位置是src/main/resources/templates,请确保您的模板位于此处。位置src/main/resources/public和src/main/resources/static用于提供静态内容,但在这种情况下,您不必提供控制器,只需转到:http://localhost :8080/map.html。
写有“ index”而不是index.html的页面是正常的,因为您要以Json格式返回内容@RestController。
改为这样做:
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";
}
}
Run Code Online (Sandbox Code Playgroud)
确保在您的Maven中导入了Thymeleaf依赖项。
| 归档时间: |
|
| 查看次数: |
8991 次 |
| 最近记录: |