如何将输入值从表单映射到findById()的URL

时间:2016-06-16 14:03:12

标签: java html spring jstl

/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]

在我直接输入网址时,一切都很好,它会放弃结果。

但查找员工按钮未映射到输入/ welcome / employees / find / {id}而不是/ welcome / employees / find /.

以及如何将输入的emp No映射到“查找员工”按钮。请详细说明。

只有在我手动输入网址时,控制器才能使员工方法正常工作。

 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>

回到

@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";
}

当我点击查找员工时,它会指向

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

1 个答案:

答案 0 :(得分:0)

这是没有任何javascript的简单方法。

listing.jsp

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

解释:当您执行${employee.empNo}时,您将从empNo对象中的employee变量中获取值。但是你不想使用这个值来查找。您想使用文本框中的值来查找。

控制器

@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";
}

除非你想做某事,否则你在find.jsp中不需要<form>