Chinnu's Chinnu's - 5 months ago 8
HTML Question

How to map input value from form to URL of findById()

/welcome/employees

I have a input area where user enters `"empNo"` and clicks on "Find Employee", mapping to be done to /welcome/employees/find/{id}.

[`it should insted be move to url appended with empNo`][2]


Here I have everything fine while I directly enter the url, it gives up the result.

but the Find Employee button isn't mapped to entered /welcome/employees/find/{id} instead to /welcome/employees/find/.

and how to map the entered emp No to Find Employee button.Please shed some light.

Controller get employee method working fine only when I manually enter the URL.

list.jsp form page


<input type="text" path="empNo" value="${employee.empNo}" placeholder="Employee No."/>${employee.empNo}
&nbsp;
<a href="/welcome/employees/find/${employee.empNo}" class="btn btn-primary">
<span class="glyphicon glyphicon-search"></span> Find Employee
</a>




find.jsp


<h2>Employee information</h2>
<form action="/welcome/employees/find/${employee.empNo}">
<table class="table table-bordered">
<tr>
<th>Employee No.</th>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Birth Date</th>


<th>Hire Date</th>
</tr>
<tr>
<td>"${employee.empNo}"</td>
<td>"${employee.firstName}"</td>
<td>"${employee.lastName}"</td>
<td>"${employee.gender}"</td>
<td>"${employee.birthDate}"</td>
<td>"${employee.hireDate}"</td>
</tr>
</table>
</form>


Back

@RequestMapping(value = "/employees/find/{empNo}", method = RequestMethod.GET)
public String getEmployee(@PathVariable("empNo") long empNo, Model model){
Employee employee1=this.employeeService.findEmployeeById(empNo);
model.addAttribute(employee1);
return "employees/find";
}


and when I click on find employee it directs to

http://localhost:8080/welcome/employees/find/

Tin Tin
Answer

Here is the simple approach without any javascript.

listing.jsp

<form action="/welcome/employees/find" method="get"> 
    <input name="empNo" placeholder="Employee No."/> 
    <button type="submit">Find Employee</button> 
</form>

Explaination: when you do ${employee.empNo}, you're getting the value out of the empNo variable inside employee object. But you don't want to use this value to find. You want to use the value in the textbox to find.

controller

@RequestMapping(value = "/employees/find", method = RequestMethod.GET)
public String getEmployee(@RequestParam("empNo") long empNo, Model model){
    Employee employee1=this.employeeService.findEmployeeById(empNo); 
    model.addAttribute(employee1);
    return "employees/find";
}

You don't need <form> in find.jsp unless you want to do something.