【问题标题】:Spring form:select cats as toString弹簧形式:选择猫作为toString
【发布时间】: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


【解决方案1】:

你可以这样添加你的列表:

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

【讨论】:

  • 谢谢!但是为什么在我的情况下该方法充当 toString()?
  • 默认情况下它需要一个字符串值,并且由于你没有提供任何它试图将对象转换为字符串我猜。
  • 我明白了。谢谢。
【解决方案2】:

您需要遍历您的客户列表并创建一个仅包含显示名称的新列表,然后将此列表用于您在 jsp 文件中的选择。

List<String> displayNames = new ArrayList<String>();
for(Client c : listOfClients)
    displayNames.add(c.getDisplayName());
model.addAttribute("listOfDisplayNames", displayNames);

【讨论】:

  • 是的,它也可以。但我一直在寻找更优雅的方式来做到这一点。无论如何,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2021-06-24
  • 2016-08-30
  • 1970-01-01
相关资源
最近更新 更多