gpt4 book ai didi

java - Spring 形式 :select cats as toString

转载 作者:行者123 更新时间:2023-12-02 05:07:37 25 4
gpt4 key购买 nike

您能否告诉我代码出了什么问题?我尝试迭代 spring mvc 的 select 中的对象列表。结果应该是通过 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>

Controller

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

bean

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

最佳答案

您可以这样添加列表:

<form:select path="displayName">
<form:options items="${listOfClients}" itemValue="displayName" itemLabel="displayName" />
</form:select>

关于java - Spring 形式 :select cats as toString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27627666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com