2017-10-03 7 views
0

バネデータJPAリポジトリメソッドを使用して、4つのフォームフィールドの名前、年齢、場所、およびアドレスをデータベースに挿入しようとしています。フォームをアクションに送信するとき、コントローラは送信された値を受け取ることができませんでした。コントローラーアクションでnullを取得します。ここで私は春の春のブートと春のデータJPAを使用してみてください。以下は、私のコントローラであるフォームを送信するときにフォームの値が取得されない

@RequestMapping(value="/driverSubmit", method=RequestMethod.POST) 
public ModelAndView driverSubmit(@ModelAttribute Driver driver, Model model) 
    { 
     model.addAttribute("driver", driver); 
     driverRepo.save(driver); 
     return new ModelAndView("redirect:/dHome"); //to home page insertoin 
    } 

そして、私のフォームは、

<form action="driverSubmit" method="post" > 
    <table width="300px" height="200px" align="center"> 
     <tr> 
      <td> 
       <b> Name </b> 
      </td> 
      <td> 
       <input type="text" name="name" /> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <b> Age</b> 
      </td> 
      <td> 
       <input type="text" name="age"/> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <b> Address</b> 
      </td> 
      <td> 
       <input type="text" name="address"/> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <b> Location</b> 
      </td> 
      <td> 
       <input type="text" name="location" /> 
      </td> 
     </tr> 
    </table> 
     <div> 
      <input type="submit" value="Submit"/> 
     </div> 
</form> 

とコントローラでは、私は次のように@ModelAttributeを使用して送信されたフォームの値を受け付けており、

@ModelAttribute Driver driver 

私はあります印刷によるトラブルシューティング

driver.name; 
driver.age; 
driver.address; 
driver.location; 

しかし、この方法ではアクセスしません。この受信方法は正しくないですか?私はこの方法で何ができますか?それ以外の場合は、受信したオブジェクトタイプを使用してフォーム値を達成するための二次的方法がありますか?

マイドライバクラスは、

パッケージcom.central.modelです。

import java.io.Serializable;

import javax.persistence。*;

@Entity 
@Table(name = "drivers") 

public class Driver implements Serializable { 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
public Integer id; 

@Column(name = "name") 
public String name; 

@Column(name = "age") 
public Integer age; 

@Column(name = "address") 
public String address; 

@Column(name = "location") 
public String location; 

public Driver() { 
} 

public Driver(String name, Integer age, String address , String location) { 
    this.name = name; 
    this.age = age; 
    this.address = address; 
    this.location = location; 
} 
} 
+0

。 – mrtasln

+0

ドライバが私のオブジェクトです、私はアクション定義で作成しました。そして、関数の始めに受け取った値をデバッグしたとき、null値を取得しました。 "@ModelAttributeドライバードライバー"コードを使用しているときに受信しません。 – Jacob

+0

Driverクラスを表示できますか? – mrtasln

答えて

1
は、以下のようなあなたのドライバの実体を変更

:あなたのドライバオブジェクトは何

@Entity 
@Table(name = "drivers") 

public class Driver implements Serializable { 
    private Integer id; 
    private String name; 
    private Integer age; 
    private String address; 
    private String location; 


@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
public Integer getId() { 
     return id; 
    } 

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

@Column(name = "name") 
public String getName() { 
     return name; 
    } 

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

    @Column(name = "age") 
    public Integer getAge() { 
     return age; 
    } 

    public void setAge(Integer age) { 
     this.age = age; 
    } 

    @Column(name = "address") 
    public String getAddress() { 
     return address; 
    } 


    public void setAddress(String address) { 
     this.address = address; 
    } 

@Column(name = "location") 
    public String getLocation() { 
     return location; 
    } 

    public void setLocation(String location) { 
     this.location = location; 
    } 




public Driver() { 
} 

public Driver(String name, Integer age, String address , String location) { 
    this.name = name; 
    this.age = age; 
    this.address = address; 
    this.location = location; 
} 
} 
+0

この新しい変更は機能しています。ご回答いただきありがとうございます。 – Jacob

+0

喜んで助けになる – mrtasln

関連する問題