2013-04-16 57 views
0

我有兩個實體類:用戶和地址 用戶等級:它具有許多其他特性加上相關類地址如何設置相關類的屬性Spring MVC中

Entity 
@Table(name = "T_USER_DETAILS") 
public class User { 


//@GenericGenerator(name = "generator",strategy = "sequence-identity",parameters = { @Parameter(name = "sequence",value = "USER_ID_SEQ")}) 

@GenericGenerator(name = "generator",strategy = "increment")  
@Id 
@GeneratedValue(generator = "generator") 
@Column(name = "USER_ID") 
private int userid; 

@Column(name = "USER_FIRSTNAME") 
private String firstname; 
@Column(name = "USER_AGE") 
private int age; 


//creating many to one relationship between address class and student class 
@ManyToOne(cascade = CascadeType.ALL) 
@JoinColumn(name = "ADDRESS_ID") 
private Address address; 

這裏的地址類也是一個enity

@Entity 
@Table(name = "T_ADDRESS_DETAILS") 
public class Address { 



    @GenericGenerator(name = "idgenerator", strategy = "increment") 
    @Id 
    @GeneratedValue(generator = "idgenerator") 
    @Column(name = "ADDRESS_ID") 
    private int addressId; 

    @Column(name = "ADDRESS_LINE1") 
    private String address_line1; 

    @Column(name = "ADDRESS_LINE2") 
    private String address_line2; 

在我的形式,我創建的用戶類的命令豆,在我的控制器類綁定它:

<form:form method = "post" action = "register.html" commandName = "newUser"> 

<br> 
<h2><i>Create new account</i></h2> 
<br> 
Enter the firstname<br> 
<input type = "text" size = "30" name = "firstname"> 
<font color = "red"> <form:errors path = "firstname" cssClass = "error"/></font><br> 
Enter the lastname<br> 
<input type = "text" size = "30" name = "lastname"> 
<font color = "red"> <form:errors path = "lastname" cssClass = "error"/></font><br> 
Enter the password<br> 
<input type = "password" size = "30" name = "password"> 
<font color = "red"> <form:errors path = "password" cssClass = "error"/></font><br> 
Enter the Email Id<br> 
<input type = "text" size = "30" name = "emailId"> 
<font color = "red"><form:errors path = "emailId" cssClass = "error"/><br></font> 
Enter the phone number<br> 
<input type = "text" size = "30" name = "phonenumber"> 
<font color = "red"><form:errors path = "phonenumber" cssClass = "error"/></font><br> 
Enter the age<br> 
<input type = "text" size = "30" name = "age"/> 
<font color = "red"><form:errors path = "age" cssClass = "error"/></font><br><br> 

Enter the Address<br> 
<input type = "textbox" name = "address" size = "30"> 
<font color = "red"><form:errors path = "address" cssClass = "error"/></font><br> 

<div align = "center"> 



<input type = "submit" name = "submit" value = "submit"> 

控制器:

@RequestMapping(method = RequestMethod.POST) 
public String register(@ModelAttribute("newUser") User user,BindingResult result,Model model,HttpSession session,HttpServletRequest request) //retrieve the backing bean with in the user attribute 
{ 
.. 
} 

這裏我的問題是我如何分配,我想從像城市,州,國家... 形式發送地址類的屬性,我需要做什麼樣的變化我的jsp頁面和我的控制器類。 請幫忙。

回答

0

你也可以用加盟類的屬性的關聯使用<form:input標籤如下所示:

<form:form commandName="person" method="post" action="${formAction}"> 
    <div class="form"> 
     <p>User</p> 
      <form:input path="firstname" /> 
      <form:input path="address.address_line1" /> 

      <input type="submit" class="btn btn-primary" /> 
    </div> 
</form:form> 

記住,關鍵是要使用表單輸入路徑來匹配聯接類的屬性。然後,您可以將此方法用於表單bean中使用的所有其他關聯。

+0

感謝他的工作:D – shashi

0

您應該更改地址bean以爲City,State等添加屬性(其中還包括使用註釋提供的良好驗證)。此後,您將不得不將輸入拆分爲地址bean的每個屬性;然後,您可以使用sping:bind標籤與符號address.city

<form:form commandName="person" method="POST" action = "register.html"> 

    <spring:bind path="address.city"> 
    <input type = "textbox" name = "address.city" size = "30"> 
    </spring:bind> 

    ... 
    ... 
</form:form> 
相關問題