简体   繁体   English

Spring MVC 3:如何稳定地构造表单动作?

[英]Spring MVC 3: How do I construct a form action restfully?

Given some typical search form, I can't construct this form action when submitting a form: 给定一些典型的搜索表单,提交表单时我无法构造此表单操作:

/myapp/orders/${orderId)

Because the user is typing in orderId, so I can't include it in the form action. 由于用户正在输入orderId,因此无法将其包含在form操作中。

  1. Is there a slick way to do this in Spring MVC 3? 在Spring MVC 3中,有什么巧妙的方法可以做到这一点吗?

  2. What about using an Interceptor to construct this? 用拦截器构造这个怎么办? Is that a good solution? 那是一个好的解决方案吗?

  3. Otherwise I'm stuck with using javascript to mung with the action onSubmit. 否则,我将无法使用javascript来完成onSubmit操作。 Fun. 好玩

In my experience a typical search query doesn't generally map seamlessly one-to-one to a result, as you're describing. 根据我的经验,正如您所描述的,典型的搜索查询通常不会无缝地一对一地映射到结果。 Even if you ask them to enter an order Id, they might want to be able to enter a partial ID and choose from a list, or they might mis-type (and you'd want to be able to give a meaningful response with possible choices, whereas an incorrect GET call to a resource that doesn't exist should just be a 404). 即使您要求他们输入订单ID,他们也可能希望能够输入部分ID并从列表中进行选择,或者他们可能输入错误(并且您希望能够给出有意义的答复选择,而对不存在的资源的不正确的GET调用应该只是404)。

What I like to do is have an intermediate resource called something like SearchResult (it doesn't have to be an actual object in your system). 我想做的是拥有一个称为SearchResult之类的中间资源(它不一定是系统中的实际对象)。 Then my search query is a create call to /myapp/searchResults/ that includes the query parameters as POST variables. 然后,我的搜索查询是对/ myapp / searchResults /的create调用,其中包含查询参数作为POST变量。 If the search result created points to a single order, than you can redirect to /orders/741, but if not, you have more ability to handle it. 如果创建的搜索结果指向单个订单,则可以重定向到/ orders / 741,但如果不是,则您有更多处理它的能力。

I ended up writing an Interceptor that looks for an "id" param in a GET request, and if it is found then append that value onto the uri and forward along. 我最终编写了一个Interceptor,它在GET请求中查找“ id”参数,如果找到,则将该值附加到uri上并向前发送。 For example, this: 例如,这:

/myapp/orders?id=1337

becomes 变成

/myapp/orders/1337?id=1337

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM