开发者

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

开发者 https://www.devze.com 2022-12-21 15:06 出处:网络
Given some typical search form, I can\'t construct this form action when submi开发者_开发知识库tting a form:

Given some typical search form, I can't construct this form action when submi开发者_开发知识库tting a form:

/myapp/orders/${orderId)

Because the user is typing in orderId, so I can't include it in the form action.

  1. Is there a slick way to do this in 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. 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).

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). Then my search query is a create call to /myapp/searchResults/ that includes the query parameters as POST variables. 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.


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. For example, this:

/myapp/orders?id=1337

becomes

/myapp/orders/1337?id=1337
0

精彩评论

暂无评论...
验证码 换一张
取 消