0

我正在使用 springframework 制作一个简单的博客。我正在尝试为我的帖子/文章创建类别。当我创建文章时,我可以选择一个类别并且它工作正常。但是,当我尝试编辑文章时,选择类别的下拉菜单不起作用。它只是一个带有“向下箭头”边栏的空白字段,当我点击它时没有任何反应

//ARTICLE EDIT--------------------------------------------------
@GetMapping("/article/edit/{id}")
@PreAuthorize("isAuthenticated()")
public String edit(@PathVariable Integer id, Model model)
{
    if(!this.articleRepository.exists(id))
    {
        return "redirect:/";
    }
    Article article = this.articleRepository.findOne(id);

    if(!isUserAuthorOrAdmin(article))
    {
        return "redirect:/article/"+id;
    }

    List<Category> categories = this.categoryRepository.findAll();
    String tagString = article.getTags().stream().map(Tag::getName).collect(Collectors.joining(", "));

    model.addAttribute("view", "article/edit");
    model.addAttribute("article", article);
    model.addAttribute("category", categories);
    model.addAttribute("tags", tagString);

    return "base-layout";
}

@PostMapping("/article/edit/{id}")
@PreAuthorize("isAuthenticated()")
public String editProcess(@PathVariable Integer id, ArticleBindingModel articleBindingModel)
{
    if(!this.articleRepository.exists(id))
    {
        return "redirect:/";
    }
    Article article = this.articleRepository.findOne(id);
    Category category = this.categoryRepository.findOne(articleBindingModel.getCategoryId());
    HashSet<Tag> tags = this.findTagsFromString(articleBindingModel.getTagString());

    if(!isUserAuthorOrAdmin(article))
    {
        return "redirect:/article/"+id;
    }

    article.setContent(articleBindingModel.getContent());
    article.setTitle(articleBindingModel.getTitle());
    article.setCategory(category);
    article.setTags(tags);

    this.articleRepository.saveAndFlush(article);

    return "redirect:/article/" + article.getId();
}

html文件是:

<main>
    <div class="container body-content span=8 offset=2">
        <div class="well">
            <form class="form-horizontal" th:action="@{/article/edit/{id}(id=${article.id})}" method="POST">
                <fieldset>
                    <legend>Edit Post</legend>

                    <div class="form-group">
                        <label class="col-sm-4 control-label" for="article_title">Post Title</label>
                        <div class="col-sm-4 ">
                            <input type="text" class="form-control" id="article_title" placeholder="Post Title" name="title" th:value="${article.title}"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label" for="article_content">Content</label>
                    <div class="col-sm-6">
                        <textarea class="form-control" rows="6" id="article_content" name="content" th:field="${article.content}"></textarea>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-4 control-label" for="article_tags">Tags</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" id="article_tags" placeholder="Tags" name="tagString" th:value="${tags}"/>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-4 control-label" for="article_category">Category</label>
                    <div class="col-sm-6">
                        <select class="form-control" id="article_category" name="categoryId">
                            <option th:each="category : ${categories}" th:value="${category.id}" th:text="${category.name}"  th:selected="${category.id == article.category.id}">
                            </option>
                        </select>
                    </div>
                 </div>

                <div class="form-group">
                    <div class="col-sm-4 col-sm-offset-4">
                        <a class="btn btn-default" th:href="@{/article/{id}(id = ${article.id})}">Cancel</a>
                        <input type="submit" class="btn btn-success" value="Edit"/>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>
</div>

4

1 回答 1

0

您应该在${}. model.addAttribute("category", categories);和之间存在不匹配th:each="category : ${categories}"

将模型属性名称更改为“类别”,应该没问题

model.addAttribute("categories", categories);
于 2017-11-16T12:51:02.233 回答