java - 阅读 Spring Boot in Action 一书
问题描述
我遇到了白标错误页面的问题,我不知道如何解决这个问题。
我编写了本书中的所有代码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import newmind.entity.Book;
import newmind.entity.ReadingListRepository;
import java.util.List;
@Controller
@RequestMapping("/readingList")
public class ReadingListController {
private ReadingListRepository readingListRepository;
@Autowired
public ReadingListController(
ReadingListRepository readingListRepository) {
this.readingListRepository = readingListRepository;
}
@RequestMapping(value="/{reader}", method=RequestMethod.GET)
public String readersBooks(@PathVariable ("reader") String reader, Model model) {
List<Book> readingList = readingListRepository.findByReader(reader);
if (readingList != null) {
model.addAttribute("books", readingList);
}
return "readingList";
}
@RequestMapping(value="/{reader}", method=RequestMethod.POST)
public String addToReadingList(@PathVariable("reader") String reader, Book book) {
book.setReader(reader);
readingListRepository.save(book);
return "redirect:/{reader}";
}
}
和 spring boot bootstrap 应用程序:
package newmind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages= {"newmind"})
public class ReadingListApplication {
public static void main(String[] args) {
SpringApplication.run(ReadingListApplication.class, args);
}
}
这是域类:
package newmind.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String reader;
private String isbn;
private String author;
private String description;
public Long getId() {
return id;
}
public String getReader() {
return reader;
}
public String getIsbn() {
return isbn;
}
public String getAuthor() {
return author;
}
public String getDescription() {
return description;
}
public void setId(Long id) {
this.id = id;
}
public void setReader(String reader) {
this.reader = reader;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public void setAuthor(String author) {
this.author = author;
}
public void setDescription(String description) {
this.description = description;
}
}
和存储库:
package newmind.entity;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ReadingListRepository extends JpaRepository<Book, Long> {
List<Book> findByReader(String reader);
}
我在 src/main/resources 目录中创建错误 html 页面和主页 html 页面:
这是error.html
<!DOCTYPE html>
<html>
<body>
Something went wrong: {{status}} {{error}}
</body>
</html>
和主页html页面:
<!DOCTYPE html>
<html>
<head>
<title>Reading List</title>
<style>
body {
background-color: #cccccc;
font-family: arial,helvetica,sans-serif;
}
</style>
</head>
<body>
<h2>Your Reading List</h2>
<div th:unless="${#lists.isEmpty(books) }">
</div>
</body>
</html>
现在,当我运行程序以侦听端口 8080 时,它向我显示:出了点问题:{{status}} {{error}}
你能帮我解决这个问题吗,因为我在家学习(书)。感谢一切。并且请不要报告此帖子,因为没有人可以解决此问题。
解决方案
推荐阅读
- reactjs - 使用 css 变换旋转时刷新 CardMedia 纵横比
- asp.net-core - Razor 页面无法正确解析 <
- angularjs - 寻找 AngularJS 服务提供者
- c# - Delphi 到 C# 问题 - 我不明白如何转换这个小函数
- javascript - 在图表顶部绘制图像不适用于 ChartJS
- excel - Excel VB - Shell 对象和 Items 扩展属性
- mongodb - Mongodb [js] 未捕获的异常:SyntaxError:标识符在数字文字之后立即开始
- javascript - Discord.js - 如何将用户 ID 转换为会员
- java - Thymeleaf 既不是 BindingResult 也不是 bean 名称的普通目标对象
- python - 尝试将前/后插入我的堆栈