0

Let's say I have a simple search page like this :

<form:form id="productsForm" method="post" modelAttribute="productsFormBean">
      <form:label path="name">Name : </form:label>
      <form:input path="name" />
      <button id="filterSubmit" type="submit">Submit</button>
</form:form>

A user can enter a name and submit the page, but he can also submit the page without entering anything.

Is it possible to obtain a RESTful url like this :

  • the user enters the name "xyz" and submits the page : www.mywebpage.com/products/name/xyz/
  • the user submits the page without a name : www.mywebpage.com/products/

Here's my controller :

    @RequestMapping(params = "search=true", value = "/**", method = RequestMethod.POST)
    public String searchHandler(@Valid final ProductsFormBean productsFormBean, final Model model) {
        // (...)
        return "productsSearch";
    }

If I change the "action" attribute of the form, the url changes. I already achieved that with javascript, by changing the action on the onSubmit event. But it's not a clean solution. Is it possible to achieve that directly in the controller ?

Christos Loupassakis
  • 1,216
  • 3
  • 16
  • 23

2 Answers2

0

No way. The controller is a pure server-side stuff. It doesn't control anything in the browser. It's invoked when a request comes in with a given URL. But it doesn't change anything in how the browser handles requests sent to the controller.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0
@RequestMapping(value="/", method=RequestMethod.POST)
public String findProduct(@RequestParam String search)
{
  if(search.isEmpty())
  {
    return "redirect:/"
  }
  else
  {
    return "redirect:/"+search;
  }
}

This should get you started, you'd still need to implement a method for handling the REST url for the search param.

Also, don't know if it's 100% accurate, but should be pretty close.

dardo
  • 4,870
  • 4
  • 35
  • 52
  • Yes ! The "redirect:/" does the trick. I had to implement new methods to handle the url but it works ! And my redirect looks more like this : return "redirect:name/"+search; . Thanks! – Christos Loupassakis Mar 08 '12 at 01:01
  • Not a problem, Spring API Documentation is great for questions like this, it's pretty much my bible at work. – dardo Mar 08 '12 at 02:55