我是春季靴子的新人,请指教
spring-boot webapp 试图在网络上查看结果,但它只是以 Json 的形式出现。
我不知道缺少什么以及如何跟踪这个问题,尤其是没有错误消息
请检查我的控制器和 pom 文件。 请注意,我能够以 json 格式获得结果并希望获得 webview
package com.example.springboot.controller;
import com.example.springboot.model.Session;
import com.example.springboot.repository.SessionRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import java.util.List;
@EnableWebMvc
@Controller
public class SessionController {
@Autowired
private SessionRepository sessionRepository;
@GetMapping(value = "/show-session", produces = "text/html")
public ModelAndView showSession(){
ModelAndView muv = new ModelAndView("show-session");
List<Session> list = sessionRepository.findAll();
muv.addObject("sessions", list);
return muv ;
}
@GetMapping("/session-form")
public ModelAndView sessionsForm() {
ModelAndView muv = new ModelAndView("session-form");
Session session = new Session();
muv.addObject("session", session);
return muv;
}
@PostMapping({ "/save-session"} )
public ModelAndView saveSession( @ModelAttribute final Session session) {
sessionRepository.saveAndFlush(session);
return showSession();
}
@GetMapping("/sessions/{id}")
public Session get(@PathVariable("id") Long id) {
return sessionRepository.getReferenceById(id);
}
@DeleteMapping(value ="/sessions/{id}")
public void delete(@PathVariable("id") Long id){
sessionRepository.deleteById(id);
}
@PutMapping(value ="/sessions/{id}")
public Session update(@PathVariable Long id, @RequestBody Session session){
Session existsession = sessionRepository.getReferenceById(id);
BeanUtils.copyProperties(session, existsession, "session_id");
return sessionRepository.saveAndFlush(existsession);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springBoot</name>
<description>springBoot</description>
<properties>
<java.version>17</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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- </dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>Conference Applications</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65"
crossorigin="anonymous">
</head>
<body>
<div class="container=5">
<h2>Session List </h2>
<h2>
</h2>
<hr/>
<a th:href="@{/session-form}" class="btn btn-primary mb-2">Add Session</a>
<table class="table table-header table-striped">
<thead>
<tr>
<th> Session Id </th>
<th> name </th>
<th> description</th>
<th> length</th>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
integrity="sha384-oBqDVmMz9ATKxIep9tiCxS/Z9fNfEXiDAYTujMAeBAsjFuCZSmKbSSUnQlmh/jp3"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V"
crossorigin="anonymous"></script>
</tr>
</thead>
<tbody>
<tr>
<tr th:each = "se: ${sessions}">
<td th:text="${se.session_id}"></td>
<td th:text="${se.session_name}"></td>
<td th:text="${se.session_description}"></td>
<td th:text="${se.session_length}"></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>