我正在构建一个博客应用程序,在这种情况下,我想创建一个新的帖子。我创建了一个带有@GetMapping和@posts(值为“/post/new”)的控制器。我在thymeleaf中使用了th:action = "@{'/posts/new'}" (LINE 14),但是当我启动这个web应用程序,并访问本地主机:8080/post/new时,它会产生一个404错误,而url是找不到的,但它就在那里……我反复检查了拼写,code...pretty很多东西,但似乎不知道我犯了什么错误。以下是代码:
post_new.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Blog :: New Post</title>
</head>
<body>
<div class="container">
<a th:href="@{/}">Home</a>
<form action="#"
th:action="@{'/posts/new'}"
th:object="${post}">
<input type="hidden" th:field="*{account}" />
<input type="hidden" th:field="*{createdAt}" />
<h2>Write new post</h2>
<div>
<label for="new-post-title">Title</label>
<input id="new-post-title" type="text" th:field="*{title}" placeholder="Title"/>
</div>
<div>
<label for="new-post-body">Body</label>
<textarea id="new-post-body" th:field="*{body}"></textarea>
</div>
<button type="submit">Publish post</button>
</form>
</div>
</body>
</html>PostController.java
package com.hellion.writeup.controller;
import com.hellion.writeup.models.Account;
import com.hellion.writeup.models.Post;
import com.hellion.writeup.service.AccountService;
import com.hellion.writeup.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.Optional;
@Controller
public class PostController {
@Autowired
private PostService postService;
@Autowired
private AccountService accountService;
@GetMapping("/posts/{id}")
public String getPost(@PathVariable Long id, Model model) {
Post post = postService.getPost(id, model);
if (post != null) {
model.addAttribute("post", post);
return "post";
} else {
return "404";
}
}
@GetMapping("/posts/new")
public String createNewPost(Model model){
return postService.createNewPost(model);
}
@PostMapping("/posts/new")
public String saveNewPost(@ModelAttribute Post post){
postService.save(post);
return "redirect:/posts/" + post.getId();
}
}PostService.java
package com.hellion.writeup.service;
import com.hellion.writeup.models.Account;
import com.hellion.writeup.models.Post;
import com.hellion.writeup.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
@Service
public class PostService {
@Autowired
private PostRepository postRepository;
@Autowired
private AccountService accountService;;
public Optional<Post> getById(Long id) {
return postRepository.findById(id);
}
public List<Post> getAll() {
return postRepository.findAll();
}
public Post save(Post post) {
if (post.getId() == null) {
post.setCreatedAt(LocalDateTime.now());
}
return postRepository.save(post);
}
public Post getPost(Long id, Model model) {
Optional<Post> optionalPost = getById(id);
if (optionalPost.isPresent()) {
Post post = optionalPost.get();
return post;
}
return null;
}
public String createNewPost(Model model){
Optional<Account> optionalAccount = accountService.findByEmail("user.user@domain.com");
if(optionalAccount.isPresent()){
Post post = new Post();
post.setAccount(optionalAccount.get());
model.addAttribute("post", post);
return "post_new";
}else{
return "404";
}
}
}post.html
<!DOCTYPE HTML>
<html land= "en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE-edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link th:href="@{/styles/style.css}" rel="stylesheet" />
<title>Writeup :: Post</title>
</head>
<body>
<div class ="container">
<a th:href="@{'/'}">Home</a>
<div class="post">
<h2 th:text="${post.title}">Title</h2>
<h5 th:text="'Written by: ' + ${post.account.getFirstName()}">Account First Name</h5>
<h5 th:text="'Created on: ' + ${post.createdAt}">Created At</h5>
<p th:text="${post.body}">Body text</p>
</div>
</div>
</body>
</html>发布于 2022-11-09 20:37:28
GET方法用于/posts/new调用服务层中的方法createNewPost。如果optionalAccount不存在,则返回页404。一个可能的问题是optionalAccount是空的,因此返回404。
if(optionalAccount.isPresent()) {
Post post = new Post();
post.setAccount(optionalAccount.get());
model.addAttribute("post", post);
return "post_new";
} else {
return "404";
}th:action应该是th:action="@{/posts/new}"。
发布于 2022-11-12 23:44:16
我找到了solution...So --基本上,我得到了堆栈溢出错误,解决方案是,因为我在我的“模型”包中使用了Lombok和@Data注释,@Data导致了一些bug,通过将其更改为@Getter和@Setter,问题得到了解决。
https://stackoverflow.com/questions/74379357
复制相似问题