【问题标题】:Submit button doesn`t work in spring boot提交按钮在 Spring Boot 中不起作用
【发布时间】:2022-06-16 04:17:12
【问题描述】:

我在 Spring Boot 上有一个 MVC 项目,它假设执行 CRUD 操作。基本上它有两个模型,分别代表论坛主题和本主题的文章。我想添加新的主题或文章。现在我坚持提交表单数据,通过 POST 请求进行管理。 我的控制器:

@Controller

公共类论坛控制器{

@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/bootstrap@5.2.0-beta1/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/bootstrap@5.2.0-beta1/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;
    }
}


【问题讨论】:

  • 你能解释一下到底是什么不工作吗?
  • 表单提交根本没有发生。而且我没有收到任何错误。
  • 您使用的是th:taglibform:form,这是我在Thymeleaf 的上下文中从未见过的。你确定没问题吗?你从哪里得到这些信息的?您可以尝试使用简单的&lt;form&gt; 标签吗?有关使用 Thymeleaf 提交表单的一些背景信息,请参阅 Form handling with Thymeleaf
  • 问题已解决,谢谢。 th:taglib 和 form:form 引起了麻烦,现在它可以正常工作了。

标签: java spring-boot spring-mvc spring-thymeleaf


【解决方案1】:

去掉th:taglibform:form的使用,使用普通的HTML&lt;form&gt;

【讨论】:

    猜你喜欢
    • 2013-04-09
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多