Spring MVC - 下拉对象选择 - 无主标识符
一个相当常见的用例是,有一个 Java 对象列表,可以在 Web 表单上从中进行选择 - 通常您会使用对象的主键作为值,以便控制器可以进行查找,或者只是将密钥绑定到创建/更新的任何对象。
我的问题是,可供选择的列表不是持久的、带键的对象,它们是来自服务的业务模型,而服务没有合理的方法来根据所包含的数据检索它们。下面是一些伪代码,其中向页面提供了 Foo 的列表,我们可以轻松地在 onSubmit 上与控制器通信 Foo 的名称,但是如果 Foo 还有其他字段需要提交怎么办?
控制器:
referenceData() {
...
List foos = fooService.getFoosForBar( bar )
return { 'foos', foos }
}
jsp:
<form>
...
<spring:bind path="formData.foo">
<select name="<c:out value="${status.expression}" />">
<c:forEach items="${foos}" var="foo">
<option value="<c:out value="${foo.name}"/>">
<c:out value="${foo.name}"/>
</option>
</c:forEach>
</select>
</spring:bind>
...
</form>
一些示例解决方案是使用隐藏字段来提交 Foo 的其他属性,并在选择更改时保持它们同步,但我不喜欢在这种情况下使用 JavaScript,因为它可能会增加混乱。当然还有其他方法可以实现这一点。
我的问题是是否存在实现这一目标的标准实践?或者我应该想出自己的做法?如果可能的话,我宁愿不重新发明轮子,而且这看起来很常见,只是即兴发挥可能不是最好的方法。
A fairly common use case occurs where there is a list of Java objects, from which selections can be made on a web form - usually you'd use the primary key of the object as the value so that the controller could either do a lookup, or just bind the key to whichever object is created/updated.
My problem is that the list to choose from are not persistent, keyed objects, they are business models from a service which have no reasonable way to retrieve them based on the data contained. Below is some psuedo code where a list of Foo's are given to the page, and we can easily communicate to the controller onSubmit the name of Foo, but what if there are other fields of Foo that need to be submitted?
controller:
referenceData() {
...
List foos = fooService.getFoosForBar( bar )
return { 'foos', foos }
}
jsp:
<form>
...
<spring:bind path="formData.foo">
<select name="<c:out value="${status.expression}" />">
<c:forEach items="${foos}" var="foo">
<option value="<c:out value="${foo.name}"/>">
<c:out value="${foo.name}"/>
</option>
</c:forEach>
</select>
</spring:bind>
...
</form>
Some example solutions would be to use hidden fields to submit Foo's other properties and keep them in sync as the selection is changed, but I prefer not to use JavaScript in a situation like this where it will likely add confusion. There are certainly other ways to accomplish this too.
My question is does there exist any standard practice for accomplishing this? Or should I just come up with my own way of doing so? I'd rather not re-invent wheels if possible, and this is so seemingly common that just winging it may not be the best approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的限制,您必须将 foos 的其他数据成员编码为选项的值。
<代码>
<选项标签=“${foo.name}”值=“${foo.encodedValues}”/>
encodedValues
方法可能看起来像这样:如果您有许多需要编码值的选择,您可能需要创建一个类来对这些值进行编码和解码以集中代码。
Based on your limitations, you must encode the other data memebers of foos as the value of the option.
<option label="${foo.name}" value="${foo.encodedValues}"/>
The
encodedValues
method might look something like this:If you have a number of selects that need to have encoded values, you may want to create a class that does the encoding and decoding of these values to centralize the code.
您可以使用列表中元素的索引将其返回到 POST 请求中。
在 POST 处理程序中,使用 foos.get(formData.getFooIndex())
如果 foos 可以在 GET 和 POST 请求之间更改,则需要将 foos 放入会话中,以便您在 POST 处理程序中肯定引用与 GET 处理程序中相同的对象。
You can use the index of the element in the list to get it back in the POST request.
In your POST handler, use
foos.get(formData.getFooIndex())
If foos can change between the GET and POST requests, you need to put
foos
in session so that you definitely reference the same object in your POST handler as you did in the GET handler.