views:

200

answers:

2

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.

  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.

A: 

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.

JacobM
Yeah we have a few weird requirements that forced my hand in this case. We don't have the capacity to search the orders table. Also, in almost every case, the user will know exactly which order they need to bring up. I'm relatively new to Spring MVC and wanted to make sure they didn't have some automagic to do this for me.In almost every other case, your approach would be the more reasonable solution (and the one I would have used). Thanks.
gmoore
A: 

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
gmoore