我在 Spring Boot 上有 MVC 專案,它假設執行 CRUD 操作。基本上它有兩個模型,分別代表論壇主題和本主題的文章。我想添加新的主題或文章。現在我堅持提交表單資料,通過 POST 請求進行管理。我的控制器:
@Controller
public class forumController {
@Autowired
private themeRepository themeRepository;
@Autowired
private articleRepository articleRepository;
@GetMapping("/forum")
public String blogMain(Model model) {
Iterable<ThemeModel> themes = themeRepository.findAll();
model.addAttribute("themes", themes);
return "blogMain";
}
@GetMapping("/guest")
public String guestMain(Model model) {
Iterable<ThemeModel> themes = themeRepository.findAll();
model.addAttribute("themes", themes);
return "blogMain";
}
@GetMapping("/guest/{id}")
public String readTheme(@PathVariable(value = "id") Long id, Model model) {
Optional<ThemeModel> theme = themeRepository.findById(id);
ArrayList<ThemeModel> result = new ArrayList<>();
theme.ifPresent(result::add);
model.addAttribute("theme", result);
return "readTheme";
}
@GetMapping("/user")
public String userMain(Model model) {
Iterable<ThemeModel> themes = themeRepository.findAll();
model.addAttribute("themes", themes);
return "userMain";
}
@GetMapping("/user/{id}")
public String userReadTheme(@PathVariable(value = "id") Long id, Model model) {
Optional<ThemeModel> theme = themeRepository.findById(id);
ArrayList<ThemeModel> result = new ArrayList<>();
theme.ifPresent(result::add);
model.addAttribute("theme", result);
return "userReadTheme";
}
@GetMapping("/user/addArticle/{id}")
public String userAddArticle(@PathVariable(value = "id") Long id, Model model) {
return "userAddArticle";
}
@PostMapping("/user/addArticle/{id}")
public String userAddArticlePost(@PathVariable(value = "id") Long id, @RequestParam String full_text, Model model) {
ArticleModel article = new ArticleModel(full_text);
articleRepository.save(article);
ThemeModel thisTheme = themeRepository.findById(id).orElseThrow();
thisTheme.getArticles().add(article);
themeRepository.save(thisTheme);
return "redirect:/user/{id}";
}
@GetMapping("/admin")
public String adminMain(Model model) {
Iterable<ThemeModel> themes = themeRepository.findAll();
model.addAttribute("themes", themes);
return "adminMain";
}
@GetMapping("/admin/addTheme")
public String addTheme(Model model) {
return "adminAddTheme";
}
@PostMapping("/admin/addTheme")
public String addPostTheme(@RequestParam("heading") String heading, @RequestParam("description") String description, Model model) {
ThemeModel theme = new ThemeModel(heading, description);
themeRepository.save(theme);
return "redirect:/admin";
}
}
添加主題的模板:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<title>Title</title>
</head>
<body>
<h1 class="text-muted p-2 mt-1">Додати нову тему</h1>
<div class="container mt-5">
<th:taglib uri="http://www.springframework.org/tags/form" prefix="form" />
<form:form action="#" th:action="@{/admin/addTheme}" th:object="${theme}" method="post">
<input type="text" name="heading" placeholder="Введ?ть заголовок..." class="form-control mb-2"/>
<input type="text" name="description" placeholder="Введ?ть опис..." class="form-control mb-2"/>
<input type="submit" value="Опубл?кувати" class="btn btn-info mt-2"/>
</form:form>
<a href="/admin" class="btn btn-secondary mt-2">Скасувати</a>
</div>
</body>
</html>
添加文章的模板:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<title>Title</title>
</head>
<body>
<h1 class="text-muted p-2 mt-1">Додати допис</h1>
<div class="container mt-5">
<th:taglib uri="http://www.springframework.org/tags/form" prefix="form" />
<form:form method="POST">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<textarea name="full_text" placeholder="Введ?ть текст" class="form-control"></textarea>
<button type="submit" class="btn btn-info mt-2">Опубл?кувати</button>
<a href="/user" class="btn btn-secondary mt-2">Скасувати</a>
</form:form>
</div>
</body>
</html>
主題模型:
@Entity
@Table(name = "themes")
public class ThemeModel {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
private String heading, description;
public ThemeModel() {
}
public ThemeModel(String heading, String description) {
this.heading = heading;
this.description = description;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "article_id", referencedColumnName = "id")
List<ArticleModel> articles = new ArrayList<>();
public Long getId() {
return id;
}
public String getHeading() {
return heading;
}
public String getDescription() {
return description;
}
public List<ArticleModel> getArticles() {
return articles;
}
public void setId(Long id) {
this.id = id;
}
public void setHeading(String heading) {
this.heading = heading;
}
public void setDescription(String description) {
this.description = description;
}
public void setArticles(List<ArticleModel> articles) {
this.articles = articles;
}
}
文章型號:
@Entity
@Table(name="articles")
public class ArticleModel {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
private String full_text;
public ArticleModel() {
}
public ArticleModel(String full_text) {
super();
this.full_text = full_text;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFull_text() {
return full_text;
}
public void setFull_text(String full_text) {
this.full_text = full_text;
}
}
uj5u.com熱心網友回復:
洗掉使用th:taglibandform:form并使用普通的 HTML <form>。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/492240.html
上一篇:Spring在驗證登錄時在loginProcessingUrl上給出404
下一篇:org.springframework.dao.IncorrectResultSizeDataAccessExceptionMongoLimit不起作用?
