2017-04-11 6 views
0

これに投稿する前に、フォームの入力を検証し、私は取得していますエラーです。 java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'e' available as request attribute at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE] .... ....Thymeleafエラーアピ

GET方法(フォームを表示):

@RequestMapping(value="/create", method=RequestMethod.GET) 
public String create(Model model){ 

    model.addAttribute("entry", new EntryForm()); 

    return "forms/frmEntry"; 
} 

私の元のPOSTメソッドが機能しなかったので、postToEntity呼び出しに関連する他のコードをApiに呼び出すことを除外するために、もう1つ、シンナーPOSTメソッドを作成しようとしました。

@PostMapping("/temp") 
public String temp(@Valid @ModelAttribute EntryForm e, BindingResult bindingResult){ 

// returns 1 in debug mode (error for the only field (name) in the form, so OK 
//  int ct = bindingResult.getErrorCount(); 

    if(bindingResult.hasErrors()){ 

     // this Thymeleaf view/template is reached 
     return "forms/frmEntry"; 
    } 

    // dummy Thymeleaf view (doesn't exist) 
    return "proslodokraja"; 
} 

マイ形式:

<form action="/consumer/temp" th:object="${e}" method="post" class="col-md-6"> 
    // in my code, this hidden field is commented out (for now) 
    <input type="hidden" th:field="*{id}" /> 
    <p> 
     Name: <input type="text" th:field="*{name}" class="form-control" /> 
     <br/> 
     <span th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name error</span> 
     </p> 

     <p> 
      <input type="submit" value="Submit" class="btn btn-primary" /> 
      <input type="reset" value="Reset" class="btn btn-default" /> 
     </p> 
</form> 

フォームBean

public class EntryForm { 

private Long id; 

@NotNull 
@Length(min=2) // hibernate import! 
private String name; 


public EntryForm(){ } 

public EntryForm(String name) { this.name = name; } 

public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 
} 

答えて

1

問題は、モデルの属性名です。あなたは "エントリ"としてそれを設定し、あなたのfrmEntryフォームの "e"モデル属性(設定されていない)にアクセスしようとしています。それを設定するときに、あなたのモデル属性は、いずれかの「E」に名前を変更するかfrmEntryページの「エントリー」に名前を変更し、明示的@ModelAttributeに名前を提供します。

<form action="/consumer/temp" th:object="${entry}" method="post" class="col-md-6"> 


@ModelAttribute("entry") EntryForm e 
+0

ないかなり。私は質問を投稿する直前に 'entry 'を' e'に置き換えました。なぜなら私はSpringのコードスニペットに合わせて調整しようとしていたからです。しかし、ちょうど確かめるために、私はtemplateとpostメソッドの両方で 'entry'に戻ってしまい、期待どおりエラーは残っていました。 – developer10

+0

私はあなたが結局あったと思います。私は 'GET'メソッドで' entry'に戻って忘れました。最後に、フォームBeanを 'entryForm'として参照することにしました。そして、最後に動作するようになりました。私はあなたの答えを受け入れています。ご協力ありがとうございました! – developer10