2017-11-28 4 views
0

spring mvcを学習しようとしています。 @ModelAttributeがどのように動作するかを理解することができません。私は学生オブジェクトを作成し、学生形式のページに移動し、属性名「テスト名」でモデルに追加すると、以下の私のコントローラ@ModelAttribute( "")annotationはまだspring mvcの間違ったパラメータで動作しています

@RequestMapping("/showForm") 
public String showForm(Model theModel){ 

    Student student = new Student(); 
    theModel.addAttribute("testName",student); 
    return "student-form"; 
} 

@RequestMapping("/processForm") 
public String showForm(@ModelAttribute("hello") Student student){ 

    return "student-confirmation"; 
} 

です。それから私は "processForm"メソッドにフォームに送信します。ここでは、フォーム

<form:form action="processForm" modelAttribute="testName"> 
    First Name: <form:input path="firstName" /> 
    Second Name: <form:input path="lastName" /> 

    <input type="submit" value="submit" /> 
</form:form> 

の下に使用すると、私は属性名は、私がモデル化するためにオブジェクトを置く場所で、どこで、私はそれをretriveと一致していない理解し `tをするものです。しかし、その結果は...正しく表示され

theModel.addAttribute("testName",student); //Here it is testName 
@ModelAttribute("hello") Student student //Here it is hello 

学生モデルはあなたがモデル属性としてテスト名を設定しているので、後にフォームを送信自動的に入力され

​​

答えて

0

学生-confirmation.jsp:

theModel.addAttribute("testName",student); 

JSP:

<form:form action="processForm" modelAttribute="testName"> 
<form:input path="firstName" /> 

SpringはStudentフィールドfirstnameと一致し、パス:path = "firstName"のフォームテキスト入力です。提出後

@RequestMapping("/processForm") 
public String showForm(@ModelAttribute("hello") Student student){ 

@ModelAttribute( "こんにちは")は、必要とされていない学生は、自動的に が一致しています。

モデルの参照名を変更するだけです。そして、JSPで$ {hello .firstName}を使用してモデルから値を取得できます。あなたは@ModelAttribute Annotation As a Method Argument

を読むことができます@ModelAttributeについて

詳細情報は、それがお役に立てば幸いです。

関連する問題