0

我有两个实体类:用户和地址用户类:它有许多其他属性以及相关的类地址

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;

这是地址类,它也是一个实体

@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;

在我的表单中,我正在为用户类创建一个命令 bean,并将其绑定到我的控制器类中:

<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 页面和我的控制器类中做哪些更改。请帮忙。

4

2 回答 2

0

您应该更改地址 bean 以添加城市、州等的属性(除其他外,还使用注释提供了良好的验证)。此后,您必须将输入拆分为地址 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>
于 2013-04-16T08:00:40.330 回答
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 中使用的所有其他关联。

于 2013-04-16T08:12:55.837 回答