Spring MVC 3 -中的自定义标签

发布于 2025-01-08 04:06:10 字数 846 浏览 0 评论 0原文

我正在创建一个表单,用户可以在其中选择(除其他外)产品的工厂。

每个工厂都有 ID 标识并有具体地址。

我想在以下代码中使用自定义标签:

<form:select items="${factories}" path="factory" itemValue="id" itemLabel="..."/>

首先,我尝试使用 Spring Formatter 功能(org.springframework.format.Formatter 接口),但是当我这样做时,以及当我删除“itemLabel”属性以通过 Formatter 自动显示它时):

<form:select items="${factories}" path="factory" itemValue="id"/>

但是如果设置了它,它就没有选择正确的值(在编辑的情况下)。

然后我尝试:

<form:select path="factory" itemValue="id">
    <c:forEach ...>
         <form:option value="${factory.id}" label="${factory.address.city} ${factory.address.street}"
    </c:foreach>
</form:select>

但与早期的解决方案一样,弹簧没有选择模型中设置的正确值。

我的问题是:

当选择字段的值与其标签不同时,是否可以以某种方式格式化实体,使 form:select 正常工作。

I'm creating a form in which user will be able to choose (among the others) the factory of a product.

Each factory is identified by and ID and has specific address.

I want to use custom label in following code:

<form:select items="${factories}" path="factory" itemValue="id" itemLabel="..."/>

At first i tried using Spring Formatter functionality (org.springframework.format.Formatter interface), but when I did this, and when I removed "itemLabel" attribute to have it displayed automatically via Formatter):

<form:select items="${factories}" path="factory" itemValue="id"/>

But then It wasn't selecting proper value if it was set (in case of editing).

Then I tried to:

<form:select path="factory" itemValue="id">
    <c:forEach ...>
         <form:option value="${factory.id}" label="${factory.address.city} ${factory.address.street}"
    </c:foreach>
</form:select>

But as in earlier solution spring was not selecting proper value that was set in model.

My question is:

Is it possible to format entity in a way, that form:select works properly, when a value of select field is not the same as its label.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自在安然 2025-01-15 04:06:10

我遇到了同样的问题,问题是 form:option 值和标签直接映射到属性,而不是灵活地指定 jstl。

因此,您必须手动启动 html,类似于:

<select name="factory">
    <c:forEach var="factory" items="${factories}" >
        <option value="${factory.id}" label="${factory.address.city} ${factory.address.street}"/>
    </c:forEach>
</select>

Spring 将根据名称属性(在本例中为“factory”)选择“路径”,因为它将尝试自动映射到您正在使用的模型对象。

另一种方法是向模型添加另一个字段,以根据需要连接和格式化标签。如果它是一个 @Entity 对象,则将字段设置为 @Transient

I had the same problem, the trouble is the form:option value and label maps directly to a property rather than having the flexibility to specify jstl.

So you have to hand crank the html, to something like:

<select name="factory">
    <c:forEach var="factory" items="${factories}" >
        <option value="${factory.id}" label="${factory.address.city} ${factory.address.street}"/>
    </c:forEach>
</select>

Spring will pick up the 'path' based on the name attribute('factory' in this case), as it will try and map to the model object you are using automatically.

Another way is to add another field to the model to concatenate and format label as you wish. And if its an @Entity object then make the field @Transient.

初吻给了烟 2025-01-15 04:06:10

您还可以在 Factory 类中重写 toString() 方法,

 @Override
    public String toString() {
        return "desired string"; 
}

然后在您的 jsp 中

<form:select items="${factories}" itemValue="id" path="factory"/>

You can also override toString() method in Factory class

 @Override
    public String toString() {
        return "desired string"; 
}

Then in your jsp

<form:select items="${factories}" itemValue="id" path="factory"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文