0

I know this is going to be really simple but I can't figure out how do get a path variable from a form.

This is my form defined below:

<form:form action="dogs/" method="get">
            <div class="row">
                <div class="col-md-3">                  
                    <div class="input-group">
                    <div class="input-group-addon" style="background-color: transparent; border-right:0 solid transparent;"><span class="glyphicon glyphicon-search"></span></div>

                    <input type="text" class="form-control" placeholder="search by ID #" name="id" id="id" />
                    <div class="input-group-btn">
                    <input type="submit" class="btn btn-info" value="Go" />
                    <button class="btn btn-info" onclick="location.href='id'">Go</button>
                    </div>  
                    </div>              
                </div>
                    <br><br>
            </div>

        </form:form>

And this is my controller method, where I am suppose to get the path variable:

@RequestMapping(value ="dogs/{id}", method = RequestMethod.GET)
public String getDogId(@PathVariable(value ="id") Long value, Model model){
        return "";
 }

But the url I get and also causes an error is the following:

http://localhost:8080/controller/dogs/dogs/?id=8

what I want is: http://localhost:8080/controller/dogs/8 I don't know how to pass the input dynamically to the button or not even to the button but to the form action. Any help would be greatly appreciated.

beginnerCoder
  • 195
  • 1
  • 5
  • 17
  • URL seems right and you should probably be looking out for parameter `public String getDogId(@RequestParam("id")Long value,....` Also change the request mapping to to `@RequestMapping(value ="/dogs", method = RequestMethod.GET)` – Shahzeb Jul 13 '15 at 22:36
  • The thing is I tried @RequestParam but it gives this url= **/?id=4, I need something like this instead url=**/4. I just have no idea how to pass the parameter the user types in into the url. – beginnerCoder Jul 13 '15 at 22:38

1 Answers1

0

I'm afraid you cannot accomplish it without the help of javascript.

Edit: Here is a jsfiddle to help you with that: https://jsfiddle.net/n588jL07/2/

function setURL(form) {
    form.action="dogs/"+form.urlVal.value;
    console.log(form.action);
    event.preventDefault();
}
Anand
  • 305
  • 1
  • 2
  • 9