【发布时间】:2015-02-22 00:38:24
【问题描述】:
您能否告诉我代码出了什么问题?我尝试遍历 spring mvc 选择中的对象列表。结果应该是通过 getDisplayName() 检索的两个 bean 文件的连接。但是jsp中这个方法的结果看起来像toString()的结果——org.financespring.model.Client@1aaed9a9。谢谢你的帮助。代码如下:
.jsp
<body>
<form:form action="newclientpage" method="post" modelAttribute="client">
<div id="client-buttons">
<input type="button" name="client-action" value="Add Client">
<input type="button" name="client-action" value="Del Client">
<input type="button" name="client-action" value="Edit Client">
<input type="button" name="client-action" value="Show client details">
</div>
<form:select path="displayName" items="${listOfClients}" size="25" width="200px"/>
</form:form>
</body>
控制器
@RequestMapping(value = "/", method = RequestMethod.GET)
public String initNewClientForm(ModelMap model) {
Client client = new Client();
List<Client> listOfClients = clientService.getListOfClients();
model.addAttribute("client", client);
model.addAttribute("listOfClients", listOfClients);
return "clientpage";
}
豆
@Entity
@Table(name = "client")
public class Client extends BaseEntity {
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Column(name = "address", nullable = false)
private String address;
@Column(name = "city", nullable = false)
private String city;
@Column(name = "postal_code", nullable = false)
private String postalCode;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getDisplayName() {
return firstName + " " + lastName;
}
}
【问题讨论】:
-
你没有在客户端重载toString()?
-
没有。但我为什么要这样做?为什么该方法返回客户端对象的字符串表示形式?
标签: java spring forms select model-view-controller