2017-11-16 5 views
0

私はspringframeworkを使用して簡単なブログを作っています。私は私の記事/記事のカテゴリーを作ろうとしています。記事を作成するときにカテゴリを選択するオプションがあり、正常に動作します。ただし、記事を編集しようとすると、カテゴリを選択するためのドロップダウンメニューが機能しません。それは下向きの矢印 "sidebadでただし、空のフィールドだと私は上でクリックしたときに何も起こりません、それカテゴリを編集するためのドロップダウンメニューが機能しません

//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> 
あなたは、モデル属性の名前を使用する必要があります

+0

あなたのマップに単数形の名前(「カテゴリ」)を渡している、まだあなたのイテレータでは、あなたが「カテゴリ」を使用して問題ないはずです。 –

+1

長い一日でした。ありがとうございます – Sibuscus

答えて

0

内側${}model.addAttribute("category", categories);th:each="category : ${categories}"の間に不一致があります。

変更モデルは、「カテゴリ」に属性名とそれが

model.addAttribute("categories", categories); 
+0

コースのオハイオ州私の神、どのように私は、笑です。私は別のJavaバージョン、springbootバージョンをインストールしようとしましたが、これを見てください...ありがとう – Sibuscus

+0

それは誰にも起こるかもしれません。どういたしまして。 –

関連する問題