问题描述:

Simply example, check it

Entity (USER, MOBILEPHONE)

@Entity

@Table(name = "USER")

public class User {

private Long id

private String name;

private Set<Mobilephone> mobilephones= new HashSet<mobilephones>(0);

public User(Long id)

this.id = id

}

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

public Long getId() {

return this.id;

}

public void setId(long id) {

this.id = id;

}

//getter and setter for name

@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")

public Set<Mobilephone> getMobilephones() {

return this.mobilephones;

}

public void setMobilephones(Set<Mobilephone> mobilephones) {

this.mobilephones= mobilephones;

}

@Entity

@Table(name = "MOBILEPHONE")

public class Mobilephone {

private Long id

private Long number;

private User user

public MobilePhone(Long id)

this.id = id

}

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

public Long getId() {

return this.id;

}

public void setId(Long id) {

this.id = id;

}

//getter and setter for number

@ManyToOne(fetch = FetchType.LAZY)

@JoinColumn(name = "USERID", nullable = false)

public User getUser() {

return this.user;

}

public void setUser(User user) {

this.user= user;

}

Webpage

<form:form modelAttribute="mobilephoneAttribute" action="url" method="post">

<form:input path="mobilephone"/>

<form:select path="user">

<c:forEach items="${userlist}" var="user">

<form:option value="${user.id}" label="${user.telephone" />

</c:forEach>

</form:select>

<input type="submit"/>

</form:form>

Whats happening.

After submit i get this error:

The request sent by the client was syntactically incorrect.

If I change my User: "Long id" to "String id" (and also methods) the problem disappears.

I thought at the beginning, spring has a problem with the convert Long to String?

But probably not, because we have a number where Long is saved with no problems.

Someone knows the problem?

网友答案:

try using

<form:select path="user.id"> 
        <c:forEach items="${userlist}" var="user">
            <form:option value="${user.id}" label="${user.telephone" />
        </c:forEach>
   </form:select>
相关阅读:
Top