2016-04-24 65 views
0

我有以下兩個有一對多映射的類。學生可以選擇多個科目。如何使用窗體:選擇彈簧mvc

@Entity 
@Table (name = "studentinfo") 
public class student { 

    @Id 
    @GeneratedValue (strategy = GenerationType.IDENTITY) 
    @Column (name = "stud_id") 
    private int studId; 

    @NotEmpty 
    @Column (name = "stud_name") 
    private String studname; 

    @NotEmpty 
    @Column (name = "stud_age") 
    private int studage; 

    @OneToMany (cascade = CascadeType.ALL) 
    @JoinColumn (name = "stud_sub_id") 
    private List<subject> subject; 

    //getters and setters 

主題模型類

@Entity 
@Table (name = "subject") 
public class subject { 

@Id 
@GeneratedValue (strategy = GenerationType.IDENTITY) 
@Column (name = "Sub_id") 
private int subId; 

@NotEmpty 
@Column (name = "Subj_name") 
private String subjname; 

//getters and setters 

如何使用形式:選擇標籤顯示的主題列表中,這樣,我可以選擇一個學生多個主題,並在編輯一個特定的學生詳細,我需要爲他顯示選定的主題。這是我目前的jsp代碼。

<form:form commandName="studentdetails" action="${pageContext.request.contextPath}/stud/studentadded" method="post"> 
    <form:hidden path="studId"/> 

    <table> 

     <tr> 
      <td><label for="studname">Name: </label> </td> 
      <td><form:input path="studname" id="name"/></td> 
      <td><form:errors path="studname" cssStyle="color:#ff0000"></form:errors></td> 
     </tr> 

     <tr> 
      <td><label for="studage">Age: </label> </td> 
      <td><form:input path="studage" id="age"/></td> 
      <td><form:errors path="studage" cssStyle="color:#ff0000"></form:errors></td> 
     </tr> 
      <tr> 
      <td><label for="subject.subjname">Opting Subject: </label> </td> 
      <td> 
       <form:select path="subject.subjname"> 
       <form:option value="" label="select"></form:option>  
       <form:options items="${subjects}"/>   
       </form:select> 
      </td> 
      <td><form:errors path="subject.subjname" cssStyle="color:#ff0000"></form:errors></td> 
     </tr> 
     <tr> 
      <td colspan="3"> 

       <input type="Submit" value="Add"/> 

      </td> 
     </tr> 

    </table> 
     </form:form> 

我的表單支持對象是學生類。這裏items =「$ {subject}」包含我從控制器傳遞的List<String>中的主題列表。對於path =「subject.subjname」,它顯示以下錯誤;

org.springframework.beans.NotReadablePropertyException: Invalid property 'subject.subjname' of bean class [com.model.studentinfo]: Bean property 'subject.subjname' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? 

但我有適當的getters &二傳手。如果我使用path =「subject」,則在提交時出現以下錯誤。

400 The request sent by the client was syntactically incorrect 

但我沒有使用任何日期屬性。我如何製作表格:選擇適用於添加和編輯學生。請幫我解決一下這個。

+0

嘗試studendetails.subject.subjname。 – LearningPhase

+0

謝謝,但這不起作用。 – karthi

+0

任何人都可以幫助我。 – karthi

回答

0

你必須做一個定義字符串inputSubjects場在你的表單支持對象「studentdetails」和路徑使用以下方法:在你的路徑

<form:form modelAttribute="studentdetails" action="${pageContext.request.contextPath}/stud/studentadded" method="post"> 

    <form:select multiple="true" path="inputSubjects"> 
      <form:option value="" label="select"></form:option>  
      <form:options items="${subjects}"/>   
    </form:select>